<?php

namespace app\models;

use Yii;
use yii\helpers\Html;
use rmrevin\yii\fontawesome\FA;
use yii\behaviors\TimestampBehavior;
use app\base\Converter;


/**
 * This is the model class for table "v_visitstatus".
 *
 * @property string $participant_id
 * @property integer $demographics_part_id
 * @property integer $quality_of_life_part_id
 * @property integer $hiv_continuum_part_id
 * @property integer $pre_art_beliefs_part_id
 * @property integer $post_art_beliefs_part_id
 * @property integer $substance_use_part_id
 * @property integer $risk_behavior_part_id
 * @property integer $depression_part_id
 * @property integer $health_care_utilization_part_id
 * @property integer $hiv_visit_part_id
 * @property integer $cd4_test_part_id
 */
class Visitschedulereport extends \yii\db\ActiveRecord
{
	/**
	 * @inheritdoc
	 */
    public static function tableName()
	{
		return 'v_visitschedule_report_allpidvisits';
	}

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

	/**
	 * @inheritdoc
	 */
	public function rules()
	{
		return [
			[['participant_id'], 'required'],
           	[['date_of_screening', 'Baseline_actual_date', 'Baseline_visit_date', 'Baseline_window_start', 'Baseline_window_end', 'Baseline_status', 'CF1_actual_date', 'CF1_visit_date', 'CF1_window_start', 'CF1_window_end', 'CF1_status', 'CF2_actual_date', 'CF2_visit_date', 'CF2_window_start', 'CF2_window_end', 'CF2_status', 'CF3_actual_date', 'CF3_visit_date', 'CF3_window_start', 'CF3_window_end', 'CF3_status', 'CF4_actual_date', 'CF4_visit_date', 'CF4_window_start', 'CF4_window_end', 'CF4_status'], 'integer'],
			[['participant_id'], 'string', 'max' => 11],
		];
	}

	/**
	 * @inheritdoc
	 */
	public function attributeLabels()
	{
		return [
			'participant_id' => Yii::t('app', 'Client ID'),			
			
		];
	}
	
	public function getBaseline($pid, $type = "")
    {
    	if($type == "" ) {
	    	$VSModel = Visitschedule::find()->where(["participant_id" => $pid])->andWhere(["visit_code" => 1])->andWhere(["is not","visit_date",NULL])->one();
	    	if(!empty($VSModel)) {
		    	$WinStrt = Converter::toStore($VSModel->window_start);
		    	$WinEnd = Converter::toStore($VSModel->window_end);
		        $Model = Visitschedulereport::find()->where(["participant_id" => $pid])->andWhere(["is not","Baseline_visit_date",NULL ])->andWhere(["between", "Baseline_visit_date", $WinStrt, $WinEnd])->one();	       
			    				
		        if($Model !== null)			    		
		        	return FA::icon('check text-success');			 		        	        	
				    
			    else
					 $Model = Visitschedulereport::find()->where(["participant_id" => $pid])->andWhere(["is not","Baseline_visit_date",NULL ])->andWhere(["<", "Baseline_visit_date", $WinStrt])->one();
				 
				   if($Model !== null)
					   return FA::icon('warning text-warningsuccess');
				   else
                	  return FA::icon('warning text-warning');				
		    } 			   
	        else
	        	return FA::icon('close text-danger');
			
	    }		
	}
	
	public function getCF1($pid, $type = "")
    {
    	if($type == "" ) {
	    	$VSModel = Visitschedule::find()->where(["participant_id" => $pid])->andWhere(["visit_code" => -1])->andWhere(["is not","visit_date",NULL])->one();
	    	if(!empty($VSModel)) {
		    	$WinStrt = Converter::toStore($VSModel->window_start);
		    	$WinEnd = Converter::toStore($VSModel->window_end);
		        $Model = Visitschedulereport::find()->where(["participant_id" => $pid])->andWhere(["is not","CF1_visit_date",NULL ])->andWhere(["between", "CF1_visit_date", $WinStrt, $WinEnd])->one();	       
			    				
		        if($Model !== null)			    		
		        	return FA::icon('check text-success');			 		        	        	
				    
			    else
					 $Model = Visitschedulereport::find()->where(["participant_id" => $pid])->andWhere(["is not","CF1_visit_date",NULL ])->andWhere(["<", "CF1_visit_date", $WinStrt])->one();
				 
				   if($Model !== null)
					   return FA::icon('warning text-warningsuccess');
				   else
                	  return FA::icon('warning text-warning');				
		    } 			   
	        else
	        	return FA::icon('close text-danger');
			
	    }		
	}
	
