<?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\Labsamplerequest;
use app\models\Counselorreport;

/**
 * LabpendingreportController implements the CRUD actions for Counselorreport model.
 */
class LabpendingreportController 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 Counselorreport();
        if ($model->load(Yii::$app->request->post()) || $model->load(Yii::$app->request->get())) {
            if(!$model->validate()) {
                return $this->render('index', ['model' => $model]);
            } else {
                
                $frmdate = Converter::toUnixTimeformat($model->frmdate);
                $todate = Converter::toUnixTimeformat($model->todate);
                
                $query = Labsamplerequest::find()->where(["IS", "lab_smple_resultdate", NULL])->andWhere(["OR", ["BETWEEN", "lab_smple_date", $frmdate, $todate], ["BETWEEN", "record_date", $frmdate, $todate]])->orderBy(["length(lab_smple_part_id)" => SORT_ASC, "lab_smple_part_id" => SORT_ASC, "record_date" => SORT_ASC]);
                $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 Counselorreport();

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

}
