<?php

namespace app\models;

use Yii;
use yii\helpers\Html;
use yii\behaviors\TimestampBehavior;
use app\base\Converter;
use app\models\Registration;
use app\models\Screening;
use app\base\Common;

/**
 * This is the model class for table "{{%registration}}".
 *
 * @property integer $corfrm_id
 * @property string $corfrm_sid
 * @property string $corfrm_pid
 * @property string $corfrm_loc
 * @property string $corfrm_q1
 * @property string $corfrm_q2
 * @property integer $corfrm_q3
 * @property integer $corfrm_q4
 * @property integer $corfrm_q5
 * @property integer $corfrm_q6
 * @property integer $corfrm_q7
 * @property integer $corfrm_q8
 * @property integer $corfrm_q9
 * @property integer $corfrm_q10
 * @property string $corfrm_q11
 * @property integer $corfrm_q12
 * @property integer $corfrm_q13
 * @property string $corfrm_q14
 * @property string $corfrm_q15
 * @property string $corfrm_q16
 * @property integer $iccplus_date
 * @property integer $icc_date
 * @property string $status
 * @property integer $create_time
 * @property integer $create_user
 * @property integer $update_time
 * @property integer $update_user
 */
class Regngoupdate extends \yii\db\ActiveRecord
{
	public $yes_no = [0 => "No", 1 => "Yes"];
	public $yes_no_dk = [0 => "No", 1 => "Yes", 997 => "Don't Know", 998 => "Refused"];
	public $q5 = [1 => "Male", 2 => "Female", 3 => "Hijra"];
	public $q6 = [1 => 'Never Married', 2 => 'Currently Married', 3 => 'Widowed', 4 => 'Divorced', 5 => 'Separated', 998 => 'Refused'];
	public $q7 = [1 => 'No School', 2 => 'Primary School (Class 1 - 5)', 3 => 'High School (Class 6 - 12)', 4 => 'High School Graduate', 5 => 'College or beyond', 998 => 'Refused'];
	public $q8 = [1 => 'Unemployed', 2 => 'Daily wages', 3 => 'Weekly wages', 4 => 'Monthly wages', 998 => 'Refused'];
	public $q10 = [1 => 'Existing client of ICC', 2 => 'Existing client of NGO', 3 => 'Friend told me', 4 => 'Saw on the street and came in', 5 => 'Referred by private doctor / other lab / CBO', 6 => 'Outreach worker', 7 => 'Referral SMS', 8 => 'Participated in previous YRG survey (e.g., RDS II)', 9 => 'Other'];
	public $q12 = [0 => 'Negative', 1 => 'Positive', 2 => 'Indeterminate', 3 => 'Unknown', 9 => 'Positive**'];
	//public $q13 = [1 => 'Client not found', 2 => 'Client migrated out of area', 3 => 'Client died (Fill out Death Form)', 4 => 'Client found and indicates willingness to follow-up', 5 => 'Client found and indicates unwillingness to follow-up', 6 => 'Client in Jail'];
	public $q13 = [1 => 'Currently on Followup', 2 => 'Migrated', 4 => 'LTF', 3 => 'Dead'];
	public $q14 = [0 => 'Negative', 1 => 'Positive', 2 => 'Indeterminate', 3 => 'Unknown', 9 => 'HCV Positive**'];
	//public $q13 = [0 => 'Staff', 1 => 'Currently on Followup', 2 => 'Migrated', 3 => 'Dead', 4 => 'LTF'];
	public $ClientCount, $dispQ12, $dispQ14, $dispQ15, $dispQ16, $lastHIVTestDate;
	public $idusite;
	public $q12rep = [0 => 'Negative',  2 => 'Indeterminate', 3 => 'Unknown'];
	
	public $q11 = [1 => '', 2 => ''];
	
	//public $qtype = [''=>'','Cohort only' => 'Cohort only', 'Incentive only' => 'Incentive only', 'Cohort and Incentive' => 'Cohort and Incentive', 'ICC only' => 'ICC only', 'ICC+ Screening only' => 'ICC+ Screening only'];

	/**
	 * @inheritdoc
	*/ 
	 
    public function init()
	{
		$this->q11 = Common::getNgovalue();
	}
	
	
    public static function tableName()
	{
		return '{{%registration}}';
	}

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

	/**
	 * @inheritdoc
	 */
	//private $_type;
	
