<?php

namespace app\models;

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

/**
 * This is the model class for table "cms_baseline_preartbeliefs".
 *
 * The followings are the available columns in table 'cms_baseline_preartbeliefs':
 * @property integer $overdose_id
 * @property string $overdose_survey
 * @property string $overdose_part_id
 * @property integer $overdose_q1
 * @property integer $overdose_q2
 * @property integer $overdose_q3
 * @property integer $overdose_q4
 * @property integer $overdose_q5
 * @property integer $overdose_q6
 * @property integer $overdose_q7
 * @property integer $overdose_q8
 * @property string $status
 * @property integer $create_time
 * @property integer $create_user
 * @property integer $update_time
 * @property integer $update_user
 */
 
class Overdosefup extends \yii\db\ActiveRecord
{
	public $yes_no = [0 => "No", 1 => "Yes"];
	public $yes_no_dk = [0 => "No", 1 => "Yes", 997 => "Don't Know"];
	public $q14 = [1 => "Nothing happened", 2 => "A friend/family member helped you", 3 => "You went to the hospital", 4 => "You were referred to drug treatment",997 => "Don’t Know",998 => "Refused"];	
	Public $visit;
	
	/**
	 * @inheritdoc
	 */
    public static function tableName()
	{
		return '{{%overdose}}';
	}

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

