<?php

namespace modules\custom\controllers;

use Yii;
use modules\custom\models\Investigationbe;
use modules\custom\models\Investigationbesearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * InvestigationController implements the CRUD actions for investigationbe model.
 */
class InvestigationController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::class,
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }
    public function beforeAction($action)
	{
       if (\Yii::$app->user->isGuest) {
            $this->redirect(['/site/login']);   	
	   }
		return true;
    } 

    /**
     * Lists all investigationbe models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new investigationbesearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
     /**
     * Lists Deleted investigationbe models.
     * @return mixed
     */
    public function actionDeleted()
    {
        $searchModel = new investigationbesearch();
        $dataProvider = $searchModel->deleted(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single investigationbe model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new investigationbe model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    
    public function actionCreate() {
        $model = new investigationbe();
		$employees = \yii\helpers\ArrayHelper::map(\backend\models\User::find()->where(['is', 'blocked_at', null])->orderby(['username' => SORT_ASC])->all(), 'id', 'fullname');
        $vehicles = \yii\helpers\ArrayHelper::map(\modules\cors\models\Vehiclebe::find()->where(['=', 'status', 'Active'])->orderby(['reg' => SORT_ASC])->all(), 'id', 'reg');
        
 		$tempid = Yii::$app->session->get('investstempid');
        if (!$tempid) {	
			$tempid = \common\models\Base::tempId();
        	Yii::$app->session->set('investstempid', $tempid);
			$r1 = new \backend\models\Investigationstepsbe();
			$r1->relatedid = $tempid;
			$r1->save();
		}
		
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
        	$model = investigationbe::find()->where(['=', 'created_at', $model->created_at])->andWhere(['=', 'created_by', $model->created_by])->one();
        	$linked = \backend\models\Investigationstepsbe::find()->where(['relatedid' => Yii::$app->session->get('investstempid')])->all();
				foreach ($linked as $a => $link) {
					$link->relatedid = $model->id;
					$link->save();
				}
			Yii::$app->session->remove('investstempid');
        
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
                'vehicles' => $vehicles,
                'employees' => $employees,
     
            ]);
        }
    }

    /**
     * Updates an existing investigationbe model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id) {
        $model = $this->findModel($id);
		$employees = \yii\helpers\ArrayHelper::map(\backend\models\User::find()->where(['is', 'blocked_at', null])->orderby(['username' => SORT_ASC])->all(), 'id', 'fullname');
        $vehicles = \yii\helpers\ArrayHelper::map(\modules\cors\models\Vehiclebe::find()->where(['=', 'status', 'Active'])->orderby(['reg' => SORT_ASC])->all(), 'id', 'reg');
     
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
                'vehicles' => $vehicles,
                'employees' => $employees,
            ]);
        }
    }

    /**
     * Deletes an existing investigationbe model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
		$model = $this->findModel($id);
		$model->status = 'Deleted';
		$model->deleted_at = strtotime('now');
		$model->save();
        return $this->redirect(['index']);
    }

    /**
     * Finds the investigationbe model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return investigationbe the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = investigationbe::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
