<?php

namespace app\models;

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

/**
 * This is the model class for table "cms_baseline_riskbehavior".
 *
 * The followings are the available columns in table 'cms_baseline_riskbehavior':
 * @property integer $risk_behavior_id
 * @property string $risk_behavior_survey
 * @property string $risk_behavior_part_id
 * @property integer $risk_behavior_q1
 * @property integer $risk_behavior_q2
 * @property integer $risk_behavior_q3
 * @property integer $risk_behavior_q4
 * @property integer $risk_behavior_q5
 * @property integer $risk_behavior_q6
 * @property integer $risk_behavior_q7
 * @property integer $risk_behavior_q8
 * @property string $status
 * @property integer $create_time
 * @property integer $create_user
 * @property integer $update_time
 * @property integer $update_user
 */
 
class Riskbehaviorfup extends \yii\db\ActiveRecord
{
	public $yes_no = [0 => "No", 1 => "Yes"];
	public $yes_no_dk = [0 => "No", 1 => "Yes", 997 => "Don't Know", 998 => "Refused"];
	public $q2 = [1 => "Within the past month", 2 => "More than 1 month to 6 months ago", 3 => "More than 6 months to 1 year ago"];
	public $often = [0 => "Never",1 => "Sometimes", 2 => "Always", 997 => "Don’t know", 998 => "Refused"];
	public $psex;
	Public $visit;
	
	
	/**
	 * @inheritdoc
	 */
    public static function tableName()
	{
		return '{{%baseline_riskbehavior}}';
	}

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

