<?php

namespace app\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use app\base\Converter;

/**
 * This is the model class for table "cms_baseline_healthcareutilization".
 *
 * The followings are the available columns in table 'cms_baseline_healthcareutilization':
 * @property integer $health_care_utilization_id
 * @property string $health_care_utilization_survey
 * @property string $health_care_utilization_part_id
 * @property integer $health_care_utilization_q1
 * @property integer $health_care_utilization_q2
 * @property integer $health_care_utilization_q3
 * @property integer $health_care_utilization_q4
 * @property integer $health_care_utilization_q5
 * @property integer $health_care_utilization_q6
 * @property integer $health_care_utilization_q7
 * @property integer $health_care_utilization_q8
 * @property string $status
 * @property integer $create_time
 * @property integer $create_user
 * @property integer $update_time
 * @property integer $update_user
 */
 
class Healthcareutilization extends \yii\db\ActiveRecord
{
	public $yes_no = [0 => "No", 1 => "Yes"];
	public $yes_no_dk = [1 => "Yes",0 => "No", 997 => "Don't Know", 998 => "Refused"];
	public $no_yes_dk = [0 => "No", 1 => "Yes", 997 => "Don't Know", 998 => "Refused"];
	Public $visit;
	
	/**
	 * @inheritdoc
	 */
    public static function tableName()
	{
		return '{{%baseline_healthcareutilization}}';
	}

	/**
	 * @inheritdoc
	 */
	public function behaviors()
	{
		return [
			[
				'class' => TimestampBehavior::className(),
				'createdAtAttribute' => 'create_time',
				'updatedAtAttribute' => 'update_time',
				'value' => date('U'),
			]
		];
	}

