<?php

namespace app\models;

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

/**
 * RiskbehaviorSearch represents the model behind the search form about `app\models\Riskbehavior`.
 */
class RiskbehaviorSearch extends Riskbehavior
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['risk_behavior_id', 'risk_behavior_q1', 'risk_behavior_q2', 'risk_behavior_q3', 'risk_behavior_q4', 'risk_behavior_q5', 'risk_behavior_q6', 'create_time', 'create_user', 'update_time', 'update_user'], 'integer'],
            [['risk_behavior_survey', 'risk_behavior_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 = Riskbehavior::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([
            'risk_behavior_id' => $this->risk_behavior_id,
            'risk_behavior_q1' => $this->risk_behavior_q1,
			'risk_behavior_q2' => $this->risk_behavior_q2,
			'risk_behavior_q3' => $this->risk_behavior_q3,
            'risk_behavior_q4' => $this->risk_behavior_q4,
            'risk_behavior_q5' => $this->risk_behavior_q5,
            'risk_behavior_q6' => $this->risk_behavior_q6,
			'risk_behavior_q7' => $this->risk_behavior_q7,
            'risk_behavior_q8' => $this->risk_behavior_q8,
			'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', 'risk_behavior_survey', $this->risk_behavior_survey])
            ->andFilterWhere(['like', 'risk_behavior_part_id', $this->risk_behavior_part_id])
            ->andFilterWhere(['like', 'risk_behavior_q1', $this->risk_behavior_q1])
            ->andFilterWhere(['like', 'risk_behavior_q2', $this->risk_behavior_q2])            
            ->andFilterWhere(['like', 'status', $this->status]);

        return $dataProvider;
    }
}
