<?php

namespace backend\modules\shop\models\searchs;

use backend\modules\shop\models\ars\City;
use backend\modules\shop\models\ars\Province;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use yii\helpers\ArrayHelper;
use backend\modules\shop\models\ars\ExpressArea;
use yii;
use yii\bootstrap4\Html;

/**
 * ExpressAreaSearch represents the model behind the search form of `backend\modules\shop\models\ars\ExpressArea`.
 */
class ExpressAreaSearch extends ExpressArea
{
    /**
    * @return array
    * 增加创建时间查询字段
    */
    public function attributes()
        {
            return ArrayHelper::merge(['created_at_range'], parent::attributes());
        }
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'express_template', 'extra_price', 'basic_price', 'basic_count', 'extra_count', 'updated_at', 'created_at'], 'integer'],
            [['province', 'city', 'area'], 'safe'],
            ['created_at_range','safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }
    /**
    * @return array
    * 列格式
    */
    public function columns()
    {
        return  [
            [
                'class' => 'blobt\grid\CheckboxColumn',
                'width' => '2%',
                'align' => 'center'
            ],
            'id',
            ['attribute' => 'city',
                'value' => function ($model) {
                    $expressAreas = ExpressArea::findOne($model->id);
                    $expressAresCityIdArr = explode(',', $expressAreas->city);
                    $cities = [];
                    $provinces = Province::find()->cache(0)->all();
                    foreach ($provinces as $k => $v) {
                        $cityId = City::find()
                            ->select(['city_id'])
                            ->where(['province_id' => $v->province_id])
                            ->column();
                        if (empty(array_diff($cityId, $expressAresCityIdArr))) {
                            $cities[] = $v->name;
                        }else{
                            foreach (\backend\modules\shop\models\ars\City::find()->andWhere(['in', 'city_id', array_diff($cityId, array_diff($cityId, $expressAresCityIdArr))])->all() as $city) {
                                $cities[] = $city->name;
                            }
                        }
                    }
                    return implode(' , ', $cities);
                },
            ],
            [
                'class' => 'iron\grid\ActionColumn',
                'align' => 'center',
                'config' => [
                    [
                        'name' => 'express-area-view',
                        'icon' => 'list',
                        'title' => '详情',
                    ],
                    [
                        'name' => 'express-area-update',
                        'icon' => 'pencil',
                        'title' => '修改'
                    ],
                    [
                        'name' => 'express-area-delete',
                        'icon' => 'trash',
                        'title' => '删除',
                        'contents' => '确定删除?'
                    ]
                ],
            ],
        ];
    }
    /**
    * @param $params
    * @return ActiveDataProvider
    * 不分页的所有数据
    */
    public function allData($params)
    {
        $query =  ExpressArea::find();
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => false,
            'sort' => false
        ]);
        $this->load($params);
        return $this->filter($query, $dataProvider);
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params, $expressTemplateId)
    {
        $query = ExpressArea::find()->where(['express_template' => $expressTemplateId]);

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSizeLimit' => [1, 200]
            ],
            'sort' => [
                'defaultOrder' => [
                    'id' => SORT_DESC,
                ]
            ],
        ]);

        $this->load($params);
        return $this->filter($query, $dataProvider);
    }
    /**
    * @param $query
    * @param $dataProvider
    * @return ActiveDataProvider
    * 条件筛选
    */
    private function filter($query, $dataProvider){
        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'express_template' => $this->express_template,
            'extra_price' => $this->extra_price,
            'basic_price' => $this->basic_price,
            'basic_count' => $this->basic_count,
            'extra_count' => $this->extra_count,
            'updated_at' => $this->updated_at,
            'created_at' => $this->created_at,
        ]);

        $query->andFilterWhere(['like', 'province', $this->province])
            ->andFilterWhere(['like', 'city', $this->city])
            ->andFilterWhere(['like', 'area', $this->area]);
        if ($this->created_at_range) {
            $arr = explode(' ~ ', $this->created_at_range);
            $start = strtotime($arr[0]);
            $end = strtotime($arr[1]) + 3600 * 24;
            $query->andFilterWhere(['between', 'created_at', $start, $end]);
        }
        return $dataProvider;
    }
}