<?php

namespace PortalPeople\shippingcosts\models;

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

/**
 * Shippingcostssearch represents the model behind the search form about `backend\models\Shippingcosts`.
 */
class Shippingcostssearch extends Shippingcosts
{
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'language', 'created_by', 'updated_by', 'deleted_by', 'relatedid', 'centerid', 'status', 'createdby_name', 'updatedby_name', 'deletedby_name', 'type', 'haulierid', 'postcodes', 'notes'], 'safe'],
            [['created_at', 'updated_at', 'deleted_at'], 'integer'],

        ];
    }

    /**
     * {@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 = Shippingcosts::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;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'deleted_at' => $this->deleted_at,
            'prices' => $this->prices,
        ]);

        $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', 'createdby_name', $this->createdby_name])
            ->andFilterWhere(['like', 'updatedby_name', $this->updatedby_name])
            ->andFilterWhere(['like', 'deletedby_name', $this->deletedby_name])
            ->andFilterWhere(['like', 'type', $this->type])
            ->andFilterWhere(['like', 'haulierid', $this->haulierid])
            ->andFilterWhere(['like', 'postcodes', $this->postcodes])
            ->andFilterWhere(['like', 'notes', $this->notes]);

        return $dataProvider;
    }
}
