<?php

namespace app\models;

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

/**
 * ScreeningSearch represents the model behind the search form about `app\models\Screening`.
 */
class ScreeningSearch extends Screening
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['mem_scrn_id', 'mem_scrn_q1', 'mem_scrn_q2', 'mem_scrn_q3', 'mem_scrn_q4', 'mem_scrn_q5', 'mem_scrn_q6', 'mem_scrn_q7', 'mem_scrn_q8', 'mem_scrn_q9', 'mem_scrn_q10', 'mem_scrn_q11', 'mem_scrn_q12', 'mem_scrn_q13', 'mem_scrn_q14', 'mem_scrn_q15', 'mem_scrn_q16', 'mem_scrn_q17', 'mem_scrn_q18', 'mem_scrn_q19', 'mem_scrn_q20', 'mem_scrn_q21', 'mem_scrn_q22', 'mem_scrn_q23', 'mem_scrn_q24', 'mem_scrn_q25','mem_scrn_q26','mem_scrn_q27','mem_scrn_q28', 'create_time', 'create_user', 'update_time', 'update_user'], 'integer'],
            [['mem_scrn_survey', 'mem_scrn_part_id', 'status','record_date','mem_scrn_q28'], '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 = Screening::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([
            'mem_scrn_id' => $this->mem_scrn_id,
            'mem_scrn_q1' => $this->mem_scrn_q1,
            'mem_scrn_q2' => $this->mem_scrn_q2,
            'mem_scrn_q3' => $this->mem_scrn_q3,
            'mem_scrn_q4' => $this->mem_scrn_q4,
            'mem_scrn_q5' => $this->mem_scrn_q5,
            'mem_scrn_q6' => $this->mem_scrn_q6,
            'mem_scrn_q7' => $this->mem_scrn_q7,
            'mem_scrn_q8' => $this->mem_scrn_q8,
            'mem_scrn_q9' => $this->mem_scrn_q9,
            'mem_scrn_q10' => $this->mem_scrn_q10,
            'mem_scrn_q11' => $this->mem_scrn_q11,
            'mem_scrn_q12' => $this->mem_scrn_q12,
            'mem_scrn_q13' => $this->mem_scrn_q13,
            'mem_scrn_q14' => $this->mem_scrn_q14,
            'mem_scrn_q15' => $this->mem_scrn_q15,
            'mem_scrn_q16' => $this->mem_scrn_q16,
            'mem_scrn_q17' => $this->mem_scrn_q17,
            'mem_scrn_q18' => $this->mem_scrn_q18,
            'mem_scrn_q19' => $this->mem_scrn_q19,
            'mem_scrn_q20' => $this->mem_scrn_q20,
            'mem_scrn_q21' => $this->mem_scrn_q21,
            'mem_scrn_q22' => $this->mem_scrn_q22,
            'mem_scrn_q23' => $this->mem_scrn_q23,
            'mem_scrn_q24' => $this->mem_scrn_q24,
            'mem_scrn_q25' => $this->mem_scrn_q25,
			'mem_scrn_q26' => $this->mem_scrn_q26,
			'mem_scrn_q27' => $this->mem_scrn_q27,
            'mem_scrn_q28' => $this->mem_scrn_q28,
            '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', 'mem_scrn_survey', $this->mem_scrn_survey])
            ->andFilterWhere(['like', 'mem_scrn_part_id', $this->mem_scrn_part_id])
            ->andFilterWhere(['like', 'status', $this->status]);

        return $dataProvider;
    }
}