<?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\Labsamplerequest;
use app\models\Labtestwisereport;
use app\models\Registration;
use app\models\Linkidlinkage;
use app\models\Hivlinkage;
use app\models\Labmaster;
use app\models\Labresultsmaster;
use app\models\Ostmaster;


/**
 * LabtestwisereportController implements the CRUD actions for Patientcrosstabreport model.
 */
class LabtestwisereportController 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 Labtestwisereport();

        if ($model->load(Yii::$app->request->post()) || $model->load(Yii::$app->request->get())) {
                    
            if(!$model->validate()) {
                return $this->render('index', ['model' => $model]);
            } else {
                $CModel = new Labsamplerequest;
				// $services = array_merge($CModel->q1, $CModel->q2);
                //$services = $CModel->q2;				
							
               
              $labtable = Labsamplerequest::tableName();   
              $labmastertable = Labmaster::tableName(); 
              $labrestable = Labresultsmaster::tableName(); 			 
              $regtable = Registration::tableName();
              $linkidtable = Linkidlinkage::tableName();
              $hivlinktable = Hivlinkage::tableName(); 
			  $osttable=Ostmaster::tableName();
                            
              $model->frmdate = Converter::toUnixTimeformat($model->frmdate);
              $model->todate = Converter::toUnixTimeformat($model->todate);            
				
							
				$qry = "SELECT Distinct $labmastertable.lab_test_name,lab_smple_part_id,  
                    CASE WHEN $regtable.corfrm_q5 = 1 THEN 'Male' WHEN $regtable.corfrm_q5 = 2 THEN 'Female' WHEN $regtable.corfrm_q5 = 3 THEN 'Hijrah' END AS Gender, corfrm_q4,ost_master_q1,link_idlinkage_q1,link_idlinkage_q2,link_idlinkage_q3,hiv_linkage_q5,lab_smple_acc_no,$labtable.record_date AS date,IF($labmastertable.lab_spot_test, $labrestable.lab_result_name, $labtable.lab_smple_result) As result
                FROM $labtable
                    INNER JOIN $regtable ON ($regtable.corfrm_pid = $labtable.lab_smple_part_id)
					LEFT JOIN $linkidtable ON ($linkidtable.link_idlinkage_part_id = $labtable.lab_smple_part_id)
					LEFT JOIN $hivlinktable ON ($hivlinktable.hiv_linkage_part_id = $labtable.lab_smple_part_id)
					LEFT JOIN $labmastertable ON ($labmastertable.lab_test_code = $labtable.lab_smple_tst_id)
					LEFT JOIN $labrestable ON ($labrestable.lab_result_code = $labtable.lab_smple_result)	
                    LEFT JOIN $osttable ON ($labtable.lab_smple_part_id = $osttable.ost_master_part_id AND $osttable.ost_master_q3 is null)  					
                WHERE $labtable.lab_smple_tst_id= '".$model->labtest."' AND $labtable.record_date BETWEEN '".$model->frmdate."' AND '".$model->todate."' ORDER BY $labtable.record_date desc,$labtable.lab_smple_part_id";
				
                $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' => ['pagesize' => 10]
                ]);
                
                $model->frmdate = Converter::toDisplay($model->frmdate);
                $model->todate = Converter::toDisplay($model->todate);
               
                return $this->render('index', ['model' => $model, 'dataProvider' => $dataProvider]);
            }
        }
        return $this->render('index', ['model' => $model]);
    }

}
