<?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 "{{%tb}}".
 *
 * @property integer $tb_test_id
 * @property string $tb_test_survey
 * @property string $tb_test_loc
 * @property string $tb_test_part_id
 * @property integer $tb_test_q1
 * @property integer $tb_test_q2
 * @property integer $tb_test_q3
 * @property integer $tb_test_q4
 * @property integer $tb_test_q5
 * @property string $status
 * @property integer $create_time
 * @property integer $create_user
 * @property integer $update_time
 * @property integer $update_user
 */
class Tb extends \yii\db\ActiveRecord
{
	public $yes_no = [0 => "No", 1 => "Yes"];
	public $q5 = [1 => "Client provided sputum sample immediately", 2 => "Client asked to provide morning sputum", 3 => "Client referred to TB clinic", 4 => "Other", 5 => "No further action taken"];
	public $q8 = [1 => "Stand alone DOT Center", 2 => "Government Hospital", 3 => "Private doctor in stand alone clinic", 4 => "Private hospital", 5 => "NGO doctor",6 => "Other"];
	public $q10 = [1 => "Currently taking treatment", 2 => "Stopped taking treatment early", 3 => "Completed the entire treatment course"];
	public $q11 = [1 => "Cured", 2 => "Not cured", 3 => "Needs to take another treatment course"];
	
	/**
	 * @inheritdoc
	 */
    public static function tableName()
	{
		return '{{%tb}}';
	}

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

	/**
	 * @inheritdoc
	 */
	public function rules()
	{
		return [
			[['tb_test_survey', 'tb_test_loc', 'tb_test_part_id', 'tb_test_q1', 'tb_test_q2', 'tb_test_q3', 'tb_test_q4'], 'required', 'message' => 'Cannot be blank.'],
			[['tb_test_q1', 'tb_test_q2', 'tb_test_q3', 'tb_test_q4', 'tb_test_q5','tb_test_q7','tb_test_q8','tb_test_q9','tb_test_q10','tb_test_q11', 'create_time', 'create_user', 'update_time', 'update_user'], 'integer'],
			[['tb_test_q5'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ($model->tb_test_q1 == '1' || $model->tb_test_q2 == '1' || $model->tb_test_q3 == '1' || $model->tb_test_q4 == '1');
			}, 'whenClient' => "function (attribute, value) {
				return ($('#tb-tb_test_q1').val() == '1' || $('#tb-tb_test_q2').val() == '1' || $('#tb-tb_test_q3').val() == '1' || $('#tb-tb_test_q4').val() == '1');
			}"],
			
			[['tb_test_q7'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ($model->tb_test_q5 == '1' || $model->tb_test_q5 == '2' || $model->tb_test_q5 == '3' || $model->tb_test_q5 == '4');
			}, 'whenClient' => "function (attribute, value) {
				return ($('#tb-tb_test_q5').val() == '1' || $('#tb-tb_test_q5').val() == '2' || $('#tb-tb_test_q5').val() == '3' || $('#tb-tb_test_q5').val() == '4');
			}"],
			
			[['tb_test_q8','tb_test_q9'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ($model->tb_test_q7 == '1');
			}, 'whenClient' => "function (attribute, value) {
				return ($('#tb-tb_test_q7').val() == '1' );
			}"],
			
			[['tb_test_q10'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ($model->tb_test_q9 == '1');
			}, 'whenClient' => "function (attribute, value) {
				return ($('#tb-tb_test_q9').val() == '1' );
			}"],
			
			[['tb_test_q11'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return ($model->tb_test_q10 == '3');
			}, 'whenClient' => "function (attribute, value) {
				return ($('#tb-tb_test_q10').val() == '3' );
			}"],
			
			[['tb_test_survey', 'tb_test_loc', 'tb_test_part_id'], 'string', 'max' => 11],
			[['status'], 'string', 'max' => 1],
			[['record_date'], 'default', 'value' => strtotime(date('Y/m/d'))],
		];
	}

	/**
	 * @inheritdoc
	 */
	public function attributeLabels()
	{
		return [
			'tb_test_id' => Yii::t('app', 'Tb Test ID'),
			'tb_test_survey' => Yii::t('app', 'Survey'),
			'tb_test_loc' => Yii::t('app', 'Location'),
			'tb_test_part_id' => Yii::t('app', 'ICC Client ID'),
			'tb_test_q1' => Yii::t('app', '1. Do you have a cough that you have had for 2 weeks or more?'),
			'tb_test_q2' => Yii::t('app', '2. In the past 4 weeks, have you had a weight loss?'),
			'tb_test_q3' => Yii::t('app', '3. In the past 4 weeks, have you had night sweats?'),
			'tb_test_q4' => Yii::t('app', '4. In the past 4 weeks, have you had a fever?'),
			'tb_test_q5' => Yii::t('app', '5. What action was taken for the positive TB symptom screen?'),
			'tb_test_q6' => Yii::t('app', '6. Lab test request Number'),
			'tb_test_q7' => Yii::t('app', '7. Was the client linked to a treatment program?'),
			'tb_test_q8' => Yii::t('app', '8. What kind of program did the client get referred to?'),
			'tb_test_q9' => Yii::t('app', '9. Did the client start treatment?'),
			'tb_test_q10' => Yii::t('app', '10. What is the clients current treatment status?'),
			'tb_test_q11' => Yii::t('app', '11. What was the outcome of the treatment?'),
			'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 getLabrequestno()
	{		
	  $model = Labsamplerequest::find()->where(["lab_smple_part_id" => $this->tb_test_part_id,"record_date" => $this->record_date])->andWhere(["lab_smple_tst_id" => 4])->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 getLabtbresult()
	{		
	  $model = Labsamplerequest::find()->where(["lab_smple_part_id" => $this->tb_test_part_id,"record_date" => $this->record_date])->andWhere(["lab_smple_tst_id" => 4])->orderBy(['record_date' => SORT_DESC])->one();	  
	   if ($model !== null && $model->lab_smple_acc_no !== null) {  
		$result = $model->lab_smple_result;	
	    return $result;	

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

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