<?php

namespace app\models;

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

/**
 * This is the model class for table "{{%sti}}".
 *
 * @property integer $sti_test_id
 * @property string $sti_test_survey
 * @property string $sti_test_loc
 * @property string $sti_test_part_id
 * @property integer $sti_test_q1
 * @property string $sti_test_q2
 * @property string $sti_test_q3
 * @property integer $sti_test_q4
 * @property string $status
 * @property integer $create_time
 * @property integer $create_user
 * @property integer $update_time
 * @property integer $update_user
 */
class Sti extends \yii\db\ActiveRecord
{
	public $yes_no = [0 => "No", 1 => "Yes"];
	public $q1 = [0 => 'Negative', 1 => 'Positive', 2 => 'Indeterminate', 3 => 'Not Done'];
	public $q2 = [1 => 'Urethral Discharge', 2 => 'Cervical Discharge', 3 => 'Painful scrotal swelling', 4 => 'Vaginal discharge', 5 => 'Genital ulcer - non herpetic', 6 => 'Genital ulcer – herpetic', 7 => 'Inguinal bubo', 8 => 'Other diagnosis', 9 => 'No diagnosis made'];
	public $q3 = [1 => 'NACO Kit 1 (GREY) Azithromycin + Cefixime', 2 => 'NACO Kit 2 (GREEN) Secnidazole + Fluconazole', 3 => 'NACO Kit 3 (WHITE) Benzathine pencillin + Azithromycin', 4 => 'NACO Kit 4 (BLUE) Doxycycline + Azithromycin', 5 => 'NACO Kit 5 (RED) Acyclovir', 6 => 'NACO Kit 6 (YELLOW) Cefixime + Metronidazole + Doxycycline', 7 => 'NACO Kit 7 (BLACK) Azithromycin + Doxycycline', 8 => 'Other treatment given', 9 => 'No treatment given'];
	/**
	 * @inheritdoc
	 */
    public static function tableName()
	{
		return '{{%sti}}';
	}

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

	/**
	 * @inheritdoc
	 */
	public function rules()
	{
		return [
			[['sti_test_survey', 'sti_test_loc', 'sti_test_part_id', 'sti_test_q1', 'sti_test_q2', 'sti_test_q4'], 'required', 'message' => 'Cannot be blank.'],
			[['sti_test_q3'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				// return $model->hiv_test_q17 == 1;
			}, 'whenClient' => "function (attribute, value) {
				return !$('#sti-sti_test_q2-9').prop('checked');
			}"],
			[['sti_test_q1', 'sti_test_q4', 'create_time', 'create_user', 'update_time', 'update_user'], 'integer', 'message' => 'Must be an integer'],
			[['sti_test_q2', 'sti_test_q3'], 'safe'],
			[['sti_test_survey', 'sti_test_loc', 'sti_test_part_id'], 'string', 'max' => 11],
			[['status'], 'string', 'max' => 1],
			[['record_date'], 'default', 'value' => strtotime(date('Y/m/d'))],
		];
	}

	/**
	 * @inheritdoc
	 */
	public function attributeLabels()
	{
		return [
			'sti_test_id' => Yii::t('app', 'Sti Test ID'),
			'sti_test_survey' => Yii::t('app', 'Survey'),
			'sti_test_loc' => Yii::t('app', 'Location'),
			'sti_test_part_id' => Yii::t('app', 'ICC Client ID'),
			'sti_test_q1' => Yii::t('app', '1. Syphilis test requested'),
			'sti_test_q2' => Yii::t('app', '2. STI Diagnosis (SELECT ALL THAT APPLY)'),
			'sti_test_q3' => Yii::t('app', '3. STI treatment given? (SELECT ALL THAT APPLY)'),
			'sti_test_q4' => Yii::t('app', '4. Referred to STI department'),
			'sti_test_q5' => Yii::t('app', '5. 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 afterFind()
	{
		parent::afterFind();
		if(Yii::$app->controller->action->id != "index" && Yii::$app->controller->action->id != "view") {
			if($this->sti_test_q2 != "")
				$this->sti_test_q2 = explode(",", $this->sti_test_q2);
			if($this->sti_test_q3 != "")
				$this->sti_test_q3 = explode(",", $this->sti_test_q3);
		}
		
		if(Yii::$app->controller->action->id != "view") {
			if($this->record_date != "")
				$this->record_date = Converter::toDisplay($this->record_date);
		}
	}
	
/*	
	public function getLabrequestno()
	{		
	  $model = Labsamplerequest::find()->where(["lab_smple_part_id" => $this->sti_test_part_id,"record_date" => $this->record_date])->andWhere(["lab_smple_tst_id" => 5])->orderBy(['record_date' => SORT_DESC])->one();	  
	   if ($model !== null && $model->lab_smple_acc_no !== null) {  
		$requestno = $model->lab_smple_acc_no;	
	    return $requestno;	

		
	 }
	}
	
	public function getLabstiresult()
	{		
	  $model = Labsamplerequest::find()->where(["lab_smple_part_id" => $this->sti_test_part_id,"record_date" => $this->record_date])->andWhere(["lab_smple_tst_id" => 5])->orderBy(['record_date' => SORT_DESC])->one();	  
	   if ($model !== null && $model->lab_smple_acc_no !== null) {  
		$result = $model->lab_smple_result;	
	    return $result;	

		
	 }
	}
	*/
	
	
	public function getLabstiresult()
	{		
	  $model = Labsamplerequest::find()->where(["lab_smple_acc_no" => $this->sti_test_q5])->one();	  
	   if ($model !== null) {  
		$result = $model->lab_smple_result;	
	    return $result;	

		
	 }
	}
	
		
}
