<?php

namespace app\models;

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

/**
 * DepressionSearch represents the model behind the search form about `app\models\Depression`.
 */
class DepressionSearch extends Depression
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['depression_id', 'depression_q1', 'depression_q2', 'depression_q3', 'depression_q4', 'depression_q5', 'depression_q6', 'create_time', 'create_user', 'update_time', 'update_user'], 'integer'],
            [['depression_survey', 'depression_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 = Depression::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
        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([
            'depression_id' => $this->depression_id,
            'depression_q1' => $this->depression_q1,
			'depression_q2' => $this->depression_q2,
			'depression_q3' => $this->depression_q3,
            'depression_q4' => $this->depression_q4,
            'depression_q5' => $this->depression_q5,
            'depression_q6' => $this->depression_q6,
			'depression_q7' => $this->depression_q7,
            'depression_q8' => $this->depression_q8,
			'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', 'depression_survey', $this->depression_survey])
            ->andFilterWhere(['like', 'depression_part_id', $this->depression_part_id])
            ->andFilterWhere(['like', 'depression_q1', $this->depression_q1])
            ->andFilterWhere(['like', 'depression_q2', $this->depression_q2])           
            ->andFilterWhere(['like', 'status', $this->status]);

        return $dataProvider;
    }
}
