<?php

namespace app\models;

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

/**
 * HivvisitartrefillSearch represents the model behind the search form about `app\models\Hivvisit`.
 */
class HivvisitartrefillSearch extends Hivvisit
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['hiv_visit_id', 'hiv_visit_q1', 'hiv_visit_q2', 'hiv_visit_q3', 'hiv_visit_q4', 'hiv_visit_q5', 'hiv_visit_q6', 'hiv_visit_q7', 'hiv_visit_q8', 'hiv_visit_q9', 'hiv_visit_q9a', 'hiv_visit_q10', 'hiv_visit_q11', 'create_time', 'create_user', 'update_time', 'update_user'], 'integer'],
            [['hiv_visit_survey', 'hiv_visit_part_id', 'hiv_visit_q6a', 'hiv_visit_q7a', 'hiv_visit_q8a', '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 = Hivvisit::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'=>['hiv_visit_part_id'=> SORT_ASC, 'hiv_visit_q3'=> SORT_DESC],
                'defaultOrder'=>['record_date'=> SORT_DESC, 'hiv_visit_q3'=> SORT_ASC],
            ],*/
			
			'sort'=> (Yii::$app->request->isAjax) ? ['defaultOrder'=>['hiv_visit_part_id'=>SORT_ASC, 'hiv_visit_q3'=>SORT_DESC]] : [              
                'defaultOrder'=>['record_date'=>SORT_DESC, 'hiv_visit_part_id'=>SORT_ASC, 'hiv_visit_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([
            'hiv_visit_id' => $this->hiv_visit_id,
            'hiv_visit_q1' => $this->hiv_visit_q1,
            'hiv_visit_q2' => $this->hiv_visit_q2,
            'hiv_visit_q3' => $this->hiv_visit_q3,
            'hiv_visit_q4' => $this->hiv_visit_q4,
            'hiv_visit_q5' => $this->hiv_visit_q5,
            'hiv_visit_q6a' => $this->hiv_visit_q6a,
            'hiv_visit_q6' => $this->hiv_visit_q6,
            'hiv_visit_q7a' => $this->hiv_visit_q7a,
            'hiv_visit_q7' => $this->hiv_visit_q7,
            'hiv_visit_q8a' => $this->hiv_visit_q8a,
            'hiv_visit_q8' => $this->hiv_visit_q8,
            'hiv_visit_q9' => $this->hiv_visit_q9,
            'hiv_visit_q10' => $this->hiv_visit_q10,
            'hiv_visit_q11' => $this->hiv_visit_q11,
            'create_time' => $this->create_time,
            'create_user' => $this->create_user,
            'update_time' => $this->update_time,
            'update_user' => $this->update_user,
        ]);

        $query->andFilterWhere(['like', 'hiv_visit_survey', $this->hiv_visit_survey])
            ->andFilterWhere(['like', 'hiv_visit_part_id', $this->hiv_visit_part_id])
            ->andFilterWhere(['like', 'status', $this->status]);

        $query->orderBy(["length(hiv_visit_part_id)" => SORT_ASC]);

        $query->limit($pagesize);

        return $dataProvider;
    }
}