	public function rules()
	{
		return [
			[['corfrm_sid', 'corfrm_pid', 'corfrm_loc', 'corfrm_q1', 'corfrm_q2', 'corfrm_q3', 'corfrm_q4', 'corfrm_q5', 'corfrm_q6', 'corfrm_q7', 'corfrm_q8', 'corfrm_q9', 'corfrm_q10', 'corfrm_q12', 'corfrm_q13', 'corfrm_q14', 'corfrm_q15'], 'required', 'message' => 'Cannot be blank.'],
			[['corfrm_q2', 'corfrm_q3', 'corfrm_q4', 'corfrm_q5', 'corfrm_q6', 'corfrm_q7', 'corfrm_q8', 'corfrm_q9', 'corfrm_q10', 'corfrm_q12', 'corfrm_q13', 'create_time', 'create_user', 'update_time', 'update_user'], 'integer', 'message' => 'Must be an integer'],	

            [['corfrm_q11'], 'required', 'message' => 'Cannot be blank.', 'when' => function ($model) {
				return $model->corfrm_q10 == '2';
			}, 'whenClient' => "function (attribute, value) {
				return $('#regngoupdate-corfrm_q10').val() == '2';
			}"],
  			
			[['pidtype'], 'safe'],	
			[['status'], 'string', 'max' => 1],			
			[['record_date', 'iccplus_date'], 'default', 'value' => strtotime(date('Y/m/d'))],
		];
	}

	
	

	/**
	 * @inheritdoc
	 */
	public function attributeLabels()
	{
		return [
			'corfrm_id' => Yii::t('app', 'Corfrm ID'),
			'corfrm_sid' => Yii::t('app', 'Survey'),
			'corfrm_pid' => Yii::t('app', 'ICC Client ID'),
			'corfrm_loc' => Yii::t('app', 'Location'),
			'corfrm_q1' => Yii::t('app', '1. Client name'),
			'corfrm_q2' => Yii::t('app', '2. What is your mobile phone number'),
			'corfrm_q3' => Yii::t('app', '3. What is the pincode where you live or where you stay most often?'),
			'corfrm_q4' => Yii::t('app', '4. What is your age?'),
			'corfrm_q5' => Yii::t('app', '5. Gender?'),
			'corfrm_q6' => Yii::t('app', '6. What is your marital status?'),
			'corfrm_q7' => Yii::t('app', '7. What is your highest level of education?'),
			'corfrm_q8' => Yii::t('app', '8. What is your employment status / frequency of pay?'),
			'corfrm_q9' => Yii::t('app', '9. What is your personal monthly income?'),
			'corfrm_q10' => Yii::t('app', '10. How did you hear about this center?'),
			'corfrm_q11' => Yii::t('app', '11. NGO Name'),
			'corfrm_q12' => Yii::t('app', '12. Client HIV Status'),
			'corfrm_q13' => Yii::t('app', '13. Client Status'),
			'corfrm_q14' => Yii::t('app', '14. Client HCV Status'),
			'corfrm_q15' => Yii::t('app', '15. Does the client have a valid government ART book that matches their name?'),
			'corfrm_q16' => Yii::t('app', '16. Does the client have a valid HCV Positive Report?'),
			'iccplus_date' => Yii::t('app', 'ICC+ Registration Date'),
			'icc_date' => Yii::t('app', 'ICC Registration Date'),
			'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 getClientHIVStatus($fmt = "html")
	{
		$class = "default";
		if($this->corfrm_q12 == "0") {
			$class = "danger";
		} elseif($this->corfrm_q12 == "1") {
			$class = "success";
		} elseif($this->corfrm_q12 == "2") {
			$class = "warning";
		} elseif($this->corfrm_q12 == "9") {
			$class = "info";
		}
		if($this->corfrm_q12 !== "") {
			if($fmt == "html")
				return Html::tag('span', $this->q12[$this->corfrm_q12], ['class' => 'label label-'.$class]);
			else
				return $this->q12[$this->corfrm_q12];
		}
		else
			return $this->corfrm_q12;
	}
	
	public function getClientStatus()
	{
		
		return $this->q13[$this->corfrm_q13];
	}

	public function getNGO($id, $data = "")
	{
		$Gmodel = Registration::find()->where(["corfrm_q11" => $id])->one();
		if($Gmodel !== null) {
			 return $Gmodel->corfrm_q11;
		} else
			return "-";
	}
	
	public function getSurvey()
    {
        return $this->hasOne(\app\models\Surveymaster::className(), ['sur_code' => 'corfrm_sid']);
    }

    public function getLocations()
    {
        return $this->hasOne(\app\models\Locationmaster::className(), ['loc_code' => 'corfrm_loc']);
    }
	
	public function getPidtype()
	{		
	  $model = Screening::find()->where(["mem_scrn_part_id" => $this->corfrm_pid])->orderBy(['record_date' => SORT_DESC])->one();
	   if ($model !== null) {
	   if($model->mem_scrn_q25 == 1 && ($model->mem_scrn_q28 != 1 || $model->mem_scrn_q28 == Null))
	     $type="Cohort only";
	   elseif($model->mem_scrn_q28 == 1 && ($model->mem_scrn_q25 != 1 || $model->mem_scrn_q25 == Null))
	     $type="Incentive only";	   
	   elseif($model->mem_scrn_q28 == 1 && $model->mem_scrn_q25 == 1)
	    $type="Cohort and Incentive";
	   elseif(($model->mem_scrn_q25 != 1 || $model->mem_scrn_q25 == Null) && ($model->mem_scrn_q28 != 1 || $model->mem_scrn_q28 == Null))
	     $type="ICC+ Screening only";	  	
	 }
	 else	   
		$type="ICC only";
	 		 
	   return $type;
	}
}