	/**
	 * @inheritdoc
	 */
	public function rules()
	{
		return [			
			[['overdose_survey','overdose_loc', 'overdose_part_id', 'overdose_q1','overdose_q12','overdose_q15','visit_code'], 'required', 'message' => 'Cannot be blank.'],
			[['overdose_q1a'], 'string', 'max' => 50],
			[['overdose_q1a'], 'safe'],			
			[['overdose_q15','overdose_q16','create_time', 'create_user', 'update_time', 'update_user'], 'integer', 'message' => 'Must be an integer'],	
            [['overdose_q15','overdose_q15a','overdose_q16','overdose_q16a'], 'number', 'min' => 0, 'tooSmall' => 'Negative value not allowed'],
			[['overdose_q15','overdose_q15a','overdose_q16','overdose_q16a'], 'string', 'max' => 3],
			
			[['overdose_q2'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return $model->overdose_q1 == '1';
			}, 'whenClient' => "function (attribute, value) {
				return $('#overdosefup-overdose_q1').val() == '1';
			}"],		
			
		[['overdose_q3a','overdose_q4a','overdose_q5a','overdose_q6a','overdose_q7a','overdose_q8a','overdose_q9a','overdose_q9b','overdose_q9c','overdose_q9d','overdose_q9e','overdose_q10a','overdose_q10b','overdose_q10c','overdose_q10d','overdose_q10e','overdose_q10f','overdose_q10g','overdose_q10h','overdose_q10i','overdose_q11a','overdose_q11b','overdose_q11c','overdose_q11d','overdose_q11e','overdose_q11f','overdose_q11g','overdose_q11h','overdose_q11i','overdose_q11j'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ($model->overdose_q1 == '1'  &&  $model->overdose_q2 == '1');
			}, 'whenClient' => "function (attribute, value) {
				return ($('#overdosefup-overdose_q1').val() == '1'  && $('#overdosefup-overdose_q2').val() == '1');
		}"],				
      
	 
	  [['overdose_q3b'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ($model->overdose_q1 == '1'  &&  $model->overdose_q2 == '1' && $model->overdose_q3a == '1' );
			}, 'whenClient' => "function (attribute, value) {
				return ($('#overdosefup-overdose_q1').val() == '1'  && $('#overdosefup-overdose_q2').val() == '1' && $('#overdosefup-overdose_q3a').val() == '1');
      }"],	
	  
	  [['overdose_q4b'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ($model->overdose_q1 == '1'  &&  $model->overdose_q2 == '1' && $model->overdose_q4a == '1' );
			}, 'whenClient' => "function (attribute, value) {
				return ($('#overdosefup-overdose_q1').val() == '1'  && $('#overdosefup-overdose_q2').val() == '1' && $('#overdosefup-overdose_q4a').val() == '1');
      }"],	
	  
	  [['overdose_q5b'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ($model->overdose_q1 == '1'  &&  $model->overdose_q2 == '1' && $model->overdose_q5a == '1' );
			}, 'whenClient' => "function (attribute, value) {
				return ($('#overdosefup-overdose_q1').val() == '1'  && $('#overdosefup-overdose_q2').val() == '1' && $('#overdosefup-overdose_q5a').val() == '1');
      }"],	
	  
	  [['overdose_q6b'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ($model->overdose_q1 == '1'  &&  $model->overdose_q2 == '1' && $model->overdose_q6a == '1' );
			}, 'whenClient' => "function (attribute, value) {
				return ($('#overdosefup-overdose_q1').val() == '1'  && $('#overdosefup-overdose_q2').val() == '1' && $('#overdosefup-overdose_q6a').val() == '1');
      }"],	
	  
	  [['overdose_q7b'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ($model->overdose_q1 == '1'  &&  $model->overdose_q2 == '1' && $model->overdose_q7a == '1' );
			}, 'whenClient' => "function (attribute, value) {
				return ($('#overdosefup-overdose_q1').val() == '1'  && $('#overdosefup-overdose_q2').val() == '1' && $('#overdosefup-overdose_q7a').val() == '1');
      }"],	
	  
	  
	 [['overdose_q8b'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ($model->overdose_q1 == '1'  &&  $model->overdose_q2 == '1' && $model->overdose_q8a == '1' );
			}, 'whenClient' => "function (attribute, value) {
				return ($('#overdosefup-overdose_q1').val() == '1'  && $('#overdosefup-overdose_q2').val() == '1' && $('#overdosefup-overdose_q8a').val() == '1');
      }"],	

       [['overdose_q13a','overdose_q13b','overdose_q13c','overdose_q13d','overdose_q13e','overdose_q13f','overdose_q13g','overdose_q13h','overdose_q13i','overdose_q13j','overdose_q13k','overdose_q13l','overdose_q13m','overdose_q13n','overdose_q13o','overdose_q13p','overdose_q14'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ($model->overdose_q12 == '1' );
			}, 'whenClient' => "function (attribute, value) {
				return ($('#overdosefup-overdose_q12').val() == '1');
		}"],		 
      
	  [['overdose_q15a','overdose_q16'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return $model->overdose_q15 > 0;
			}, 'whenClient' => "function (attribute, value) {
				return $('#overdosefup-overdose_q15').val() > '0';
			}"],
       
	   [['overdose_q16a'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ( $model->overdose_q15 > 0 && $model->overdose_q16 > 0);
			}, 'whenClient' => "function (attribute, value) {
				return ( $('#overdosefup-overdose_q15').val() > '0' && $('#overdosefup-overdose_q16').val() > '0');
			}"],
      	 
	  
	  		
        [['overdose_q8a'],'compare',  'compareAttribute'=>'overdose_q2', 'operator'=>'>=', 'type'=>'number','skipOnEmpty'=>true, 'message' => 'All of Q3a-8a cannot be 0 (No). At least one of Q3a-8a must be 1 (Yes).', 'when' => function ($model) {
				return ($model->overdose_q1 == '1' && $model->overdose_q2 == '1' && $model->overdose_q3a == '0' &&  $model->overdose_q4a == '0' &&  $model->overdose_q5a == '0' &&  $model->overdose_q6a == '0' &&  $model->overdose_q7a == '0' &&  $model->overdose_q8a == '0' );
			}, 'whenClient' => "function (attribute, value) {
				return ($('#overdosefup-overdose_q1').val() == '1' &&  $('#overdosefup-overdose_q2').val() == '1' && $('#overdosefup-overdose_q3a').val() == '0' && $('#overdosefup-overdose_q4a').val() == '0' && $('#overdosefup-overdose_q5a').val() == '0' && $('#overdosefup-overdose_q6a').val() == '0' && $('#overdosefup-overdose_q7a').val() == '0' && $('#overdosefup-overdose_q8a').val() == '0' );
		}"],
		
		
		 [['overdose_q9e'],'compare',  'compareAttribute'=>'overdose_q2', 'operator'=>'>=', 'type'=>'number','skipOnEmpty'=>true, 'message' => 'All of Q9a-9e cannot be 0 (No). At least one of Q9a-9e must be 1 (Yes).', 'when' => function ($model) {
				return ($model->overdose_q1 == '1' && $model->overdose_q2 == '1' && $model->overdose_q9a == '0' &&  $model->overdose_q9b == '0' &&  $model->overdose_q9c == '0' &&  $model->overdose_q9d == '0' &&  $model->overdose_q9e == '0' );
			}, 'whenClient' => "function (attribute, value) {
				return ($('#overdosefup-overdose_q1').val() == '1' &&  $('#overdosefup-overdose_q2').val() == '1' && $('#overdosefup-overdose_q9a').val() == '0' && $('#overdosefup-overdose_q9b').val() == '0' && $('#overdosefup-overdose_q9c').val() == '0' && $('#overdosefup-overdose_q9d').val() == '0' && $('#overdosefup-overdose_q9e').val() == '0' );
		}"],	
		
		 [['overdose_q10i'],'compare',  'compareAttribute'=>'overdose_q2', 'operator'=>'>=', 'type'=>'number','skipOnEmpty'=>true, 'message' => 'All of Q10a-10i cannot be 0 (No). At least one of Q10a-10i must be 1 (Yes).', 'when' => function ($model) {
				return ($model->overdose_q1 == '1' && $model->overdose_q2 == '1' && $model->overdose_q10a == '0' &&  $model->overdose_q10b == '0' &&  $model->overdose_q10c == '0' &&  $model->overdose_q10d == '0' &&  $model->overdose_q10e == '0' &&  $model->overdose_q10f == '0' &&  $model->overdose_q10g == '0' &&  $model->overdose_q10h == '0' &&  $model->overdose_q10i == '0' );
			}, 'whenClient' => "function (attribute, value) {
				return ($('#overdosefup-overdose_q1').val() == '1' &&  $('#overdosefup-overdose_q2').val() == '1' && $('#overdosefup-overdose_q10a').val() == '0' && $('#overdosefup-overdose_q10b').val() == '0' && $('#overdosefup-overdose_q10c').val() == '0' && $('#overdosefup-overdose_q10d').val() == '0' && $('#overdosefup-overdose_q10e').val() == '0' && $('#overdosefup-overdose_q10f').val() == '0' &&  $('#overdosefup-overdose_q10g').val() == '0' && $('#overdosefup-overdose_q10h').val() == '0' &&  $('#overdosefup-overdose_q10i').val() == '0'  );
		}"],	
		
		 [['overdose_q11j'],'compare',  'compareAttribute'=>'overdose_q2', 'operator'=>'>=', 'type'=>'number','skipOnEmpty'=>true, 'message' => 'All of Q11a-11j cannot be 0 (No). At least one of Q11a-11j must be 1 (Yes).', 'when' => function ($model) {
				return ($model->overdose_q1 == '1' && $model->overdose_q2 == '1' && $model->overdose_q11a == '0' &&  $model->overdose_q11b == '0' &&  $model->overdose_q11c == '0' &&  $model->overdose_q11d == '0' &&  $model->overdose_q11e == '0' &&  $model->overdose_q11f == '0' &&  $model->overdose_q11g == '0'&&  $model->overdose_q11h == '0' &&  $model->overdose_q11i == '0' &&  $model->overdose_q11j == '0'  );
			}, 'whenClient' => "function (attribute, value) {
				return ($('#overdosefup-overdose_q1').val() == '1' &&  $('#overdosefup-overdose_q2').val() == '1' && $('#overdosefup-overdose_q11a').val() == '0' && $('#overdosefup-overdose_q11b').val() == '0' && $('#overdosefup-overdose_q11c').val() == '0' && $('#overdosefup-overdose_q11d').val() == '0' && $('#overdosefup-overdose_q11e').val() == '0' && $('#overdosefup-overdose_q11f').val() == '0' && $('#overdosefup-overdose_q11g').val() == '0' && $('#overdosefup-overdose_q11h').val() == '0' && $('#overdosefup-overdose_q11i').val() == '0' && $('#overdosefup-overdose_q11j').val() == '0' );
		}"],	
		
		 [['overdose_q13p'],'compare',  'compareAttribute'=>'overdose_q12', 'operator'=>'>=', 'type'=>'number','skipOnEmpty'=>true, 'message' => 'All of Q13a-13p cannot be 0 (No). At least one of Q13a-13p must be 1 (Yes).', 'when' => function ($model) {
				return ($model->overdose_q12 == '1' && $model->overdose_q13a == '0' &&  $model->overdose_q13b == '0' &&  $model->overdose_q13c == '0' &&  $model->overdose_q13d == '0' &&  $model->overdose_q13e == '0' &&  $model->overdose_q13f == '0' &&  $model->overdose_q13g == '0'&&  $model->overdose_q13h == '0' &&  $model->overdose_q13i == '0' &&  $model->overdose_q13j == '0' &&  $model->overdose_q13k == '0' &&  $model->overdose_q13l == '0' &&  $model->overdose_q13m == '0' &&  $model->overdose_q13n == '0' &&  $model->overdose_q13o == '0' && $model->overdose_q13p == '0' );
			}, 'whenClient' => "function (attribute, value) {
				return ($('#overdosefup-overdose_q12').val() == '1' && $('#overdosefup-overdose_q13a').val() == '0' && $('#overdosefup-overdose_q13b').val() == '0' && $('#overdosefup-overdose_q13c').val() == '0' && $('#overdosefup-overdose_q13d').val() == '0' && $('#overdosefup-overdose_q13e').val() == '0' && $('#overdosefup-overdose_q13f').val() == '0' && $('#overdosefup-overdose_q13g').val() == '0' && $('#overdosefup-overdose_q13h').val() == '0' && $('#overdosefup-overdose_q13i').val() == '0' && $('#overdosefup-overdose_q13j').val() == '0' && $('#overdosefup-overdose_q13k').val() == '0' && $('#overdosefup-overdose_q13l').val() == '0' && $('#overdosefup-overdose_q13m').val() == '0' && $('#overdosefup-overdose_q13n').val() == '0' && $('#overdosefup-overdose_q13o').val() == '0' && $('#overdosefup-overdose_q13p').val() == '0' );
		}"],	
		 
		    [['overdose_q15a'], 'compare', 'compareAttribute'=>'overdose_q15', 'operator'=>'<=','type'=>'number', 'skipOnEmpty'=>true,'message'=>'This value cannot be greater than Q15'],
			
			[['overdose_q16'], 'compare', 'compareAttribute'=>'overdose_q15', 'operator'=>'<=','type'=>'number', 'skipOnEmpty'=>true,'message'=>'This value cannot be greater than Q15'],			
				
			[['overdose_q16a'], 'compare', 'compareAttribute'=>'overdose_q16', 'operator'=>'<=','type'=>'number', 'skipOnEmpty'=>true,'message'=>'This value cannot be greater than Q16'],
			
			[['visit_code'], 'integer', 'message' => 'Must be an integer'],
			[['status'], 'string', 'max' => 1],
			[['record_date'], 'default', 'value' => strtotime(date('Y/m/d'))],
			
			];
	}

	
	
