<?php

namespace modules\custom\controllers;

use Yii;
use modules\custom\models\Complaintsbe;
use modules\custom\models\ComplaintsbeSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

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

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }    
    public function actionUser($user) {
        $searchModel = new ComplaintsbeSearch();
        $searchModal['employee'] = $user;
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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

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

    /**
     * Displays a single complaintsbe model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }
    
    /**
     * Creates a new complaintsbe model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Complaintsbe();
        $tempid = Yii::$app->session->get('comptempid');
        if (!$tempid) {	
			$tempid = \common\models\Base::tempId();
        	Yii::$app->session->set('comptempid', $tempid);
		}
		
		$employees = \frontend\models\User::Employees();
        $vehicles = yii\helpers\ArrayHelper::map(\modules\cors\models\Vehiclebe::find()->orderby(['reg' => SORT_ASC])->all(), 'id', 'reg');

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
        	$model = Complaintsbe::find()->where(['=', 'created_at', $model->created_at])->andWhere(['=', 'created_by', $model->created_by])->one();
      		$linked = \backend\models\Uploadsbe::find()->where(['relatedid' => Yii::$app->session->get('comptempid')])->all();
			foreach ($linked as $a => $link) {
				$link->relatedid = $model->id;
				$link->save();
			}
			Yii::$app->session->remove('comptempid');
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
                'vehicles' => $vehicles,
                'employees' => $employees,
            ]);
        }
    }

    /**
     * Updates an existing complaintsbe 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 = \frontend\models\User::Employees();
        $vehicles = yii\helpers\ArrayHelper::map(\modules\cors\models\Vehiclebe::find()->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 complaintsbe 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 complaintsbe model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return complaintsbe the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Complaintsbe::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
