<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use yii\data\SqlDataProvider;
use yii\data\ActiveDataProvider;

use app\base\Converter;
use app\models\Vaccination;
use app\models\Followupreport;
use app\models\Users;

/**
 * VisitscheduleController implements the CRUD actions for Clientreport model.
 */
class FollowupreportController 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 Followupreport();
        if ($model->load(Yii::$app->request->post()) || $model->load(Yii::$app->request->get())) {
			//$pid=model->pid;
            if(!$model->validate()) {
                return $this->render('index', ['model' => $model]);
            } else {
				$frmdate = Converter::toUnixTimeformat($model->frmdate);
                $todate = Converter::toUnixTimeformat($model->todate);	
				 
				 $sloc=Yii::$app->user->identity->signedin_loc;
		         $suser=Yii::$app->user->identity->users_name;
		
		         $Usermodel = Users::find()->where(['users_name' => $suser])->one();			   
		         $UserRole= $Usermodel->user_role;	
		
		         $sdistrict=Yii::$app->user->identity->district;
				
				/*
				if($model->staff !="")	{							 
		         if($UserRole == 1)					
				     $query = Vaccination::find()->where(["between","dose2_schedule_date",$frmdate,$todate])->andWhere(["staff_code" => $model->staff])->orderBy(["beneficiary_name" => SORT_ASC,"cowin_beneficiary_id" => SORT_ASC]);
				 elseif($UserRole == 2)	
					 $query = Vaccination::find()->where(["between","dose2_schedule_date",$frmdate,$todate])->andWhere(["staff_code" => $model->staff])->andWhere(["district" => $sdistrict])->orderBy(["beneficiary_name" => SORT_ASC,"cowin_beneficiary_id" => SORT_ASC]);
				 else
                 	 $query = Vaccination::find()->where(["between","dose2_schedule_date",$frmdate,$todate])->andWhere(["staff_code" => $model->staff])->andWhere(["block" => $sloc])->orderBy(["beneficiary_name" => SORT_ASC,"cowin_beneficiary_id" => SORT_ASC]); 			  
				}				
				else {
				 if($UserRole == 1)					
				     $query = Vaccination::find()->where(["between","dose2_schedule_date",$frmdate,$todate])->orderBy(["beneficiary_name" => SORT_ASC,"cowin_beneficiary_id" => SORT_ASC]);
				 elseif($UserRole == 2)	
					 $query = Vaccination::find()->where(["between","dose2_schedule_date",$frmdate,$todate])->andWhere(["district" => $sdistrict])->orderBy(["beneficiary_name" => SORT_ASC,"cowin_beneficiary_id" => SORT_ASC]);
				 else
                 	 $query = Vaccination::find()->where(["between","dose2_schedule_date",$frmdate,$todate])->andWhere(["block" => $sloc])->orderBy(["beneficiary_name" => SORT_ASC,"cowin_beneficiary_id" => SORT_ASC]);
				}
				*/
							
                $title = "Follow-up Report from ".date("d-m-Y", $frmdate)." to ".date("d-m-Y", $todate); 
			    $titlelbl = " ";
				
				$query = Vaccination::find()->where(["between","dose2_schedule_date",$frmdate,$todate]);
				
				 if($model->staff !="") {
					$query=$query->andWhere(["staff_code" => $model->staff]); 
					$titlelbl = " for Staff ".$model->staff;
				  }					
				 else {
					if($UserRole == 1) {
				      if($model->rep == 1)  	
						 $titlelbl = " for All Blocks";
				      else
				         $titlelbl = " for All Districts ";
					}					  
					 elseif($UserRole == 2)
   					    $titlelbl = " for All Blocks";
					 else 
						$titlelbl = " ";
				}
		
                if($UserRole == 1) {
				     if($model->rep == 1) 				 
					   $query=$query->andWhere(["district" => $sdistrict]);
				  }
				 elseif($UserRole == 2) 				      
				      $query=$query->andWhere(["district" => $sdistrict]);				 
				 else					  
                 	  $query=$query->andWhere(["block" => $sloc]);                   
                  
			    $title= $title.$titlelbl;    				
				 				
				
				$query=$query->orderBy(["beneficiary_name" => SORT_ASC,"cowin_beneficiary_id" => SORT_ASC]);
				
                $dataProvider = new ActiveDataProvider(['query' => $query]);
				
				$model->frmdate = Converter::toDisplay($model->frmdate);
                $model->todate = Converter::toDisplay($model->todate);
				$model->staff = $model->staff;
				$model->rep = $model->rep;
				
                return $this->render('index', ['model' => $model, 'dataProvider' => $dataProvider, 'title' => $title]);
            }
        }
        return $this->render('index', ['model' => $model]);
    }

  
}
