<?php

namespace app\models;

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

/**
 * This is the model class for table "{{%hivtesting}}".
 *
 * @property integer $hiv_test_id
 * @property string $hiv_test_survey
 * @property string $hiv_test_loc
 * @property string $hiv_test_part_id
 * @property integer $hiv_test_q1
 * @property string $hiv_test_q2
 * @property integer $hiv_test_q3
 * @property integer $hiv_test_q4
 * @property integer $hiv_test_q5
 * @property integer $hiv_test_q6
 * @property integer $hiv_test_q7
 * @property integer $hiv_test_q8
 * @property integer $hiv_test_q9
 * @property integer $hiv_test_q10
 * @property integer $hiv_test_q11
 * @property integer $hiv_test_q12
 * @property integer $hiv_test_q13
 * @property integer $hiv_test_q14
 * @property integer $hiv_test_q15
 * @property integer $hiv_test_q16
 * @property integer $hiv_test_q17
 * @property integer $hiv_test_q18
 * @property integer $hiv_test_q19
 * @property integer $hiv_test_q20
 * @property integer $hiv_test_q21
 * @property string $hiv_test_q22
 * @property string $status
 * @property integer $create_time
 * @property integer $create_user
 * @property integer $update_time
 * @property integer $update_user
 * @property integer $record_date
 */
class Hivtesting extends \yii\db\ActiveRecord
{
	public $yes_no = [0 => "No", 1 => "Yes"];
	public $yes_no_dk = [0 => "No", 1 => "Yes", 997 => "Don't Know"];
	public $q2 = [1 => 'I wanted to know my status', 2 => 'I had a high risk behaviour', 3 => 'I was referred by someone who had not visited this ICC', 4 => 'I was referred by someone who had visited this ICC', 5 => 'I was at the ICC for something else and saw that HIV testing was available', 6 => 'I was referred by a friend', 7 => 'I was referred by an injection partner', 8 => 'I was referred by a sexual partner', 9 => 'Other'];
	public $q5 = [0 => 'Negative', 1 => 'Positive', 2 => 'Indeterminate', 997 => 'Don\'t Know'];

	/**
	 * @inheritdoc
	 */
    public static function tableName()
	{
		return '{{%hivtesting}}';
	}

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

