<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Contact;

/**
 * ContactSearch represents the model behind the search form about `app\models\Contact`.
 */
class ContactSearch extends Contact
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['contact_id', 'contact_q2', 'contact_q3', 'contact_q5', 'contact_q8', 'create_time', 'create_user', 'update_time', 'update_user'], 'integer'],
            [['contact_survey', 'contact_part_id', 'contact_q1', 'contact_q4', 'contact_q6', 'contact_q7', 'contact_q9', 'status','record_date'], 'safe'],
        ];
    }

    /**
     * @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 = Contact::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
			'sort'=>[
                'defaultOrder'=>['record_date'=> SORT_DESC],
            ],
        ]);
        if(empty($params))
            $params = Yii::$app->request->post();
        $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([
            'contact_id' => $this->contact_id,
            'contact_q2' => $this->contact_q2,
            'contact_q3' => $this->contact_q3,
            'contact_q5' => $this->contact_q5,
            'contact_q8' => $this->contact_q8,
            'create_time' => $this->create_time,
            'create_user' => $this->create_user,
            'update_time' => $this->update_time,
            'update_user' => $this->update_user,
        ]);

        $query->andFilterWhere(['like', 'contact_survey', $this->contact_survey])
            ->andFilterWhere(['like', 'contact_part_id', $this->contact_part_id])
            ->andFilterWhere(['like', 'contact_q1', $this->contact_q1])
            ->andFilterWhere(['like', 'contact_q4', $this->contact_q4])
            ->andFilterWhere(['like', 'contact_q6', $this->contact_q6])
            ->andFilterWhere(['like', 'contact_q7', $this->contact_q7])
            ->andFilterWhere(['like', 'contact_q9', $this->contact_q9])
            ->andFilterWhere(['like', 'status', $this->status]);

        return $dataProvider;
    }
}