	/**
	 * @inheritdoc
	 */
	public function rules()
	{
		return [
			
			[['risk_behavior_survey','risk_behavior_loc', 'risk_behavior_part_id', 'risk_behavior_q1','visit_code'], 'required', 'message' => 'Cannot be blank.'],
			[['risk_behavior_q3','risk_behavior_q4','risk_behavior_q9','risk_behavior_q10','risk_behavior_q12','risk_behavior_q15','risk_behavior_q16','risk_behavior_q17','risk_behavior_q18','risk_behavior_q20','risk_behavior_q23','risk_behavior_q24','risk_behavior_q25','risk_behavior_q26','risk_behavior_q28','create_time', 'create_user', 'update_time', 'update_user'], 'integer', 'message' => 'Must be an integer'],
			//['psex','safe'],
			
			[['risk_behavior_q3','risk_behavior_q4','risk_behavior_q9','risk_behavior_q17','risk_behavior_q25','risk_behavior_q10','risk_behavior_q12','risk_behavior_q15','risk_behavior_q18','risk_behavior_q20','risk_behavior_q23','risk_behavior_q26','risk_behavior_q28'], 'number', 'min' => 0, 'tooSmall' => 'Negative value not allowed'],
			
			[['risk_behavior_q3','risk_behavior_q4','risk_behavior_q9','risk_behavior_q17','risk_behavior_q25'], 'number', 'min' => 1, 'tooSmall' => 'This value cannot be 0.'],
			
			[['risk_behavior_q2'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return $model->risk_behavior_q1 == '1';
			}, 'whenClient' => "function (attribute, value) {
				return $('#riskbehaviorfup-risk_behavior_q1').val() == '1';
			}"],	
			
			[['risk_behavior_q3','risk_behavior_q4','risk_behavior_q5'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ($model->risk_behavior_q1 == '1' &&  $model->risk_behavior_q2 < '3' );
			}, 'whenClient' => "function (attribute, value) {
				return ($('#riskbehaviorfup-risk_behavior_q1').val() == '1' && $('#riskbehaviorfup-risk_behavior_q2').val() < '3' );
            }"],
			
			[['risk_behavior_q6'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return $model->psex == '1';
			}, 'whenClient' => "function (attribute, value) {
				return $('#riskbehaviorfup-psex').val() == '1';
			}"],
		
			[['risk_behavior_q9'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ($model->risk_behavior_q6 == '1' && $model->psex == '1');
			}, 'whenClient' => "function (attribute, value) {
				return ($('#riskbehaviorfup-risk_behavior_q6').val() == '1' && $('#riskbehaviorfup-psex').val() == '1') ;
            }"],
			
			[['risk_behavior_q10','risk_behavior_q12'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ($model->risk_behavior_q6 == '1' && $model->risk_behavior_q9 != '0' && $model->psex == '1');
			}, 'whenClient' => "function (attribute, value) {
				return ($('#riskbehaviorfup-risk_behavior_q6').val() == '1' && $('#riskbehaviorfup-risk_behavior_q9').val() != '0' && $('#riskbehaviorfup-psex').val() == '1');
            }"],
			
			[['risk_behavior_q11'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ($model->risk_behavior_q6 == '1' && $model->risk_behavior_q9 != '0' &&  $model->risk_behavior_q10 !='0' && $model->psex == '1' );
			}, 'whenClient' => "function (attribute, value) {
				return ($('#riskbehaviorfup-risk_behavior_q6').val() == '1' && $('#riskbehaviorfup-risk_behavior_q9').val() != '0' && $('#riskbehaviorfup-risk_behavior_q10').val() != '0' && $('#riskbehaviorfup-psex').val() == '1');
            }"],
			
			[['risk_behavior_q13'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ($model->risk_behavior_q6 == '1' && $model->risk_behavior_q9 != '0' &&  $model->risk_behavior_q12 != '0' && $model->psex == '1' );
			}, 'whenClient' => "function (attribute, value) {
				return ($('#riskbehaviorfup-risk_behavior_q6').val() == '1' && $('#riskbehaviorfup-risk_behavior_q9').val() != '0' && $('#riskbehaviorfup-risk_behavior_q12').val() != '0' && $('#riskbehaviorfup-psex').val() == '1');
            }"],
	  		
			
			
			[['risk_behavior_q14'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ( $model->psex == '2' );
			}, 'whenClient' => "function (attribute, value) {
				return ( $('#riskbehaviorfup-psex').val() == '2' );
            }"],
			
		
			[['risk_behavior_q15','risk_behavior_q16','risk_behavior_q17'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ( $model->risk_behavior_q14 == '1'  && $model->psex == '2');
			}, 'whenClient' => "function (attribute, value) {
				return ($('#riskbehaviorfup-risk_behavior_q14').val() == '1' && $('#riskbehaviorfup-psex').val() == '2' );
            }"],
			
			[['risk_behavior_q18','risk_behavior_q20'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ( $model->risk_behavior_q14 == '1' && $model->risk_behavior_q17 != '0'  && $model->psex == '2');
			}, 'whenClient' => "function (attribute, value) {
				return ( $('#riskbehaviorfup-risk_behavior_q14').val() == '1' && $('#riskbehaviorfup-risk_behavior_q17').val() != '0' && $('#riskbehaviorfup-psex').val() == '2');
            }"],
				
			[['risk_behavior_q19'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
							return ($model->risk_behavior_q14 == '1' && $model->risk_behavior_q17 != '0' &&  $model->risk_behavior_q18 != '0'  && $model->psex == '2');
			}, 'whenClient' => "function (attribute, value) {
				return ($('#riskbehaviorfup-risk_behavior_q14').val() == '1' && $('#riskbehaviorfup-risk_behavior_q17').val() != '0' && $('#riskbehaviorfup-risk_behavior_q18').val() != '0' && $('#riskbehaviorfup-psex').val() == '2' );
            }"],
			
			[['risk_behavior_q21'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
							return ( $model->risk_behavior_q14 == '1' && $model->risk_behavior_q17 != '0' &&  $model->risk_behavior_q20 != '0' && $model->psex == '2');
			}, 'whenClient' => "function (attribute, value) {
				return ( $('#riskbehaviorfup-risk_behavior_q14').val() == '1' && $('#riskbehaviorfup-risk_behavior_q17').val() != '0' && $('#riskbehaviorfup-risk_behavior_q20').val() != '0' && $('#riskbehaviorfup-psex').val() == '2');
            }"],
			
			
			[['risk_behavior_q22'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ($model->psex == '1' || $model->psex == '3');
			}, 'whenClient' => "function (attribute, value) {
				return($('#riskbehaviorfup-psex').val() == '1' || $('#riskbehaviorfup-psex').val() == '3');
			}"],
		
			
			[['risk_behavior_q23','risk_behavior_q24','risk_behavior_q25'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
							return ( $model->risk_behavior_q22 == '1' && ($model->psex == '1' || $model->psex == '3'));
			}, 'whenClient' => "function (attribute, value) {
				return ($('#riskbehaviorfup-risk_behavior_q22').val() == '1' && ($('#riskbehaviorfup-psex').val() == '1' || $('#riskbehaviorfup-psex').val() == '3'));
            }"],
			
			[['risk_behavior_q26','risk_behavior_q28'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
							return ( $model->risk_behavior_q22 == '1' &&  $model->risk_behavior_q25 != '0' && ($model->psex == '1' || $model->psex == '3') );
			}, 'whenClient' => "function (attribute, value) {
				return ( $('#riskbehaviorfup-risk_behavior_q22').val() == '1' && $('#riskbehaviorfup-risk_behavior_q25').val() != '0' && ($('#riskbehaviorfup-psex').val() == '1' || $('#riskbehaviorfup-psex').val() == '3'));
            }"],
			
			[['risk_behavior_q27'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
							return (  $model->risk_behavior_q22 == '1' &&  $model->risk_behavior_q25 != '0' && $model->risk_behavior_q26 != '0' && ($model->psex == '1' || $model->psex == '3') );
			}, 'whenClient' => "function (attribute, value) {
				return ( $('#riskbehaviorfup-risk_behavior_q22').val() == '1'  && $('#riskbehaviorfup-risk_behavior_q25').val() != '0' && $('#riskbehaviorfup-risk_behavior_q26').val() != '0' && ($('#riskbehaviorfup-psex').val() == '1' || $('#riskbehaviorfup-psex').val() == '3'));
            }"],
			
			[['risk_behavior_q29'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
							return (  $model->risk_behavior_q22 == '1' &&  $model->risk_behavior_q25 != '0' && $model->risk_behavior_q26 != '0' && $model->risk_behavior_q28 != '0' && ($model->psex == '1' || $model->psex == '3'));
			}, 'whenClient' => "function (attribute, value) {
				return ( $('#riskbehaviorfup-risk_behavior_q22').val() == '1' && $('#riskbehaviorfup-risk_behavior_q25').val() != '0' && $('#riskbehaviorfup-risk_behavior_q26').val() != '0' && $('#riskbehaviorfup-risk_behavior_q28').val() != '0' && ($('#riskbehaviorfup-psex').val() == '1' || $('#riskbehaviorfup-psex').val() == '3'));
            }"],
			
	
			[['risk_behavior_q3','risk_behavior_q4','risk_behavior_q10','risk_behavior_q9','risk_behavior_q12','risk_behavior_q14','risk_behavior_q17','risk_behavior_q18','risk_behavior_q20','risk_behavior_q25','risk_behavior_q26','risk_behavior_q28'], 'string', 'max' => 3],
			
			
			[['risk_behavior_q10','risk_behavior_q12'], 'compare', 'compareAttribute'=>'risk_behavior_q9', 'operator'=>'<=','type'=>'number', 'skipOnEmpty'=>true,'message'=>'This value must be less than or equal to the value of Q7'],
		
		   
			[['risk_behavior_q18','risk_behavior_q20'], 'compare', 'compareAttribute'=>'risk_behavior_q17', 'operator'=>'<=','type'=>'number', 'skipOnEmpty'=>true,'message'=>'This value must be less than or equal to the value of Q13'],
		 
		   
		     [['risk_behavior_q25'], 'compare', 'compareValue'=>995, 'operator'=>'>=','type'=>'number', 'skipOnEmpty'=>true,'message'=>'997 is not a valid answer','when' => function ($model) {
				return ($model->risk_behavior_q25 >= '995');
			}, 'whenClient' => "function (attribute, value) {
				return ($('#riskbehaviorfup-risk_behavior_q25').val() >= '995');
		    }"],
			
			[['risk_behavior_q26','risk_behavior_q28'], 'compare', 'compareAttribute'=>'risk_behavior_q25', 'operator'=>'<=','type'=>'number', 'skipOnEmpty'=>true,'message'=>'This value must be less than or equal to the value of Q19'],
		
		    ['risk_behavior_q25','safe'],
		    ['risk_behavior_q25','q25validation'],	
			[['risk_behavior_q23'], 'number', 'min' => 1, 'tooSmall' => '0 is not a valid answer'],
            [['visit_code'], 'integer', 'message' => 'Must be an integer'],			
			[['status'], 'string', 'max' => 1],
			[['record_date'], 'default', 'value' => strtotime(date('Y/m/d'))],
			
			];
	}

