<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\Cd4;
use app\base\Converter;
/**
 * Cd4Search represents the model behind the search form about `app\models\Cd4`.
 */
class Cd4Search extends Cd4
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['cd4_test_id', 'cd4_test_q1', 'cd4_test_q2', 'cd4_test_q3', 'cd4_test_q4', 'cd4_test_q5', 'cd4_test_q6', 'create_time', 'create_user', 'update_time', 'update_user'], 'integer'],
            [['cd4_test_survey', 'cd4_test_part_id', 'cd4_test_q3', 'cd4_test_q4', '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 = Cd4::find();

        // add conditions that should always apply here

        $request = Yii::$app->request;
        if ($request->isGet)
            $pagesize = $request->get('pagesize');
        if ($request->isPost)
            $pagesize = $request->post('pagesize');

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
			/*
			'sort' => (Yii::$app->request->isAjax) ? false : [
                'defaultOrder'=>['record_date'=> SORT_DESC],
            ],
            'pagination' => ($pagesize != "") ? false : []
			*/
			
			'sort'=> (Yii::$app->request->isAjax) ? ['defaultOrder'=>['cd4_test_part_id'=> SORT_ASC, 'cd4_test_q3'=> SORT_DESC]] : [              
                'defaultOrder'=>['record_date'=> SORT_DESC, 'cd4_test_part_id'=> SORT_ASC,'cd4_test_q3'=> SORT_DESC],
			],
			'pagination' => ($pagesize != "") ? ['pagesize'=>6] : []
        ]);

        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([
            'cd4_test_id' => $this->cd4_test_id,
            'cd4_test_q1' => $this->cd4_test_q1,
            'cd4_test_q2' => $this->cd4_test_q2,
            'cd4_test_q3' => $this->cd4_test_q3,
            'cd4_test_q4' => $this->cd4_test_q4,
            'cd4_test_q5' => $this->cd4_test_q5,
            'cd4_test_q6' => $this->cd4_test_q6,
            'create_time' => $this->create_time,
            'create_user' => $this->create_user,
            'update_time' => $this->update_time,
            'update_user' => $this->update_user,
            //'cd4_test_q3' => Converter::toUnixTimeformat($this->cd4_test_q3),
            'record_date' => Converter::toUnixTimeformat($this->record_date),
        ]);

        $query->andFilterWhere(['like', 'cd4_test_survey', $this->cd4_test_survey])
            ->andFilterWhere(['like', 'cd4_test_part_id', $this->cd4_test_part_id])
            ->andFilterWhere(['like', 'status', $this->status]);

        // $query->addGroupBy(['cd4_test_survey', 'cd4_test_part_id', 'record_date']);

        $query->limit($pagesize);

        return $dataProvider;
    }
}