<?php

namespace app\models;

use Yii;
use app\models\Hivlinkage;
use app\models\Ostmaster;
use yii\behaviors\TimestampBehavior;
use codeonyii\yii2validators\AtLeastValidator;
use app\models\Applicationsettings;

/**
 * This is the model class for table "{{%nursevisit}}".
 *
 * @property integer $nursefrm_id
 * @property string $nursefrm_sid
 * @property string $nursefrm_loc
 * @property string $nursefrm_pid
 * @property string $nursefrm_q1
 * @property string $nursefrm_q2
 * @property string $status
 * @property integer $create_time
 * @property integer $create_user
 * @property integer $update_time
 * @property integer $update_user
 */
class Nursevisit extends \yii\db\ActiveRecord
{
	public $q1 = [1 => 'ART book checked', 2 => 'Referred to government center for doctor visit', 3 => 'Referred to government center for ART refill', 4 => 'Referred to counselor for HIV care counseling'];
	public $q2 = [2 => 'Needle / Syringe Pickup', 3 => 'Condoms', 4 => 'STI Testing', 5 => 'TB Screen', 6 => 'General health care check (BP/Diabetes)'];
	public $q2msm = [3 => 'Condoms', 4 => 'STI Testing', 5 => 'TB Screen', 6 => 'General health care check (BP/Diabetes)'];
	public $hdnq1, $hdnq2;
	public $idusite;

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

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

	/**
	 * @inheritdoc
	 */
	public function rules()
	{
		return [
			[['nursefrm_sid', 'nursefrm_loc', 'nursefrm_pid'], 'required', 'message' => 'Cannot be blank.'],
			[['nursefrm_q1', 'nursefrm_q2'], AtLeastValidator::className(), 'in' => ['hdnq1', 'hdnq2'], 'message' => 'Please select any one service.'],
			[['nursefrm_q3_no_needle','nursefrm_q4_no_condom','create_time', 'create_user', 'update_time', 'update_user'], 'integer', 'message' => 'Must be an integer'],
			[['nursefrm_sid', 'nursefrm_pid', 'nursefrm_q1', 'nursefrm_q2'], 'string', 'max' => 100],
			
			[['nursefrm_q3_no_needle'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {				
			}, 'whenClient' => "function (attribute, value) {
				return $('#nursevisit-nursefrm_q2-2').prop('checked');
			}"],
			
			[['nursefrm_q4_no_condom'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {				
			}, 'whenClient' => "function (attribute, value) {
				return $('#nursevisit-nursefrm_q2-3').prop('checked');
			}"],
			
			[['nursefrm_q1', 'nursefrm_q2', 'hdnq1', 'hdnq2'], 'safe'],
		    [['nursefrm_q3_no_needle','nursefrm_q4_no_condom'], 'string', 'max' => 3],
			[['nursefrm_q3_no_needle','nursefrm_q4_no_condom'], 'number', 'min' => 1, 'tooSmall' => 'Minimum value is {min}'],
			[['status'], 'string', 'max' => 1],
			[['nursefrm_pid', 'nursefrm_q1'], 'artValidate'],
			[['nursefrm_pid', 'nursefrm_q2'], 'ostValidate'],
			[['record_date'], 'default', 'value' => strtotime(date('Y/m/d'))],
			
		];
	}

	public function artValidate($attribute, $params) {
        if($this->nursefrm_pid != "") :
			$Hivlinkmaster = Hivlinkage::find()->where(["hiv_linkage_part_id" => $this->nursefrm_pid])->one();
	        if($this->nursefrm_q1 != ""):
		        if(!is_array($this->nursefrm_q1))
		            $this->nursefrm_q1 = explode(",", $this->nursefrm_q1);
		        if(is_array($this->nursefrm_q1) && $this->nursefrm_q1[0] == 1):
			        if($Hivlinkmaster === null)
			        	$this->addError($attribute, 'HIV linkage is not available for this Client ID');
			        elseif (($Hivlinkmaster->hiv_linkage_q3 == "" || $Hivlinkmaster->hiv_linkage_q3 === null) && ($Hivlinkmaster->hiv_linkage_q5 == "" || $Hivlinkmaster->hiv_linkage_q5 === null)) {
			        	$this->addError($attribute, 'ART Number is not available for this in HIV linkage');
			        }
			        else
			            $this->nursefrm_q1 = implode(",", $this->nursefrm_q1);
		        else:
		            $this->nursefrm_q1 = implode(",", $this->nursefrm_q1);
	        	endif;
        	endif;
        endif;
	}

	public function ostValidate($attribute, $params) {
        if($this->nursefrm_pid != "") :
			$Ostmaster = Ostmaster::find()->where(["ost_master_part_id" => $this->nursefrm_pid])->andWhere("ost_master_q3 IS NULL")->one();
	        if($this->nursefrm_q2 != ""):
		        if(!is_array($this->nursefrm_q2))
		            $this->nursefrm_q2 = explode(",", $this->nursefrm_q2);
		        if($Ostmaster === null && is_array($this->nursefrm_q2) && $this->nursefrm_q2[0] == 1)
		        	$this->addError($attribute, 'OST Master is not available for this Client ID');
		        else
		            $this->nursefrm_q2 = implode(",", $this->nursefrm_q2);
    	    endif;
        endif;
	}
	
	public function getAppidusite()
	{
	   $Amodel = Applicationsettings::find()->orderBy("create_time DESC")->one();
        if ($Amodel !== null) {		
          return $Amodel->app_idu;
			
        } 
	}

	/**
	 * @inheritdoc
	 */
	public function attributeLabels()
	{
		return [
			'nursefrm_id' => Yii::t('app', 'Nursefrm ID'),
			'nursefrm_sid' => Yii::t('app', 'Survey'),
			'nursefrm_loc' => Yii::t('app', 'Location'),
			'nursefrm_pid' => Yii::t('app', 'ICC Client ID'),
			'nursefrm_q1' => Yii::t('app', '1. HIV-related services provided [SELECT ALL THAT APPLY]'),
			'nursefrm_q2' => Yii::t('app', '2. Other services provided [SELECT ALL THAT APPLY]'),
			'nursefrm_q3_no_needle' => Yii::t('app', 'No of Needles given'),
			'nursefrm_q4_no_condom' => Yii::t('app', 'No of Condoms given'),	
			'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 == "update") {
			if($this->nursefrm_q1 != "")
                $this->nursefrm_q1 = explode(",", $this->nursefrm_q1);
            if($this->nursefrm_q2 != "")
                $this->nursefrm_q2 = explode(",", $this->nursefrm_q2);
		}
	}
}
