<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Demographics;
use app\base\Converter;

/**
 * DemographicsSearch represents the model behind the search form about `app\models\Demographics`.
 */
class DemographicsSearch extends Demographics
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['demographics_id', 'demographics_q1', 'demographics_q2', 'demographics_q3', 'demographics_q4', 'demographics_q5', 'demographics_q6', 'demographics_q7', 'demographics_q8', 'demographics_q9', 'demographics_q10', 'create_time', 'create_user', 'update_time', 'update_user'], 'integer'],
            [['demographics_survey', 'demographics_part_id', 'status','record_date','visit_code'], '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 = Demographics::find()->where(["=", "visit_code", 1]);

        // 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([
            'demographics_id' => $this->demographics_id,
            'demographics_q1' => $this->demographics_q1,
			'demographics_q2' => $this->demographics_q2,
			'demographics_q3' => $this->demographics_q3,
            'demographics_q4' => $this->demographics_q4,
            'demographics_q5' => $this->demographics_q5,
            'demographics_q6' => $this->demographics_q6,
            'demographics_q7' => $this->demographics_q7,
            'demographics_q8' => $this->demographics_q8,
            'demographics_q9' => $this->demographics_q9,
            'demographics_q10' => $this->demographics_q10,
			'visit_code' => $this->visit_code, 
            'create_time' => $this->create_time,
            'create_user' => $this->create_user,
            'update_time' => $this->update_time,
            'update_user' => $this->update_user,
			'record_date' => Converter::toUnixTimeformat($this->record_date),
			
        ]);

        $query->andFilterWhere(['like', 'demographics_survey', $this->demographics_survey])
            ->andFilterWhere(['like', 'demographics_part_id', $this->demographics_part_id])
            ->andFilterWhere(['like', 'demographics_q1', $this->demographics_q1])
            ->andFilterWhere(['like', 'demographics_q2', $this->demographics_q2])			          
            ->andFilterWhere(['like', 'status', $this->status]);

        return $dataProvider;
    }
}
