<?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\Hivtesting;
use app\models\Hivtestreport;

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

       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 Hivtesting;
				// $services = array_merge($CModel->q1, $CModel->q2);
                //$services = $CModel->q2;	
				
							
               
                $hivtable = Hivtesting::tableName();             
                            
              $model->frmdate = Converter::toUnixTimeformat($model->frmdate);
              $model->todate = Converter::toUnixTimeformat($model->todate);
              
				
				$qry = "SELECT Distinct hiv_test_part_id,hiv_test_q1,IF(FIND_IN_SET(1,hiv_test_q2)>0,1,0) as hiv_test_q2a,IF(FIND_IN_SET(2,hiv_test_q2)>0,1,0) as hiv_test_q2b,
IF(FIND_IN_SET(3,hiv_test_q2)>0,1,0) as hiv_test_q2c,IF(FIND_IN_SET(4,hiv_test_q2)>0,1,0) as hiv_test_q2d,
IF(FIND_IN_SET(5,hiv_test_q2)>0,1,0) as hiv_test_q2e,IF(FIND_IN_SET(6,hiv_test_q2)>0,1,0) as hiv_test_q2f,
IF(FIND_IN_SET(7,hiv_test_q2)>0,1,0) as hiv_test_q2g,IF(FIND_IN_SET(8,hiv_test_q2)>0,1,0) as hiv_test_q2h,
IF(FIND_IN_SET(9,hiv_test_q2)>0,1,0) as hiv_test_q2i,hiv_test_q3,hiv_test_q4,hiv_test_q5,hiv_test_q6,hiv_test_q7,hiv_test_q8,hiv_test_q9,hiv_test_q10,hiv_test_q11,hiv_test_q12,hiv_test_q13,hiv_test_q14,hiv_test_q15,hiv_test_q16,hiv_test_q17,hiv_test_q18,hiv_test_q19,hiv_test_q20,hiv_test_q21,hiv_test_q22,
					$hivtable.status,$hivtable.create_time,$hivtable.create_user,$hivtable.update_time,$hivtable.update_user,$hivtable.record_date                    
                FROM $hivtable                    
                WHERE $hivtable.record_date BETWEEN '".$model->frmdate."' AND '".$model->todate."' ORDER BY $hivtable.record_date";
				
                $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]);
    }

}
