<?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\Registration;
use app\models\Registrationreport;

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

         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 Registration;
				// $services = array_merge($CModel->q1, $CModel->q2);
                //$services = $CModel->q2;				
							
               
              $Regtable = Registration::tableName();             
                            
              $model->frmdate = Converter::toUnixTimeformat($model->frmdate);
              $model->todate = Converter::toUnixTimeformat($model->todate);
              
				
				$qry = "SELECT Distinct corfrm_pid,iccplus_date as Reg_date,corfrm_q1,corfrm_q2,corfrm_q3,corfrm_q4,corfrm_q5,corfrm_q6,corfrm_q7,corfrm_q8,corfrm_q9,corfrm_q10,corfrm_q11,corfrm_q12,corfrm_q13,corfrm_q14,corfrm_q15,corfrm_q16,iccplus_date,icc_date,RDS1Verified,RDS2Verified,no_of_fingers,
					$Regtable.status,$Regtable.create_time,$Regtable.create_user,$Regtable.update_time,$Regtable.update_user,record_date  FROM $Regtable WHERE $Regtable.iccplus_date BETWEEN '".$model->frmdate."' AND '".$model->todate."' ORDER BY $Regtable.iccplus_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]);
    }

}
