<?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\Settings;
use app\models\Visitschedule;
use app\models\Cohortfollowupreport;

use kartik\mpdf\Pdf;

/**
 * VisitscheduleController implements the CRUD actions for Clientreport model.
 */
class CohortfollowupreportController 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 Cohortfollowupreport();
        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);
				
               			
				//$query = Visitschedule::find()->where(["between","actual_date",$frmdate,$todate])->andWhere(["<","visit_code",1])->orderBy(["actual_date" => SORT_ASC,"participant_id" => SORT_ASC]);
				
				if($model->period ==1)
				 $Searchdate="actual_date";
			    elseif($model->period ==2)
				 $Searchdate="window_start";
                else					
			   	 $Searchdate="window_end";	
				
				$query = Visitschedule::find()->where(["between",$Searchdate,$frmdate,$todate])->andWhere(["<","visit_code",1])->orderBy(["window_end" => SORT_ASC,"participant_id" => SORT_ASC]);
				 
                $dataProvider = new ActiveDataProvider(['query' => $query]);
				
				$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]);
    }

  
}
