<?php

namespace app\controllers;

use Yii;
use yii\web\Response;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\widgets\ActiveForm;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use yii\data\ArrayDataProvider;
use app\base\Converter;
use app\models\Misreport;

/**
 * MisreportController implements the CRUD actions for Patientcrosstabreport model.
 */
class MisreportController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['error'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }
			
	
		 
    /**
     * Lists all models.
     * @return mixed
     */
    public function actionIndex()
    {
        $model = new Misreport();

         if ($model->load(Yii::$app->request->post()) || $model->load(Yii::$app->request->get())) {	
            
           // if(!$model->validate()) {
           //     return $this->render('index', ['model' => $model]);
          //  } else {                                                       
              
			if($model->pid != "") {
				$qry = "Select distinct case when corfrm_q13=1 then 'Currently on Followup' when corfrm_q13=2 then 'Migrated' when corfrm_q13=3 then 'Dead' when corfrm_q13=4 then 'LTF' End as 'Client Status',v_attendance.participant_id As Vpid,corfrm_pid,v_participant_finger_print.participant_id,v_participant_finger_print.No_of_Fingers,link_idlinkage_q1,link_idlinkage_q6,corfrm_q1,corfrm_q2,corfrm_q4,corfrm_q5,corfrm_q10,corfrm_q11,cms_registration.record_date,cms_registration.update_time,hiv_linkage_q3,hiv_linkage_q4,hiv_linkage_q5,hiv_linkage_q6,ost_master_q1,ost_master_q4,ost_master_q5,ost_master_q6,ost_master_q2,No_of_Visits ,Last_Visit_Date,case when corfrm_q12=0 then 'Negative' when corfrm_q12=1 then 'Positive' when corfrm_q12=2 then 'Indeterminate' when corfrm_q12=3 then 'Unknown'
when corfrm_q12=9 then 'Positive**' End as 'ClientHIV Status',v_lab_hiv.No_of_time as No_Hiv,v_lab_hiv.Last_testdone_date as LHIVD,v_lab_hiv.lab_result_name as HivRes,
v_lab_hcv.No_of_time as No_Hcv,v_lab_hcv.Last_testdone_date as LHCVD,v_lab_hcv.lab_result_name as HcvRes,
v_lab_tb.No_of_time as No_TB,v_lab_tb.Last_testdone_date as LTBD,v_lab_tb.lab_result_name as TBRes
from cms_registration
Left Join v_attendance on cms_registration.corfrm_pid=v_attendance.participant_id 
Left Join v_participant_finger_print on cms_registration.corfrm_pid=v_participant_finger_print.participant_id 
Left Join cms_linkidlinkage on cms_registration.corfrm_pid=cms_linkidlinkage.link_idlinkage_part_id 
Left Join cms_hivlinkage on cms_registration.corfrm_pid=cms_hivlinkage.hiv_linkage_part_id 
Left Join cms_ostmaster on cms_registration.corfrm_pid=cms_ostmaster.ost_master_part_id and ost_master_q3 is null
Left Join v_lab_hiv on cms_registration.corfrm_pid=v_lab_hiv.lab_smple_part_id 
Left Join v_lab_hcv on cms_registration.corfrm_pid=v_lab_hcv.lab_smple_part_id 
Left Join v_lab_tb on cms_registration.corfrm_pid=v_lab_tb.lab_smple_part_id 
where cms_registration.corfrm_pid='".$model->pid."' ORDER BY cms_registration.corfrm_pid ";	
			}			
else
{
$qry = "Select distinct case when corfrm_q13=1 then 'Currently on Followup' when corfrm_q13=2 then 'Migrated' when corfrm_q13=3 then 'Dead' when corfrm_q13=4 then 'LTF' End as 'Client Status',v_attendance.participant_id As Vpid,corfrm_pid,v_participant_finger_print.participant_id,v_participant_finger_print.No_of_Fingers,link_idlinkage_q1,link_idlinkage_q6,corfrm_q1,corfrm_q2,corfrm_q4,corfrm_q5,corfrm_q10,corfrm_q11,cms_registration.record_date,cms_registration.update_time,hiv_linkage_q3,hiv_linkage_q4,hiv_linkage_q5,hiv_linkage_q6,ost_master_q1,ost_master_q4,ost_master_q5,ost_master_q6,ost_master_q2,No_of_Visits ,Last_Visit_Date,case when corfrm_q12=0 then 'Negative' when corfrm_q12=1 then 'Positive' when corfrm_q12=2 then 'Indeterminate' when corfrm_q12=3 then 'Unknown'
when corfrm_q12=9 then 'Positive**' End as 'ClientHIV Status',v_lab_hiv.No_of_time as No_Hiv,v_lab_hiv.Last_testdone_date as LHIVD,v_lab_hiv.lab_result_name as HivRes,
v_lab_hcv.No_of_time as No_Hcv,v_lab_hcv.Last_testdone_date as LHCVD,v_lab_hcv.lab_result_name as HcvRes,
v_lab_tb.No_of_time as No_TB,v_lab_tb.Last_testdone_date as LTBD,v_lab_tb.lab_result_name as TBRes
from cms_registration
Left Join v_attendance on cms_registration.corfrm_pid=v_attendance.participant_id 
Left Join v_participant_finger_print on cms_registration.corfrm_pid=v_participant_finger_print.participant_id 
Left Join cms_linkidlinkage on cms_registration.corfrm_pid=cms_linkidlinkage.link_idlinkage_part_id 
Left Join cms_hivlinkage on cms_registration.corfrm_pid=cms_hivlinkage.hiv_linkage_part_id 
Left Join cms_ostmaster on cms_registration.corfrm_pid=cms_ostmaster.ost_master_part_id and ost_master_q3 is null
Left Join v_lab_hiv on cms_registration.corfrm_pid=v_lab_hiv.lab_smple_part_id 
Left Join v_lab_hcv on cms_registration.corfrm_pid=v_lab_hcv.lab_smple_part_id 
Left Join v_lab_tb on cms_registration.corfrm_pid=v_lab_tb.lab_smple_part_id 
ORDER BY cms_registration.corfrm_pid ";	
	}			
	
				
                $QModel = Yii::$app->db->createCommand($qry)->queryAll();              
                $nmodel = [];
				$services=[];
                foreach ($QModel as $key => $value) {
                    foreach ($services as $skey => $svalue) {
                        $nservices[$svalue] = '-';
                    }                   
					
                    $xservices = [];
                    $index = 0;
                    foreach ($value as $key1 => $value1) {
                        $xservices[] = $value1;                        
                    }
                    
                    $nmodel[] = $xservices;
                   
                }

                $dataProvider = new ArrayDataProvider([
                    'allModels' => $nmodel,
                    'pagination' => false
                ]);
                
                              
                return $this->render('index', ['model' => $model, 'dataProvider' => $dataProvider]);
            }
     //   }
        return $this->render('index', ['model' => $model]);
    }

}
