<?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\Ostmaster;
use app\models\Ostvisitnew;
use app\models\Linkidlinkage;
use app\models\Ostvisitreport;
use app\models\Attandance;


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

       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 Ostvisitnew;
				// $services = array_merge($CModel->q1, $CModel->q2);
                //$services = $CModel->q2;				
							
               
              $ostmastertable = Ostmaster::tableName();      
              $ostvisittable = Ostvisitnew::tableName(); 
	          $Linkidtable = Linkidlinkage::tableName();    	
              $attntable = Attandance::tableName();						  
              
			  $model->repdate = Converter::toUnixTimeformat($model->repdate);	
              
			   $qry = "SELECT Distinct $ostmastertable.ost_master_part_id,$ostmastertable.ost_master_q1,$Linkidtable.link_idlinkage_q1,visit_in,visit_out,$ostvisittable.ost_visit_q2,$ostvisittable.ost_visit_q3,$ostvisittable.ost_visit_q4,$ostvisittable.ost_visit_q5
                FROM $ostmastertable
                    LEFT JOIN $ostvisittable ON ($ostvisittable.ost_visit_part_id = $ostmastertable.ost_master_part_id and $ostvisittable.ost_visit_q1 = $ostmastertable.ost_master_q1 and $ostvisittable.record_date = '".$model->repdate."')
					LEFT JOIN  $Linkidtable ON ($Linkidtable.link_idlinkage_part_id	 = $ostmastertable.ost_master_part_id  )
					LEFT JOIN  $attntable ON ($attntable.participant_id = $ostmastertable.ost_master_part_id  and $attntable.record_date = '".$model->repdate."')
                WHERE $ostmastertable.ost_master_q3 is null  ORDER BY $ostvisittable.record_date desc,$ostmastertable.ost_master_part_id";
				
				/*
				$qry = "SELECT $ostmastertable.ost_master_part_id,$ostmastertable.ost_master_q1,$Linkidtable.link_idlinkage_q1,$ostvisittable.ost_visit_q2,$ostvisittable.ost_visit_q3,$ostvisittable.ost_visit_q4,$ostvisittable.ost_visit_q5
                FROM $ostmastertable
                    LEFT JOIN $ostvisittable ON ($ostvisittable.ost_visit_part_id = $ostmastertable.ost_master_part_id and $ostvisittable.ost_visit_q1 = $ostmastertable.ost_master_q1)
					LEFT JOIN  $Linkidtable ON ($Linkidtable.link_idlinkage_part_id	 = $ostmastertable.ost_master_part_id )
                WHERE $ostmastertable.ost_master_q3 is null and $ostvisittable.record_date = '".$model->repdate."' ORDER BY $ostvisittable.record_date,$ostmastertable.ost_master_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' => 20]
                ]);
                
           
			
             $model->repdate= Converter::toDisplay($model->repdate);			   
               
                return $this->render('index', ['model' => $model, 'dataProvider' => $dataProvider]);
            }
        }
        return $this->render('index', ['model' => $model]);
    }

}
