<?php

namespace app\models;

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

/**
 * HivtestingSearch represents the model behind the search form about `app\models\Hivtesting`.
 */
class HivtestingSearch extends Hivtesting
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['hiv_test_id', 'hiv_test_q1', 'hiv_test_q2', 'hiv_test_q3', 'hiv_test_q4', 'hiv_test_q5', 'hiv_test_q6', 'hiv_test_q7', 'hiv_test_q8', 'hiv_test_q9', 'hiv_test_q10', 'hiv_test_q11', 'hiv_test_q12', 'hiv_test_q13', 'hiv_test_q14', 'hiv_test_q15', 'hiv_test_q16', 'hiv_test_q17', 'hiv_test_q18', 'hiv_test_q19', 'hiv_test_q20', 'create_time', 'create_user', 'update_time', 'update_user'], 'integer'],
            [['hiv_test_survey', 'hiv_test_part_id', '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 = Hivtesting::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([
            'hiv_test_id' => $this->hiv_test_id,
            'hiv_test_q1' => $this->hiv_test_q1,
            'hiv_test_q2' => $this->hiv_test_q2,
            'hiv_test_q3' => $this->hiv_test_q3,
            'hiv_test_q4' => $this->hiv_test_q4,
            'hiv_test_q5' => $this->hiv_test_q5,
            'hiv_test_q6' => $this->hiv_test_q6,
            'hiv_test_q7' => $this->hiv_test_q7,
            'hiv_test_q8' => $this->hiv_test_q8,
            'hiv_test_q9' => $this->hiv_test_q9,
            'hiv_test_q10' => $this->hiv_test_q10,
            'hiv_test_q11' => $this->hiv_test_q11,
            'hiv_test_q12' => $this->hiv_test_q12,
            'hiv_test_q13' => $this->hiv_test_q13,
            'hiv_test_q14' => $this->hiv_test_q14,
            'hiv_test_q15' => $this->hiv_test_q15,
            'hiv_test_q16' => $this->hiv_test_q16,
            'hiv_test_q17' => $this->hiv_test_q17,
            'hiv_test_q18' => $this->hiv_test_q18,
            'hiv_test_q19' => $this->hiv_test_q19,
            'hiv_test_q20' => $this->hiv_test_q20,
            'create_time' => $this->create_time,
            'create_user' => $this->create_user,
            'update_time' => $this->update_time,
            'update_user' => $this->update_user,
        ]);

        $query->andFilterWhere(['like', 'hiv_test_survey', $this->hiv_test_survey])
            ->andFilterWhere(['like', 'hiv_test_part_id', $this->hiv_test_part_id])
            ->andFilterWhere(['like', 'status', $this->status]);

        return $dataProvider;
    }
}
