<?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\Nursevisit;
use app\models\Counselor;
use app\models\Servicereport;
use app\models\Ostvisit;
use app\models\Attandance;

/**
 * ServicereportController implements the CRUD actions for Patientcrosstabreport model.
 */
class ServicereportController 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 Arvmaster models.
     * @return mixed
     */
    public function actionIndex()
    {
        $model = new Servicereport();

        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 Attandance;
               				
		       $attntable = Attandance::tableName();
		       $counstable = Counselor::tableName();
               $nursetable = Nursevisit::tableName();
               $osttable=Ostvisit::tableName();
			 	            
                $model->frmdate = Converter::toUnixTimeformat($model->frmdate);
                $model->todate = Converter::toUnixTimeformat($model->todate);

                $qry = "SELECT Distinct participant_id,$attntable.record_date ,IF(isnull(counfrm_pid),'-','Yes'),IF(isnull(nursefrm_pid),'-','Yes'),IF(isnull(ost_visit_part_id),'-','Yes')
                FROM $attntable
                    LEFT JOIN $counstable ON ($attntable.participant_id = $counstable.counfrm_pid and $attntable.record_date = $counstable.record_date )
					LEFT JOIN $nursetable ON ($attntable.participant_id = $nursetable.nursefrm_pid and $attntable.record_date = $nursetable.record_date)
					LEFT JOIN $osttable ON ($attntable.participant_id =$osttable.ost_visit_part_id and $attntable.record_date = $osttable.record_date)
                WHERE ($attntable.record_date BETWEEN '".$model->frmdate."' AND '".$model->todate."') and counfrm_pid is null and nursefrm_pid is null and ost_visit_part_id is null ORDER BY $attntable.record_date,participant_id";
								
			
		/*
			$qry = "SELECT Distinct participant_id,$attntable.record_date ,IF(isnull(counfrm_pid),'-','Yes'),IF(isnull(nursefrm_pid),'-','Yes'),IF(isnull(ost_visit_part_id),'-','Yes')
                FROM $attntable
                    LEFT JOIN $counstable ON ($attntable.participant_id = $counstable.counfrm_pid and $counstable.record_date = '".$model->repdate."' )
					LEFT JOIN $nursetable ON ($attntable.participant_id = $nursetable.nursefrm_pid and $nursetable.record_date = '".$model->repdate."')
					LEFT JOIN $osttable ON ($attntable.participant_id =$osttable.ost_visit_part_id and $osttable.record_date = '".$model->repdate."')
                WHERE  ($attntable.record_date = '".$model->repdate."' and counfrm_pid is null and nursefrm_pid is null and ost_visit_part_id is null ORDER BY $attntable.record_date,participant_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]);
    }

}