	/**
	 * @inheritdoc
	 */
	public function rules()
	{
		return [
			
			[['health_care_utilization_survey','health_care_utilization_loc', 'health_care_utilization_part_id', 'health_care_utilization_q1a', 'health_care_utilization_q2a', 'health_care_utilization_q3a', 'health_care_utilization_q4a', 'health_care_utilization_q5a', 'health_care_utilization_q6a', 'health_care_utilization_q7', 'health_care_utilization_q8a', 'health_care_utilization_q9a', 'health_care_utilization_q10a', 'health_care_utilization_q11a', 'health_care_utilization_q12a', 'health_care_utilization_q13', 'health_care_utilization_q14','visit_code'], 'required', 'message' => 'Cannot be blank.'],
			
						
			[['health_care_utilization_q1b','health_care_utilization_q1c','health_care_utilization_q1d'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ($model->health_care_utilization_q1a != '0' && $model->health_care_utilization_q1a != '997' && $model->health_care_utilization_q1a != '998' ) ;
			}, 'whenClient' => "function (attribute, value) {
				return ($('#healthcareutilization-health_care_utilization_q1a').val() != '0' && $('#healthcareutilization-health_care_utilization_q1a').val() != '997' && $('#healthcareutilization-health_care_utilization_q1a').val() != '998' ) ;
        }"],	
		
        [['health_care_utilization_q2b','health_care_utilization_q2c','health_care_utilization_q2d'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ($model->health_care_utilization_q2a != '0' && $model->health_care_utilization_q2a != '997' && $model->health_care_utilization_q2a != '998' ) ;
			}, 'whenClient' => "function (attribute, value) {
				return ($('#healthcareutilization-health_care_utilization_q2a').val() != '0' && $('#healthcareutilization-health_care_utilization_q2a').val() != '997' && $('#healthcareutilization-health_care_utilization_q2a').val() != '998' ) ;
        }"],		
   [['health_care_utilization_q3b','health_care_utilization_q3c','health_care_utilization_q3d'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ($model->health_care_utilization_q3a != '0' && $model->health_care_utilization_q3a != '997' && $model->health_care_utilization_q3a != '998' ) ;
			}, 'whenClient' => "function (attribute, value) {
				return ($('#healthcareutilization-health_care_utilization_q3a').val() != '0' && $('#healthcareutilization-health_care_utilization_q3a').val() != '997' && $('#healthcareutilization-health_care_utilization_q3a').val() != '998' ) ;
        }"],		

[['health_care_utilization_q4b','health_care_utilization_q4c','health_care_utilization_q4d'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ($model->health_care_utilization_q4a != '0' && $model->health_care_utilization_q4a != '997' && $model->health_care_utilization_q4a != '998' ) ;
			}, 'whenClient' => "function (attribute, value) {
				return ($('#healthcareutilization-health_care_utilization_q4a').val() != '0' && $('#healthcareutilization-health_care_utilization_q4a').val() != '997' && $('#healthcareutilization-health_care_utilization_q4a').val() != '998' ) ;
        }"],	
[['health_care_utilization_q5b','health_care_utilization_q5c','health_care_utilization_q5d'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ($model->health_care_utilization_q5a != '0' && $model->health_care_utilization_q5a != '997' && $model->health_care_utilization_q5a != '998' ) ;
			}, 'whenClient' => "function (attribute, value) {
				return ($('#healthcareutilization-health_care_utilization_q5a').val() != '0' && $('#healthcareutilization-health_care_utilization_q5a').val() != '997' && $('#healthcareutilization-health_care_utilization_q5a').val() != '998' ) ;
        }"],		
[['health_care_utilization_q6b','health_care_utilization_q6c','health_care_utilization_q6d'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ($model->health_care_utilization_q6a != '0' && $model->health_care_utilization_q6a != '997' && $model->health_care_utilization_q6a != '998' ) ;
			}, 'whenClient' => "function (attribute, value) {
				return ($('#healthcareutilization-health_care_utilization_q6a').val() != '0' && $('#healthcareutilization-health_care_utilization_q6a').val() != '997' && $('#healthcareutilization-health_care_utilization_q6a').val() != '998' ) ;
        }"],		
		
		[['health_care_utilization_q8b'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return $model->health_care_utilization_q8a =='1'  ;
			}, 'whenClient' => "function (attribute, value) {
				return $('#healthcareutilization-health_care_utilization_q8a').val() == '1' ;
        }"],	
		
		[['health_care_utilization_q9b'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return $model->health_care_utilization_q9a == '1'  ;
			}, 'whenClient' => "function (attribute, value) {
				return $('#healthcareutilization-health_care_utilization_q9a').val() == '1' ;
        }"],	
		
		
		[['health_care_utilization_q10b'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return $model->health_care_utilization_q10a == '1'  ;
			}, 'whenClient' => "function (attribute, value) {
				return $('#healthcareutilization-health_care_utilization_q10a').val() == '1' ;
        }"],	
		
		[['health_care_utilization_q11b'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return $model->health_care_utilization_q11a == '1'  ;
			}, 'whenClient' => "function (attribute, value) {
				return $('#healthcareutilization-health_care_utilization_q11a').val() == '1' ;
        }"],	
		
			
		[['health_care_utilization_q12b'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return $model->health_care_utilization_q12a == '1'  ;
			}, 'whenClient' => "function (attribute, value) {
				return $('#healthcareutilization-health_care_utilization_q12a').val() == '1' ;
        }"],	
  		
		
  [['health_care_utilization_q15','health_care_utilization_q16','health_care_utilization_q17','health_care_utilization_q18'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return $model->health_care_utilization_q14 == '1'  ;
			}, 'whenClient' => "function (attribute, value) {
				return $('#healthcareutilization-health_care_utilization_q14').val() == '1' ;
        }"],	
		
 [['health_care_utilization_q16'],'compare',  'compareAttribute'=>'health_care_utilization_q14', 'operator'=>'>=','type'=>'number', 'skipOnEmpty'=>true, 'message' => 'Both Q15 and Q16 cannot be 0.', 'when' => function ($model) {
				return ($model->health_care_utilization_q15 == '0' &&  $model->health_care_utilization_q16 == '0' );
			}, 'whenClient' => "function (attribute, value) {
				return ($('#healthcareutilization-health_care_utilization_q15').val() == '0' && $('#healthcareutilization-health_care_utilization_q16').val() == '0');
}"],			
					
			[['health_care_utilization_q1a', 'health_care_utilization_q2a', 'health_care_utilization_q3a', 'health_care_utilization_q4a', 'health_care_utilization_q5a', 'health_care_utilization_q6a', 'health_care_utilization_q7', 'health_care_utilization_q8b', 'health_care_utilization_q9b', 'health_care_utilization_q10b', 'health_care_utilization_q11b', 'health_care_utilization_q12b','health_care_utilization_q13','health_care_utilization_q15','health_care_utilization_q16','health_care_utilization_q1c', 'health_care_utilization_q2c', 'health_care_utilization_q3c', 'health_care_utilization_q4c', 'health_care_utilization_q5c', 'health_care_utilization_q6c','health_care_utilization_q1d', 'health_care_utilization_q2d', 'health_care_utilization_q3d', 'health_care_utilization_q4d', 'health_care_utilization_q5d', 'health_care_utilization_q6d', 'health_care_utilization_q17','create_time', 'create_user', 'update_time', 'update_user'], 'integer', 'message' => 'Must be an integer'],		
			[['health_care_utilization_q1a', 'health_care_utilization_q2a', 'health_care_utilization_q3a', 'health_care_utilization_q4a', 'health_care_utilization_q5a', 'health_care_utilization_q6a', 'health_care_utilization_q7', 'health_care_utilization_q8b', 'health_care_utilization_q9b', 'health_care_utilization_q10b', 'health_care_utilization_q11b', 'health_care_utilization_q12b','health_care_utilization_q13','health_care_utilization_q15','health_care_utilization_q16'], 'string', 'max' => 3],
			
			[['health_care_utilization_q1b', 'health_care_utilization_q2b', 'health_care_utilization_q3b', 'health_care_utilization_q4b', 'health_care_utilization_q5b', 'health_care_utilization_q6b','health_care_utilization_q1c', 'health_care_utilization_q2c', 'health_care_utilization_q3c', 'health_care_utilization_q4c', 'health_care_utilization_q5c', 'health_care_utilization_q6c','health_care_utilization_q1d', 'health_care_utilization_q2d', 'health_care_utilization_q3d', 'health_care_utilization_q4d', 'health_care_utilization_q5d', 'health_care_utilization_q6d', 'health_care_utilization_q17','health_care_utilization_q18'], 'string', 'max' => 4],
			
			[['health_care_utilization_q8b','health_care_utilization_q9b','health_care_utilization_q10b','health_care_utilization_q11b','health_care_utilization_q12b'],'number','min'=>1,'max'=>180,'tooSmall'=>'Minimum value is {min}','tooBig'=>'Maximum value is {max}'],
			
			[['health_care_utilization_q1a', 'health_care_utilization_q2a', 'health_care_utilization_q3a', 'health_care_utilization_q4a', 'health_care_utilization_q5a', 'health_care_utilization_q6a', 'health_care_utilization_q7', 'health_care_utilization_q13','health_care_utilization_q15','health_care_utilization_q16','health_care_utilization_q17','health_care_utilization_q18','health_care_utilization_q1c', 'health_care_utilization_q2c', 'health_care_utilization_q3c', 'health_care_utilization_q4c', 'health_care_utilization_q5c', 'health_care_utilization_q6c','health_care_utilization_q1d', 'health_care_utilization_q2d', 'health_care_utilization_q3d', 'health_care_utilization_q4d', 'health_care_utilization_q5d', 'health_care_utilization_q6d'], 'number', 'min' => 0, 'tooSmall' => 'Negative value not allowed'],
			
			[['health_care_utilization_q1b', 'health_care_utilization_q2b', 'health_care_utilization_q3b', 'health_care_utilization_q4b', 'health_care_utilization_q5b', 'health_care_utilization_q6b'], 'number','min'=>0.25,'max'=>24,'tooSmall'=>'Minimum value is {min}','tooBig'=>'Maximum value is {max}'],
			
			['health_care_utilization_q12a', 'safe'],
			['health_care_utilization_q12a', 'q812bvalidate'],
			[['visit_code'], 'integer', 'message' => 'Must be an integer'],
			[['status'], 'string', 'max' => 1],
			[['record_date'], 'default', 'value' => strtotime(date('Y/m/d'))],
			
			];
	}
public function q812bvalidate() {
		
	    	if(($this->health_care_utilization_q8b+$this->health_care_utilization_q9b+$this->health_care_utilization_q10b+$this->health_care_utilization_q11b+$this->health_care_utilization_q12b) >180) {
			$this->addError('health_care_utilization_q12a', 'The sum of values in 8b-12b should be ≤ 180.');
		}
	}


  
	 	    
	
	/**
	 * @inheritdoc
	 */
	public function attributeLabels()
	{
		return [
			'health_care_utilization_id' => Yii::t('app', 'Healthcareutilization ID'),
			'health_care_utilization_survey' => Yii::t('app', 'Survey'),
			'health_care_utilization_loc' => Yii::t('app', 'Location'),
			'health_care_utilization_part_id' => Yii::t('app', 'ICC Client ID'),
			
'visit_code' => Yii::t('app', 'Visit'),

'health_care_utilization_q1a' => Yii::t('app', '1 a. During the past 6 months, how many visits did you have with a doctor in a Clinic at a government hospital ?'),
'health_care_utilization_q1b' => Yii::t('app', 'b. For one typical visit with a doctor in a Clinic at a government hospital , what was the total number of hours you spent, including the visit and traveling to and from the clinic?'),
'health_care_utilization_q1c' => Yii::t('app', 'c. For one typical visit with a doctor in a Clinic at a government hospital , how many rupees did you pay, both for the doctor and any travel to the clinic?'),
'health_care_utilization_q1d' => Yii::t('app', 'd. For one typical visit with a doctor in Clinic at a government hospital , how many rupees did you lose from not working because you went to the clinic?'),
'health_care_utilization_q2a' => Yii::t('app', '2 a. During the past 6 months, how many visits did you have with a doctor in a Clinic at a charity hospital?'),
'health_care_utilization_q2b' => Yii::t('app', 'b. For one typical visit with a doctor in a Clinic at a charity hospital, what was the total number of hours you spent, including the visit and traveling to and from the clinic?'),
'health_care_utilization_q2c' => Yii::t('app', 'c. For one typical visit with a doctor in a Clinic at a charity hospital, how many rupees did you pay, both for the doctor and any travel to the clinic?'),
'health_care_utilization_q2d' => Yii::t('app', 'd. For one typical visit with a doctor in Clinic at a charity hospital, how many rupees did you lose from not working because you went to the clinic?'),
'health_care_utilization_q3a' => Yii::t('app', '3 a. During the past 6 months, how many visits did you have with a doctor in a Clinic at a private hospital?'),
'health_care_utilization_q3b' => Yii::t('app', 'b. For one typical visit with a doctor in a Clinic at a private hospital, what was the total number of hours you spent, including the visit and traveling to and from the clinic?'),
'health_care_utilization_q3c' => Yii::t('app', 'c. For one typical visit with a doctor in a Clinic at a private hospital, how many rupees did you pay, both for the doctor and any travel to the clinic?'),
'health_care_utilization_q3d' => Yii::t('app', 'd. For one typical visit with a doctor in Clinic at a private hospital, how many rupees did you lose from not working because you went to the clinic?'),
'health_care_utilization_q4a' => Yii::t('app', '4 a. During the past 6 months, how many visits did you have with a doctor in a Stand-alone clinic, not associated with a hospital?'),
'health_care_utilization_q4b' => Yii::t('app', 'b. For one typical visit with a doctor in a Stand-alone clinic, not associated with a hospital, what was the total number of hours you spent, including the visit and traveling to and from the clinic?'),
'health_care_utilization_q4c' => Yii::t('app', 'c. For one typical visit with a doctor in a Stand-alone clinic, not associated with a hospital, how many rupees did you pay, both for the doctor and any travel to the clinic?'),
'health_care_utilization_q4d' => Yii::t('app', 'd. For one typical visit with a doctor in Stand-alone clinic, not associated with a hospital, how many rupees did you lose from not working because you went to the clinic?'),
'health_care_utilization_q5a' => Yii::t('app', '5 a. During the past 6 months, how many visits did you have with a doctor in a YRG ICC (integrated care center)?'),
'health_care_utilization_q5b' => Yii::t('app', 'b. For one typical visit with a doctor in a YRG ICC (integrated care center), what was the total number of hours you spent, including the visit and traveling to and from the clinic?'),
'health_care_utilization_q5c' => Yii::t('app', 'c. For one typical visit with a doctor in a YRG ICC (integrated care center), how many rupees did you pay, both for the doctor and any travel to the clinic?'),
'health_care_utilization_q5d' => Yii::t('app', 'd. For one typical visit with a doctor in YRG ICC (integrated care center), how many rupees did you lose from not working because you went to the clinic?'),
'health_care_utilization_q6a' => Yii::t('app', '6 a. During the past 6 months, how many visits did you have with a doctor in a Different type of clinic, not mentioned already?'),
'health_care_utilization_q6b' => Yii::t('app', 'b. For one typical visit with a doctor in a Different type of clinic, not mentioned already, what was the total number of hours you spent, including the visit and traveling to and from the clinic?'),
'health_care_utilization_q6c' => Yii::t('app', 'c. For one typical visit with a doctor in a Different type of clinic, not mentioned already, how many rupees did you pay, both for the doctor and any travel to the clinic?'),
'health_care_utilization_q6d' => Yii::t('app', 'd. For one typical visit with a doctor in Different type of clinic, not mentioned already, how many rupees did you lose from not working because you went to the clinic?'),
'health_care_utilization_q7' => Yii::t('app', '7. During the past 6 months, how many times did you go to an emergency ward in a hospital (e.g., for an injury, overdose, severe illness, etc.)?'), 
'health_care_utilization_q8a' => Yii::t('app', '8 a. In the past 6 months, did you stay for one or more nights in a Government hospital?'),
'health_care_utilization_q8b' => Yii::t('app', 'b. In the past 6 months, how many TOTAL DAYS did you stay in a Government hospital?'),
'health_care_utilization_q9a' => Yii::t('app', '9 a. In the past 6 months, did you stay for one or more nights in a Charity hospital?'),
'health_care_utilization_q9b' => Yii::t('app', 'b. In the past 6 months, how many TOTAL DAYS did you stay in a Charity hospital?'),
'health_care_utilization_q10a' => Yii::t('app', '10 a. In the past 6 months, did you stay for one or more nights in a Private hospital?'),
'health_care_utilization_q10b' => Yii::t('app', 'b. In the past 6 months, how many TOTAL DAYS did you stay in a Private hospital?'),
'health_care_utilization_q11a' => Yii::t('app', '11 a. In the past 6 months, did you stay for one or more nights in a Polyclinic?'),
'health_care_utilization_q11b' => Yii::t('app', 'b. In the past 6 months, how many TOTAL DAYS did you stay in a Polyclinic?'),
'health_care_utilization_q12a' => Yii::t('app', '12 a. In the past 6 months, did you stay for one or more nights in a Other type of hospital or overnight clinic?'),
'health_care_utilization_q12b' => Yii::t('app', 'b. In the past 6 months, how many TOTAL DAYS did you stay in a Other type of hospital or overnight clinic?'),
'health_care_utilization_q13' => Yii::t('app', '13. During the past 6 months, how many major surgeries did you have (where you were put to sleep/given anesthesia)?'),
'health_care_utilization_q14' => Yii::t('app', '14. Do you take any medicines regularly? By medicines, I mean both western (i.e. tablets/syrups purchased at medical stores) and eastern (e.g., homeopathic, Ayurveda, Siddha, unani etc.) medicines. By regularly, I mean every day or most days.'),
'health_care_utilization_q15' => Yii::t('app', '15. How many western medicines do you take regularly?'),
'health_care_utilization_q16' => Yii::t('app', '16. How many eastern medicines do you take regularly?'),
'health_care_utilization_q17' => Yii::t('app', '17. About how many rupees do you spend getting these medicines (both western and eastern) each month?'),
'health_care_utilization_q18' => Yii::t('app', '18. Not counting doctor visits, about how many hours do you spend getting these medicines each month?'),
			
			'status' => Yii::t('app', 'Status'),
			'create_time' => Yii::t('app', 'Create Time'),
			'create_user' => Yii::t('app', 'Create User'),
			'update_time' => Yii::t('app', 'Update Time'),
			'update_user' => Yii::t('app', 'Update User'),
			'record_date' => Yii::t('app', 'Record Date'),
			
		];
	}

	public function getVisits($id)
	{
		$Gmodel = \app\models\Visitmaster::find()->where(['NOT', ['visit_interval' => null]])->andWhere(["status" => 1])->andWhere(["visit_code" => $id])->one();
		if($Gmodel !== null) {			
				return $Gmodel->visit_name;
		} else
			return "-";
	}	
/*	
	public function afterSave($insert, $changedAttributes) {
	    parent::afterSave($insert, $changedAttributes);
	    if($this->record_date !==Null) {	    	
		    $visitdate = $this->record_date;			
			$VSModels = Visitschedule::find()->where(['participant_id' => $this->health_care_utilization_part_id,'visit_code'=> $this->visit_code])->one();
			if(!empty($VSModels)) {						
					$VSModels->visit_date = $visitdate;	
					$VSModels->actual_date = Converter::toStore($VSModels->actual_date);
					$VSModels->window_start = Converter::toStore($VSModels->window_start);
					$VSModels->window_end = Converter::toStore($VSModels->window_end);
					$VSModels->date_of_screening = Converter::toStore($VSModels->date_of_screening);
                    $VSModels->save(false);			
     			}
	    }
	}
*/
}