	public function q25validation($attribute, $params) {
        if ($this->risk_behavior_q25 != "") :
		if ($this->risk_behavior_q25 == 996)
		{
    	$this->addError($attribute, '996 is not a valid answer');
    	}
    	else if	($this->risk_behavior_q25 == 997)
		{
    	$this->addError($attribute, '997 is not a valid answer');
    	}
        endif;
	}

	/**
	 * @inheritdoc
	 */
	public function attributeLabels()
	{
		return [
			'risk_behavior_id' => Yii::t('app', 'Riskbehaviorfup ID'),
			'risk_behavior_survey' => Yii::t('app', 'Survey'),
			'risk_behavior_loc' => Yii::t('app', 'Location'),
			'risk_behavior_part_id' => Yii::t('app', 'ICC Client ID'),
			
'visit_code' => Yii::t('app', 'Visit'),
'risk_behavior_q1' => Yii::t('app', '1. In the last 6 months, have you ever shared a needle or syringe when injecting drugs? By “share” I mean you either used the needle/syringe after someone else used it or you passed it to someone else after you used it.'),
'risk_behavior_q2' => Yii::t('app', '2. When was the last time you shared a needle or syringe?'),
'risk_behavior_q3' => Yii::t('app', '3. In the last 6 months, with how many different PEOPLE did you share a needle or syringe?'),
'risk_behavior_q4' => Yii::t('app', '4. In the last 6 months, how many different TIMES did you share a needle or syringe with someone else?'),
'risk_behavior_q5' => Yii::t('app', '5. Think of the last time you injected drugs. Did you share a needle or syringe?'),
'risk_behavior_q6' => Yii::t('app', '6. In the last 6 months, have you ever had vaginal or anal sex with a woman (including wife)?'),

'risk_behavior_q9' => Yii::t('app', '7. In the last 6 months, with how many different women (including wife) have you had vaginal or anal sex?'),
'risk_behavior_q10' => Yii::t('app', '8. Of the {count} women with whom you’ve had sex in the past 6 months, how many do you consider to be regular partners (wife or girlfriend) that you have a commitment to?'),
'risk_behavior_q11' => Yii::t('app', '9. How often did you use a condom with these regular female partners?'),
'risk_behavior_q12' => Yii::t('app', '10. Of the {count} women with whom you’ve had sex in the past 6 months, how many do you consider to be casual or exchange partners? By “casual partner”, I mean a woman who you have sex with but do not feel committed to or don’t know very well. And by exchange partners, I mean a woman you have sex with in exchange for things like money or drugs.'),
'risk_behavior_q13' => Yii::t('app', '11. How often did you use a condom with these casual female partners?'),
'risk_behavior_q14' => Yii::t('app', '12. In the last 6 months, have you ever had vaginal or anal sex with a man (including husband)?'),

'risk_behavior_q17' => Yii::t('app', '13. In the last 6 months, with how many different men (including husband) have you had vaginal or anal sex?'),
'risk_behavior_q18' => Yii::t('app', '14. Of the {count} men with whom you’ve had sex in the past 6 months, how many do you consider to be regular partners (husband or boyfriend) that you have a commitment to?'),
'risk_behavior_q19' => Yii::t('app', '15. How often did you use a condom with these regular male partners?'),
'risk_behavior_q20' => Yii::t('app', '16. Of the {count} men with whom you’ve had sex in the past 6 months, how many do you consider to be casual or exchange partners? By “casual partner”, I mean a man who you have sex with but do not feel committed to or don’t know very well. And by exchange partners, I mean a man you have sex with in exchange for things like money or drugs.'),
'risk_behavior_q21' => Yii::t('app', '17. How often did you use a condom with these casual male partners?'),
'risk_behavior_q22' => Yii::t('app', '18. In the last 6 months, have you ever had oral or anal sex with a man or hijra?'),

'risk_behavior_q25' => Yii::t('app', '19. In the last 6 months, with how many different men or hijra have you had anal sex?'),
'risk_behavior_q26' => Yii::t('app', '20. Of the {count} men or hijra with whom you’ve had sex in the past 6 months, how many do you consider to be regular partners that you have a commitment to?'),
'risk_behavior_q27' => Yii::t('app', '21. How often did you use a condom with these regular male partners?'),
'risk_behavior_q28' => Yii::t('app', '22. Of the {count} men or hijra with whom you’ve had sex in the past 6 months, how many do you consider to be casual or exchange partners? By “casual partner”, I mean a man who you have sex with but do not feel committed to or don’t know very well. And by exchange partners, I mean a man you have sex with in exchange for things like money or drugs.'),
'risk_behavior_q29' => Yii::t('app', '23. How often did you use a condom with these casual male partners?'),
			
			'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 "-";
	}	
}