<?php

namespace modules\cors\controllers;

use Yii;
use modules\cors\models\Vehicleservicingbe;
use modules\cors\models\VehicleservicingbeSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * VehicleservicingbeController implements the CRUD actions for vehicleservicingbe model.
 */
class VehicleservicingbeController 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 vehicleservicingbe models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new vehicleservicingbeSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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

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

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

    /**
     * Creates a new vehicleservicingbe model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new vehicleservicingbe();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
        	$model = vehicleservicingbe::find()->where(['=', 'created_at', $model->created_at])->andWhere(['=', 'created_by', $model->created_by])->one();
        
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing vehicleservicingbe 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);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

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