<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use app\base\Converter;
use yii\data\ActiveDataProvider;

use app\models\Ostvisitnew;
use app\models\Registration;
use app\models\Ostweekreport;
use app\models\Ostmaster;
use yii\data\SqlDataProvider;
use app\models\Repdateostweek;

/**
 * OstweekreportController implements the CRUD actions for Ostvisitreport model.
 */
class OstweekreportController 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 Ostweekreport();
		
		if ($model->load(Yii::$app->request->post()) || $model->load(Yii::$app->request->get())) {
            if (Yii::$app->request->isAjax) {
                Yii::$app->response->format = Response::FORMAT_JSON;
                return ActiveForm::validate($model);
            }
            
            if(!$model->validate()) {
                return $this->render('index', ['model' => $model]);
            } else {
				
			    $osttable = Ostvisitnew::tableName();
				// $osttable = Ostmaster::tableName();
                $regtable = Registration::tableName();
			
				//$frmdate = Converter::toUnixTimeformat($frmdate);
               // $todate = Converter::toUnixTimeformat($todate);
				
				$frmdate = "01-".$model->month;
                $todate = self::last_day_of_the_month($frmdate);
                $frmdate = Converter::toUnixTimeformat($frmdate);
                if(date('m-Y') == date('m-Y', $frmdate))
                    $todate = strtotime(date('Y/m/d'));
                else
                    $todate = Converter::toUnixTimeformat($todate);
				
				$Rmodel = Repdateostweek::find()->one();
			     if($Rmodel === null)
				 $Rmodel = new Repdateostweek();			  		
			
			     if($frmdate  != "") {
					$Rmodel->fromdate = $frmdate ;						
				}	
				
				if($todate  != "") {
					$Rmodel->todate = $todate ;						
				}				
				$Rmodel->save();
						
 
        $dataProvider = new SqlDataProvider([
           'sql' => 'Select noofdaysdesc,count(ost_visit_part_id) as Noofclients from v_ostvisit_noofdays_summary GROUP BY noofdaysdesc ORDER BY orderno',
           'params' => [],
        ]);
 
       // $dataProvider = new ActiveDataProvider(['query' =>$query,'pagination' => false]);   
	   // $frmdate = Converter::toDisplay($frmdate);
       // $todate = Converter::toDisplay($todate);
		
		 return $this->render('index', ['model' => $model, 'dataProvider' => $dataProvider]);
		
		}
	}  
     return $this->render('index', ['model' => $model]);  
    }
	
	function last_day_of_the_month($date = '')
    {
        $month  = date('m', strtotime($date));
        $year   = date('Y', strtotime($date));
        $result = strtotime("{$year}-{$month}-01");
        $result = strtotime('-1 second', strtotime('+1 month', $result));
        return Converter::toDisplay($result);
    }
		
      
}