	public function getCF2($pid, $type = "")
    {
    	if($type == "" ) {
	    	$VSModel = Visitschedule::find()->where(["participant_id" => $pid])->andWhere(["visit_code" => -2])->andWhere(["is not","visit_date",NULL])->one();
	    	if(!empty($VSModel)) {
		    	$WinStrt = Converter::toStore($VSModel->window_start);
		    	$WinEnd = Converter::toStore($VSModel->window_end);
		        $Model = Visitschedulereport::find()->where(["participant_id" => $pid])->andWhere(["is not","CF2_visit_date",NULL ])->andWhere(["between", "CF2_visit_date", $WinStrt, $WinEnd])->one();	       
			    				
		        if($Model !== null)			    		
		        	return FA::icon('check text-success');			 		        	        	
				    
			    else
					 $Model = Visitschedulereport::find()->where(["participant_id" => $pid])->andWhere(["is not","CF2_visit_date",NULL ])->andWhere(["<", "CF2_visit_date", $WinStrt])->one();
				 
				   if($Model !== null)
					   return FA::icon('warning text-warningsuccess');
				   else
                	  return FA::icon('warning text-warning');				
		    } 			   
	        else
	        	return FA::icon('close text-danger');
			
	    }	
    }		
		
	public function getCF3($pid, $type = "")
    {
    	if($type == "" ) {
	    	$VSModel = Visitschedule::find()->where(["participant_id" => $pid])->andWhere(["visit_code" => -3])->andWhere(["is not","visit_date",NULL])->one();
	    	if(!empty($VSModel)) {
		    	$WinStrt = Converter::toStore($VSModel->window_start);
		    	$WinEnd = Converter::toStore($VSModel->window_end);
		        $Model = Visitschedulereport::find()->where(["participant_id" => $pid])->andWhere(["is not","CF3_visit_date",NULL ])->andWhere(["between", "CF3_visit_date", $WinStrt, $WinEnd])->one();	       
			    				
		        if($Model !== null)			    		
		        	return FA::icon('check text-success');			 		        	        	
				    
			    else
					 $Model = Visitschedulereport::find()->where(["participant_id" => $pid])->andWhere(["is not","CF3_visit_date",NULL ])->andWhere(["<", "CF3_visit_date", $WinStrt])->one();
				 
				   if($Model !== null)
					   return FA::icon('warning text-warningsuccess');
				   else
                	  return FA::icon('warning text-warning');				
		    } 			   
	        else
	        	return FA::icon('close text-danger');
			
	    }		
	}
	public function getCF4($pid, $type = "")
    {
    	if($type == "" ) {
	    	$VSModel = Visitschedule::find()->where(["participant_id" => $pid])->andWhere(["visit_code" => -4])->andWhere(["is not","visit_date",NULL])->one();
	    	if(!empty($VSModel)) {
		    	$WinStrt = Converter::toStore($VSModel->window_start);
		    	$WinEnd = Converter::toStore($VSModel->window_end);
		        $Model = Visitschedulereport::find()->where(["participant_id" => $pid])->andWhere(["is not","CF4_visit_date",NULL ])->andWhere(["between", "CF4_visit_date", $WinStrt, $WinEnd])->one();	       
			    				
		        if($Model !== null)			    		
		        	return FA::icon('check text-success');			 		        	        	
				    
			    else
					 $Model = Visitschedulereport::find()->where(["participant_id" => $pid])->andWhere(["is not","CF4_visit_date",NULL ])->andWhere(["<", "CF4_visit_date", $WinStrt])->one();
				 
				   if($Model !== null)
					   return FA::icon('warning text-warningsuccess');
				   else
                	  return FA::icon('warning text-warning');				
		    } 			   
	        else
	        	return FA::icon('close text-danger');
			
	    }		
 
    
}

    
}
