<?php

namespace app\models;

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

/**
 * SubstanceusefupSearch represents the model behind the search form about `app\models\Substanceusefup`.
 */
class SubstanceusefupSearch extends Substanceusefup
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['substance_use_id', 'substance_use_q1', 'substance_use_q2', 'substance_use_q3', 'substance_use_q4', 'substance_use_q5', 'substance_use_q6a', 'create_time', 'create_user', 'update_time', 'update_user'], 'integer'],
            [['substance_use_survey', 'substance_use_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 = Substanceusefup::find()->where(["<", "visit_code", 1]);
		
        // 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([
            'substance_use_id' => $this->substance_use_id,
            'substance_use_q1' => $this->substance_use_q1,
			'substance_use_q2' => $this->substance_use_q2,
			'substance_use_q3' => $this->substance_use_q3,
            'substance_use_q4' => $this->substance_use_q4,
            'substance_use_q5' => $this->substance_use_q5,
            'substance_use_q6a' => $this->substance_use_q6a,
			'substance_use_q7a' => $this->substance_use_q7a,
            'substance_use_q8a' => $this->substance_use_q8a,
			'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', 'substance_use_survey', $this->substance_use_survey])
            ->andFilterWhere(['like', 'substance_use_part_id', $this->substance_use_part_id])
            ->andFilterWhere(['like', 'substance_use_q1', $this->substance_use_q1])
            ->andFilterWhere(['like', 'substance_use_q2', $this->substance_use_q2])            
            ->andFilterWhere(['like', 'status', $this->status]);

        return $dataProvider;
    }
}