	/**
	 * @inheritdoc
	 */
	public function rules()
	{
		return [
			[['hiv_test_survey', 'hiv_test_loc', 'hiv_test_part_id', 'hiv_test_q1', 'hiv_test_q2', 'hiv_test_q3', 'hiv_test_q21'], 'required', 'message' => 'Cannot be blank.'],
			// [['hiv_test_survey', 'hiv_test_part_id', 'hiv_test_q1', 'hiv_test_q2', 'hiv_test_q3', 'hiv_test_q5'], 'required'],
			[['hiv_test_q3', 'hiv_test_q5', 'hiv_test_q6', 'hiv_test_q7', 'hiv_test_q8', 'hiv_test_q9', 'hiv_test_q10', 'hiv_test_q11', 'hiv_test_q12', 'hiv_test_q13', 'hiv_test_q14', 'hiv_test_q15', 'hiv_test_q16', 'hiv_test_q17', 'hiv_test_q18', 'hiv_test_q19', 'hiv_test_q20', 'create_time', 'create_user', 'update_time', 'update_user'], 'integer', 'message' => 'Must be an integer'],
			[['hiv_test_q1', 'hiv_test_q2', 'hiv_test_q4', 'hiv_test_q19'], 'safe'],
			[['hiv_test_q4', 'hiv_test_q5'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ($model->hiv_test_q3 != 0 && $model->hiv_test_q3 != "");
			}, 'whenClient' => "function (attribute, value) {
				return ($('#hivtesting-hiv_test_q3').val() != '0' && $('#hivtesting-hiv_test_q3').val() != '');
			}"],
			[['hiv_test_q6'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return $model->hiv_test_q5 == 1;
			}, 'whenClient' => "function (attribute, value) {
				return $('#hivtesting-hiv_test_q5').val() == '1';
			}"],
			// [['hiv_test_q7', 'hiv_test_q8', 'hiv_test_q11', 'hiv_test_q15', 'hiv_test_q19', 'hiv_test_q20'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
			[['hiv_test_q7', 'hiv_test_q11', 'hiv_test_q15', 'hiv_test_q19', 'hiv_test_q20'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ($model->hiv_test_q5 != 1 || $model->hiv_test_q6 == 0);
			}, 'whenClient' => "function (attribute, value) {
				return ($('#hivtesting-hiv_test_q5').val() != '1' || $('#hivtesting-hiv_test_q6').val() == '0');
			}"],
			[['hiv_test_q8'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return $model->hiv_test_q7 == 1;
			}, 'whenClient' => "function (attribute, value) {
				return $('#hivtesting-hiv_test_q7').val() == '1';
			}"],
			[['hiv_test_q9', 'hiv_test_q10'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return $model->hiv_test_q8 == 1;
			}, 'whenClient' => "function (attribute, value) {
				return $('#hivtesting-hiv_test_q8').val() == '1';
			}"],
			[['hiv_test_q11', 'hiv_test_q15'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return $model->hiv_test_q5 != 1;
			}, 'whenClient' => "function (attribute, value) {
				return $('#hivtesting-hiv_test_q5').val() != '1';
			}"],
			[['hiv_test_q12'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return $model->hiv_test_q11 == 1;
			}, 'whenClient' => "function (attribute, value) {
				return $('#hivtesting-hiv_test_q11').val() == '1';
			}"],
			[['hiv_test_q13', 'hiv_test_q14'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return $model->hiv_test_q12 == 1;
			}, 'whenClient' => "function (attribute, value) {
				return $('#hivtesting-hiv_test_q12').val() == '1';
			}"],
			[['hiv_test_q16'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return $model->hiv_test_q15 == 1;
			}, 'whenClient' => "function (attribute, value) {
				return $('#hivtesting-hiv_test_q15').val() == '1';
			}"],
			[['hiv_test_q17'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return $model->hiv_test_q16 == 1;
			}, 'whenClient' => "function (attribute, value) {
				return $('#hivtesting-hiv_test_q16').val() == '1';
			}"],
			[['hiv_test_q18'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return $model->hiv_test_q17 == 1;
			}, 'whenClient' => "function (attribute, value) {
				return $('#hivtesting-hiv_test_q17').val() == '1';
			}"],
			// [['hiv_test_q22'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
			// 	return $model->hiv_test_q21 == 1;
			// }, 'whenClient' => "function (attribute, value) {
			// 	return $('#hivtesting-hiv_test_q21').val() == '1';
			// }"],
			[['hiv_test_survey', 'hiv_test_part_id'], 'string', 'max' => 11],
			//[['hiv_test_q9', 'hiv_test_q13', 'hiv_test_q14'], 'number', 'min' => 1, 'tooSmall' => 'Value cannot be 0', 'max' => 995, 'tooBig' => 'Maximum value is {max}'],
			//[['hiv_test_q18'], 'number', 'min' => 1, 'tooSmall' => 'Value cannot be 0'],
			[['hiv_test_q9', 'hiv_test_q13', 'hiv_test_q14'], 'number', 'max' => 995, 'tooBig' => 'Maximum value is {max}'],
			['hiv_test_q10', 'compare', 'compareAttribute' => 'hiv_test_q9', 'operator' => '<=', 'type' => 'number', 'message' => 'Value cannot be greater than Q9'],
			['hiv_test_q14', 'compare', 'compareAttribute' => 'hiv_test_q13', 'operator' => '<=', 'type' => 'number', 'message' => 'Value cannot be greater than Q13'],
			// ['hiv_test_q18', 'validateq18', 'skipOnEmpty' => false, 'skipOnError' => false],
			// ['hiv_test_q19', 'validateq18', 'skipOnEmpty' => false, 'skipOnError' => false],
			[['hiv_test_q3', 'hiv_test_q9', 'hiv_test_q10', 'hiv_test_q13', 'hiv_test_q14', 'hiv_test_q18'], 'string', 'max' => 3],
			[['status'], 'string', 'max' => 1],
			[['record_date'], 'default', 'value' => strtotime(date('Y/m/d'))],
		];
	}

	public function validateq18($attribute, $params) {
		// if ($this->attribute > 40) {
			$this->addError($attribute, 'Acceptable Range is 1 - 40');
		// }
	}