	/**
	 * @inheritdoc
	 */
	public function attributeLabels()
	{
		return [
			'overdose_id' => Yii::t('app', 'Substanceuse ID'),
			'overdose_survey' => Yii::t('app', 'Survey'),
			'overdose_loc' => Yii::t('app', 'Location'),
			'overdose_part_id' => Yii::t('app', 'ICC Client ID'),
			
'visit_code' => Yii::t('app', 'Visit'),
'overdose_q1' => Yii::t('app', '1. Before today, have you ever heard of Heroin no 4 (China White, no 4, spee, fentanyl)?'),
'overdose_q1a' => Yii::t('app', '1a. Can you tell me any other names by which Heroin no 4 is known in your region- other than China White, no 4, spee, fentanyl?'),
'overdose_q2' => Yii::t('app', '2. Have you ever used Heroin no 4 (China White, no 4, fentanyl)?'),
'overdose_q3a' => Yii::t('app', '3a. Have you ever used Heroin no 4 on its own by smoking?'),
'overdose_q3b' => Yii::t('app', '3b. Have you used Heroin no 4 on its own by smoking in the past 6 months?'),
'overdose_q4a' => Yii::t('app', '4a. Have you ever used Heroin no 4 on its own by snorting?'),
'overdose_q4b' => Yii::t('app', '4b. Have you used Heroin no 4 on its own by snorting in the past 6 months?'),
'overdose_q5a' => Yii::t('app', '5a. Have you ever used Heroin no 4 on its own by injection?'),
'overdose_q5b' => Yii::t('app', '5b. Have you used Heroin no 4 on its own by injection in the past 6 months?'),
'overdose_q6a' => Yii::t('app', '6a. Have you ever used Heroin no 4 with another drug by smoking?'),
'overdose_q6b' => Yii::t('app', '6b. Have you used Heroin no 4 with another drug by smoking in the past 6 months?'),
'overdose_q7a' => Yii::t('app', '7a. Have you ever used Heroin no 4 with another drug by snorting?'),
'overdose_q7b' => Yii::t('app', '7b. Have you used Heroin no 4 with another drug by snorting in the past 6 months?'),
'overdose_q8a' => Yii::t('app', '8a. Have you ever used Heroin no 4 with another drug by injection?'),
'overdose_q8b' => Yii::t('app', '8b. Have you used Heroin no 4 with another drug by injection in the past 6 months?'),
'overdose_q9a' => Yii::t('app', '9a. Pharmacy'),
'overdose_q9b' => Yii::t('app', 'b. Street / Dealer'),
'overdose_q9c' => Yii::t('app', 'c. Friend / Family'),
'overdose_q9d' => Yii::t('app', 'd. Don’t Know'),
'overdose_q9e' => Yii::t('app', 'e. Refused to answer '),
'overdose_q10a' => Yii::t('app', '10a. I wanted a better high'),
'overdose_q10b' => Yii::t('app', 'b. There was no other choice because I had no other drugs'),
'overdose_q10c' => Yii::t('app', 'c. I needed to treat my withdrawal symptoms'),
'overdose_q10d' => Yii::t('app', 'd. Drugs with Heroin no. 4 in them are cheaper '),
'overdose_q10e' => Yii::t('app', 'e. I didn’t know that there was a difference between heroin and heroin no. 4'),
'overdose_q10f' => Yii::t('app', 'f. I used for recreation / just to try'),
'overdose_q10g' => Yii::t('app', 'g. Other'),
'overdose_q10h' => Yii::t('app', 'h. Don’t know '),
'overdose_q10i' => Yii::t('app', 'i. Refused to answer '),
'overdose_q11a' => Yii::t('app', '11a. Dealer told me	'),
'overdose_q11b' => Yii::t('app', 'b. Someone else told me'),
'overdose_q11c' => Yii::t('app', 'c. Someone else overdosed/had a bad reaction'),
'overdose_q11d' => Yii::t('app', 'd. I had a good reaction/ better high'),
'overdose_q11e' => Yii::t('app', 'e. The drug looks different'),
'overdose_q11f' => Yii::t('app', 'f. The drug tasted different'),
'overdose_q11g' => Yii::t('app', 'g. The drug smelled different'),
'overdose_q11h' => Yii::t('app', 'h. Other'),
'overdose_q11i' => Yii::t('app', 'i. Don’t know '),
'overdose_q11j' => Yii::t('app', 'j. Refused to Answer'),
'overdose_q12' => Yii::t('app', '12. In the last 6 months, have you had a drug overdose?'),
'overdose_q13a' => Yii::t('app', '13a. Heroin (e.g., brown sugar, opium, smack)'),
'overdose_q13b' => Yii::t('app', 'b. Heroin 4, China white, no4, fentanyl'),
'overdose_q13c' => Yii::t('app', 'c. Marijuana (e.g., smoke ganja, use hash, THC)'),
'overdose_q13d' => Yii::t('app', 'd. Cocaine or crack'),
'overdose_q13e' => Yii::t('app', 'e. Stimulants (e.g., amphetamine, methamphetamine, YABA)'),
'overdose_q13f' => Yii::t('app', 'f. Hallucinogens (e.g., LSD, MDMA, ecstasy, X, Stamp)'),
'overdose_q13g' => Yii::t('app', 'g. Buprenorphine (e.g., norphine, ADDNOK, Tidi, Sublingual Tidigesic,)'),
'overdose_q13h' => Yii::t('app', 'h. Inhalants/ Solvents (e.g., petrol, glue, paint thinner)'),
'overdose_q13i' => Yii::t('app', 'i. Allergy medicine/ antihistamines (e.g., avil, phenargan)'),
'overdose_q13j' => Yii::t('app', 'j. Pain killers (e.g., proxyvon, spasmoproxyvon, SP, Demerol, Fortwin, Morphine, Paxmax, Pentazocine, Pethidine, Tramadol)'),
'overdose_q13k' => Yii::t('app', 'k. Sedatives, Tranquilizers, Anti-anxiety drugs (e.g., Diazepam, Nitrosin, Alprazolam, Benzodiazepine, Calmpose, Clonazepam Nitrazepam, Nitrovit, Paxam, Restyl)'),
'overdose_q13l' => Yii::t('app', 'l. Intoxicating tobacco (e.g., mawa, zarda)'),
'overdose_q13m' => Yii::t('app', 'm. Alcohol'),
'overdose_q13n' => Yii::t('app', 'n. Any other drugs'),
'overdose_q13o' => Yii::t('app', 'o. Don’t Know '),
'overdose_q13p' => Yii::t('app', 'p. Refused to Answer'),
'overdose_q14' => Yii::t('app', '14. How was your overdose treated?'),
'overdose_q15' => Yii::t('app', '15. In the last 6 months, how many people have you seen overdose?'),
'overdose_q15a' => Yii::t('app', '15a. In the last 6 months, how many of these people overdosed when they were using heroin no. 4/ fentanyl?'),
'overdose_q16' => Yii::t('app', '16. Of the {count} people that you have seen overdose, how many died?'),
'overdose_q16a' => Yii::t('app', '16a. In the last 6 months, how many of these people who died were using heroin no. 4/ fentanyl?'),

			'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 getAppidusite()
	{
	   $Amodel = Applicationsettings::find()->orderBy("create_time DESC")->one();
        if ($Amodel !== null) {		
          return $Amodel->app_idu;
		 	
        } 
	}
	
}