<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use app\models\Attandance;
use app\models\Registration;
use app\models\Labsamplerequest;
use app\models\Hivstatusreport;
use yii\data\SqlDataProvider;
use yii\data\ActiveDataProvider;
use app\base\Converter;

/**
 * HivstatusreportController implements the CRUD actions for Hivstatusreport model.
 */
class HivstatusreportController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['error'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['index', 'report'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }
			
	
		 
    /**
     * Lists all Arvmaster models.
     * @return mixed
     */
    public function actionIndex()
    {
        $model = new Hivstatusreport();
        if ($model->load(Yii::$app->request->post()) || $model->load(Yii::$app->request->get())) {
            if(!$model->validate()) {
                return $this->render('index', ['model' => $model]);
            } else {
                $query = Attandance::find()->innerJoinWith("registration")->where(["corfrm_q12" => $model->reptype])->orderBy(["length(pid)" => SORT_ASC, "pid" => SORT_ASC, "visit_date" => SORT_ASC]);
                    // $query = Registration::find()->where(["corfrm_q12" => $model->reptype])->orderBy(["length(corfrm_pid)" => SORT_ASC, "corfrm_pid" => SORT_ASC, "record_date" => SORT_ASC]);
                if($model->reptype != "pos") {
                    if($model->rngtype == "" || empty($model->rngtype))
                        $query = Registration::find()->joinWith('samplerequests')->where(["corfrm_q12" => $model->reptype])->orderBy(["length(corfrm_pid)" => SORT_ASC, "corfrm_pid" => SORT_ASC, "record_date" => SORT_ASC])->groupby(['corfrm_pid']);
                    elseif($model->rngtype == "1")
                        $query = Registration::find()->joinWith('cd4s')->where(["corfrm_q12" => $model->reptype])->orderBy(["length(corfrm_pid)" => SORT_ASC, "corfrm_pid" => SORT_ASC, "record_date" => SORT_ASC])->groupby(['corfrm_pid']);
                    elseif($model->rngtype == "2")
                        $query = Registration::find()->joinWith('hivcounselings')->where(["corfrm_q12" => $model->reptype])->orderBy(["length(corfrm_pid)" => SORT_ASC, "corfrm_pid" => SORT_ASC, "record_date" => SORT_ASC])->groupby(['corfrm_pid']);
                    elseif($model->rngtype == "3")
                        $query = Registration::find()->joinWith('hivvisits')->where(["corfrm_q12" => $model->reptype])->orderBy(["length(corfrm_pid)" => SORT_ASC, "corfrm_pid" => SORT_ASC, "record_date" => SORT_ASC])->groupby(['corfrm_pid']);
                } else {
                    if($model->rngtype == "" || empty($model->rngtype))
                        $query = Registration::find()->joinWith('samplerequests')->where(["corfrm_q12" => 1])->orWhere(["corfrm_q12" => 9])->orderBy(["length(corfrm_pid)" => SORT_ASC, "corfrm_pid" => SORT_ASC, "record_date" => SORT_ASC])->groupby(['corfrm_pid']);
                    elseif($model->rngtype == "1")
                        $query = Registration::find()->joinWith('cd4s')->where(["corfrm_q12" => 1])->orWhere(["corfrm_q12" => 9])->orderBy(["length(corfrm_pid)" => SORT_ASC, "corfrm_pid" => SORT_ASC, "record_date" => SORT_ASC])->groupby(['corfrm_pid']);
                    elseif($model->rngtype == "2")
                        $query = Registration::find()->joinWith('hivcounselings')->where(["corfrm_q12" => 1])->orWhere(["corfrm_q12" => 9])->orderBy(["length(corfrm_pid)" => SORT_ASC, "corfrm_pid" => SORT_ASC, "record_date" => SORT_ASC])->groupby(['corfrm_pid']);
                    elseif($model->rngtype == "3")
                        $query = Registration::find()->joinWith('hivvisits')->where(["corfrm_q12" => 1])->orWhere(["corfrm_q12" => 9])->orderBy(["length(corfrm_pid)" => SORT_ASC, "corfrm_pid" => SORT_ASC, "record_date" => SORT_ASC])->groupby(['corfrm_pid']);
                }
                $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => false]);
                if($model->frmdate != "" && $model->todate != "") {
                    $frmdate = Converter::toStore($model->frmdate);
                    $todate = Converter::toStore($model->todate);

                    if($model->rngtype == "" || empty($model->rngtype)) {
                        $frmdate = strtotime("-150 Days", $frmdate);
                        $todate = strtotime("-150 Days", $todate);
                        $dataProvider->query->andWhere(['between', Labsamplerequest::tableName().'.lab_smple_resultdate', $frmdate, $todate])->orderBy([Labsamplerequest::tableName().".lab_smple_resultdate" => SORT_ASC]);
                    } elseif($model->rngtype == "1") {
                        $frmdate = strtotime("-180 Days", $frmdate);
                        $todate = strtotime("-180 Days", $todate);
                        $dataProvider->query->andWhere(['between', \app\models\Cd4::tableName().'.cd4_test_q3', $frmdate, $todate])->orderBy([\app\models\Cd4::tableName().".cd4_test_q3" => SORT_ASC]);
                    } elseif($model->rngtype == "2") {
                        $frmdate = strtotime("-80 Days", $frmdate);
                        $todate = strtotime("-80 Days", $todate);
                        $dataProvider->query->andWhere(['between', \app\models\Hivcounseling::tableName().'.record_date', $frmdate, $todate])->orderBy([\app\models\Hivcounseling::tableName().".record_date" => SORT_ASC]);
                    } elseif($model->rngtype == "3") {
                        $query->andWhere(['IS NOT', \app\models\Hivvisit::tableName().'.hiv_visit_q3', NULL]);
                        $models = $query->all();
                        $Ids = [];
                        foreach ($models as $key => $value) {
                            $nextvisit = $value->getLastvisit("nextvisit");
                            if($nextvisit != "-") {
                                $nextvisit = Converter::toStore($nextvisit);
                                if($nextvisit >= $frmdate && $nextvisit <= $todate) {
                                    $Ids[] = $value->corfrm_id;
                                }
                            }
                        }
                        $query = Registration::find()->joinWith('hivvisits')->where(["IN", "corfrm_id", $Ids])->orderBy(["length(corfrm_pid)" => SORT_ASC, "corfrm_pid" => SORT_ASC, "record_date" => SORT_ASC])->groupby(['corfrm_pid']);
                        $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => false]);
                    }
                }
                return $this->render('index', ['model' => $model, 'dataProvider' => $dataProvider]);
            }
        }
        return $this->render('index', ['model' => $model]);
    }
    public function actionReport()
    {
        $model = new Hivstatusreport();

        if ($model->load(Yii::$app->request->post()) || $model->load(Yii::$app->request->get())) {
            if(!$model->validate()) {
                return $this->render('index', ['model' => $model]);
            } else {
                $query = Attandance::find()->innerJoinWith("registration")->where(["corfrm_q12" => $model->reptype]);
                $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => false]);
                // $dataProvider = Common::getIncentives($model->pid);
                // print_r($dataProvider->getModels());
                return $this->render('index', ['model' => $model, 'dataProvider' => $dataProvider]);
            }
        }
        return $this->render('index', ['model' => $model]);
    }

}
