<?php

namespace backend\modules\shop\models\searchs;

use yii\base\Model;
use yii\data\ActiveDataProvider;
use yii\helpers\ArrayHelper;
use backend\modules\shop\models\ars\Order;
use Yii;
use yii\helpers\Html;

/**
 * OrderSearch represents the model behind the search form of ` backend\modules\shop\models\ars\Order`.
 */
class OrderSearch extends Order
{
    /**
     * @return array
     * 增加创建时间查询字段
     */
    public function attributes()
    {
        return ArrayHelper::merge(['created_at_range'], parent::attributes());
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'user_id', 'status', 'type', 'goods_count', 'goods_amount', 'shipping_amount', 'shipping_type', 'taking_site', 'pay_type', 'pay_at', 'payment_amount', 'receivables', 'discount_amount', 'updated_at', 'created_at'], 'integer'],
            [['order_sn', 'invoice_id', 'consignee', 'phone', 'province', 'city', 'area', 'payment_sn', 'remarks', 'discount_description'], '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'
            ],
            [
                'attribute' => 'id',
                'width' => '5%',
            ],
            [
                'attribute' => 'user_id',
                'width' => '5%',
//                'value' => function ($model) {
//                    return base64_decode($model->user->nickname);
//                }
            ],
            [
                'attribute' => 'order_sn',
                'width' => '8%',
            ],
//            [
//                'attribute' => 'invoice_id',
//                'width' => '8%',
//            ],
            [
                'attribute' => 'type',
                'width' => '5%',
                'value' => function ($model) {
                    return !empty($model->type) ? Order::dropDown('type', $model->type) : '';
                }
            ],
            [
                'label' => '订单信息',
                'format' => 'raw',
                'width' => '7%',
                'value' => function ($model) {
                    return Yii::$app->controller->renderPartial('order_info', ['model' => $model]);
                },
            ],
            [
                'label' => '物流信息',
                'format' => 'raw',
                'width' => '20%',
                'value' => function ($model) {
                    return Yii::$app->controller->renderPartial('shipping_info', ['model' => $model]);
                },
            ],
            [
                'label' => '收货信息',
                'format' => 'raw',
                'width' => '17%',
                'value' => function ($model) {
                    return Yii::$app->controller->renderPartial('table_consignee_info', ['model' => $model]);
                },
            ],
            [
                'attribute' => 'status',
                'width' => '5%',
                'value' => function ($model) {
                    return Order::dropDown('status', $model->status);
                }
            ],
//            [
//                'attribute' => 'updated_at',
//                'width' => '8%',
//                'value' => function ($model) {
//                    return date('Y-m-d H:i:s', $model->updated_at);
//                }
//            ],
//            [
//                'attribute' => 'created_at',
//                'value' => function ($model) {
//                    return date('Y-m-d H:i:s', $model->created_at);
//                }
//            ],
            [
                'class' => 'iron\grid\ActionColumn',
                'align' => 'center',
                'config' => [
                    [
                        'name' => 'view',
                        'icon' => 'list',
                        'title' => '详情',
                    ],
                    [
                        'name' => 'update',
                        'icon' => 'pencil',
                        'title' => '修改'
                    ],
                    [
                        'name' => 'delivery',
                        'icon' => 'box',
                        'title' => '发货',
                        'hide' => [
                            'attributes' => [
                                'status',
                                'status',
                                'status',
                                'status',
                                'status',
                                'status',
                                'status',
                            ],
                            'values' => [
                                Order::STATUS_UNCONFIRMED,
                                Order::STATUS_NONPAYMENT,
                                Order::STATUS_CANCEL,
                                Order::STATUS_PAYMENT_TO_BE_CONFIRMED,
                                Order::STATUS_APPLY_REFUND,
                                Order::STATUS_REFUND,
                                Order::STATUS_SHIPMENT_ALL,
                                Order::STATUS_FINISH
                            ],
                            'rule' => 'or'
                        ]
                    ],
                    [
                        'name' => 'refund',
                        'icon' => 'dollar',
                        'title' => '退款',
                        'hide' => [
                            'attributes' => [
                                'status',
                                'status',
                                'status',
                                'status',
                                'status',
                                'status',
                                'status',
                            ],
                            'values' => [
                                Order::STATUS_UNCONFIRMED,
                                Order::STATUS_NONPAYMENT,
                                Order::STATUS_CANCEL,
                                Order::STATUS_PAYMENT_TO_BE_CONFIRMED,
                                Order::STATUS_REFUND,
                                Order::STATUS_SHIPMENT_ALL,
                                Order::STATUS_FINISH
                            ],
                            'rule' => 'or'
                        ]
                    ],
                ],
            ],
        ];
    }

    /**
     * @param $params
     * @return ActiveDataProvider
     * 不分页的所有数据
     */
    public function allData($params)
    {
        $query = Order::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)
    {
        $query = Order::find();

        // 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,
            'user_id' => $this->user_id,
            'status' => $this->status,
            'type' => $this->type,
            'goods_count' => $this->goods_count,
            'goods_amount' => $this->goods_amount,
            'shipping_amount' => $this->shipping_amount,
            'shipping_type' => $this->shipping_type,
            'taking_site' => $this->taking_site,
            'pay_type' => $this->pay_type,
            'pay_at' => $this->pay_at,
            'payment_amount' => $this->payment_amount,
            'receivables' => $this->receivables,
            'discount_amount' => $this->discount_amount,
            'updated_at' => $this->updated_at,
            'created_at' => $this->created_at,
        ]);

        $query->andFilterWhere(['like', 'order_sn', $this->order_sn])
            ->andFilterWhere(['like', 'invoice_id', $this->invoice_id])
            ->andFilterWhere(['like', 'consignee', $this->consignee])
            ->andFilterWhere(['like', 'phone', $this->phone])
            ->andFilterWhere(['like', 'province', $this->province])
            ->andFilterWhere(['like', 'city', $this->city])
            ->andFilterWhere(['like', 'area', $this->area])
            ->andFilterWhere(['like', 'payment_sn', $this->payment_sn])
            ->andFilterWhere(['like', 'remarks', $this->remarks])
            ->andFilterWhere(['like', 'discount_description', $this->discount_description]);
        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;
    }
}