<?php

namespace PortalPeople\shippingcosts\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;

/**
 * SysShippingSettingSearch represents the model behind the search form of
 * `PortalPeople\shippingcosts\models\SysShippingSetting`.
 */
class SysShippingSettingSearch extends SysShippingSetting
{
    public $created_at_range;
    public $updated_at_range;

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [[
                'id',
                'language',
                'created_by',
                'updated_by',
                'deleted_by',
                'relatedid',
                'centerid',
                'status',
                'title',
                'max_shipping_cost',
                'shipping_promotion',
                'zero_cost_shipping',
                'handling_fee',
                'countries',
                'error_message',
                'totaling_algo',
            ], 'safe'],
            [[
                'created_at',
                'updated_at',
                'deleted_at',
                'shipping_group',
                'applicable_countries',
                'enabled',
                'package_id',
                'post_code_range',
                'uk_post_code',
                'discount_price',
                'exclude_fornt_price',
                'cal_handling_fee',
                'apply_handling_fee',
            ], 'integer'],
            [['created_at_range', 'updated_at_range', 'updatedby_name', 'createdby_name'], 'string'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = SysShippingSetting::find();

        // add conditions that should always apply here

        $pagesizeName = 'per-page';
        $psize = !empty(Yii::$app->request->get($pagesizeName))
            || Yii::$app->request->get($pagesizeName) == 0
            ? Yii::$app->request->get($pagesizeName)
            : (empty(Yii::$app->params['gridsize']) == false
                ? Yii::$app->params['gridsize']
                : 20);
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSizeParam' => $pagesizeName,
                'pageSize' => $psize,
            ],
        ]);

        $this->load($params);

        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;
        }
        if (!empty($this->created_at_range) && strpos($this->created_at_range, '-') !== false) {
            list($start_date, $end_date) = explode(' - ', $this->created_at_range);
            $query->andFilterWhere([
                'between',
                'sys_shipping_setting.created_at',
                strtotime(str_replace('/', '-', $start_date)),
                strtotime(str_replace('/', '-', $end_date)),
            ]);
        }
        if (!empty($this->updated_at_range) && strpos($this->updated_at_range, '-') !== false) {
            list($start_date1, $end_date1) = explode(' - ', $this->updated_at_range);
            $query->andFilterWhere([
                'between',
                'sys_shipping_setting.updated_at',
                strtotime(str_replace('/', '-', $start_date1)),
                strtotime(str_replace('/', '-', $end_date1)),
            ]);
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'deleted_at' => $this->deleted_at,
            'shipping_group' => $this->shipping_group,
            'applicable_countries' => $this->applicable_countries,
            'enabled' => $this->enabled,
            'package_id' => $this->package_id,
            'post_code_range' => $this->post_code_range,
            'uk_post_code' => $this->uk_post_code,
            'discount_price' => $this->discount_price,
            'exclude_fornt_price' => $this->exclude_fornt_price,
            'cal_handling_fee' => $this->cal_handling_fee,
            'apply_handling_fee' => $this->apply_handling_fee,
        ]);

        $query->andFilterWhere(['like', 'id', $this->id])
            ->andFilterWhere(['like', 'language', $this->language])
            ->andFilterWhere(['like', 'created_by', $this->created_by])
            ->andFilterWhere(['like', 'updated_by', $this->updated_by])
            ->andFilterWhere(['like', 'deleted_by', $this->deleted_by])
            ->andFilterWhere(['like', 'relatedid', $this->relatedid])
            ->andFilterWhere(['like', 'centerid', $this->centerid])
            ->andFilterWhere(['like', 'status', $this->status])
            ->andFilterWhere(['like', 'title', $this->title])
            ->andFilterWhere(['like', 'max_shipping_cost', $this->max_shipping_cost])
            ->andFilterWhere(['like', 'shipping_promotion', $this->shipping_promotion])
            ->andFilterWhere(['like', 'zero_cost_shipping', $this->zero_cost_shipping])
            ->andFilterWhere(['like', 'handling_fee', $this->handling_fee])
            ->andFilterWhere(['like', 'countries', $this->countries])
            ->andFilterWhere(['like', 'error_message', $this->error_message])
            ->andFilterWhere(['like', 'sys_shipping_setting.createdby_name', $this->createdby_name])
            ->andFilterWhere(['like', 'sys_shipping_setting.updatedby_name', $this->updatedby_name])
            ->andFilterWhere(['like', 'totaling_algo', $this->totaling_algo]);

        return $dataProvider;
    }
}