	/**
	 * @inheritdoc
	 */
	public function attributeLabels()
	{
		return [
			'hiv_test_id' => Yii::t('app', 'Hiv Test ID'),
			'hiv_test_survey' => Yii::t('app', 'Survey'),
			'hiv_test_loc' => Yii::t('app', 'Location'),
			'hiv_test_part_id' => Yii::t('app', 'ICC Client ID'),
			'hiv_test_q1' => Yii::t('app', '1. Date of HIV Counseling and Testing'),
			'hiv_test_q2' => Yii::t('app', '2. Why have you come for an HIV test? [SELECT ALL THAT APPLY]'),
			'hiv_test_q3' => Yii::t('app', '3. How many times have you been tested for HIV before?'),
			'hiv_test_q4' => Yii::t('app', '4. When was the last time you were tested for HIV?'),
			'hiv_test_q5' => Yii::t('app', '5. What was the result of your last HIV test?'),
			'hiv_test_q6' => Yii::t('app', '6. Does the client have a valid government ART book that matches their name?'),
			'hiv_test_q7' => Yii::t('app', '7. Have you ever had sex with a man?'),
			'hiv_test_q8' => Yii::t('app', '8. Have you had sex with a man in the past 6 months?'),
			'hiv_test_q9' => Yii::t('app', '9. With how many different men have you had sex in the past 6 months?'),
			'hiv_test_q10' => Yii::t('app', '10. Of these {count} men, how many did you have unprotected sex with in the past 6 months?'),
			'hiv_test_q11' => Yii::t('app', '11. Have you ever had sex with a woman?'),
			'hiv_test_q12' => Yii::t('app', '12. Have you had sex with a woman in the past 6 months?'),
			'hiv_test_q13' => Yii::t('app', '13. With how many different women have you had sex in the past 6 months?'),
			'hiv_test_q14' => Yii::t('app', '14. Of these women, how many did you have unprotected sex with in the past 6 months?'),
			'hiv_test_q15' => Yii::t('app', '15. Have you ever injected drugs?'),
			'hiv_test_q16' => Yii::t('app', '16. Have you injected drugs in the past 6 months?'),
			'hiv_test_q17' => Yii::t('app', '17. Have you shared needles or syringes with anyone in the past 6 months?'),
			'hiv_test_q18' => Yii::t('app', '18. With how many different people have you shared needles or syringes with in the past 6 months?'),
			'hiv_test_q19' => Yii::t('app', '19. In the past six months, have you had a sexually transmitted infection (e.g., gonorrhea, chlamydia, syphilis, herpes, anal/genital warts or HPV, viral hepatitis)?'),
			'hiv_test_q20' => Yii::t('app', '20. Have you had any recurrent episodes of fever or weight loss in the past 6 months?'),
			'hiv_test_q21' => Yii::t('app', '21. Was an HIV lab test requested at this visit?'),
			'hiv_test_q22' => Yii::t('app', '22. Lab test request Number'),
			'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'),
		];
	}

	public function beforeSave($insert)
	{
		// parent::beforeSave();
		if (parent::beforeSave($insert)) {
			$this->hiv_test_q1 = Converter::toStore($this->hiv_test_q1);
			if($this->hiv_test_q4 != "")
				$this->hiv_test_q4 = Converter::toStore($this->hiv_test_q4);
			if($this->hiv_test_q6 != "") {
				$RegModel = Registration::find()->where(["corfrm_pid" => $this->hiv_test_part_id])->one();
				if($RegModel !== null) {
					if($this->hiv_test_q6 == 1)
						$RegModel->corfrm_q12 = 9;
					else
						$RegModel->corfrm_q12 = 3;
					$RegModel->corfrm_q15 = $this->hiv_test_q6;
					$RegModel->save(false);
				}
			}
	        return true;
	    } else {
	        return false;
	    }
	}

	public function afterFind()
	{
		parent::afterFind();
		if(Yii::$app->controller->action->id != "view") {
			$this->hiv_test_q1 = Converter::toDisplay($this->hiv_test_q1);
			$this->hiv_test_q2 = explode(",", $this->hiv_test_q2);
			if($this->hiv_test_q4 != "")
				$this->hiv_test_q4 = Converter::toDisplay($this->hiv_test_q4);
		}
	}
}
