<?php

namespace modules\cors\controllers;

use Yii;
use modules\cors\models\Vehiclebe;
use modules\cors\models\VehiclebeSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * VehicleController implements the CRUD actions for vehicle model.
 */
class VehicleController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::class,
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }
    /**
     * Lists all vehicle models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new vehiclebeSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('@modules/cors/views/vehicle/index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

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

    /**
     * Creates a new vehicle model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new vehiclebe();
        $tempid = Yii::$app->session->get('shtempid');
        if (!$tempid) {	
			$tempid = \common\models\Base::tempId();
        	Yii::$app->session->set('shtempid', $tempid);
			$r1 = new \modules\cors\models\Vehicleservicingbe();
			$r1->relatedid = $tempid;
			$r1->save();
		}
		

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
        	$model = \modules\cors\models\Vehiclebe::find()->where(['=','created_at',$model->created_at])->andwhere(['=','created_by',$model->created_by])->One();
   			$linked = \modules\cors\models\Vehicleservicingbe::find()->where(['relatedid' => Yii::$app->session->get('shtempid')])->all();
			foreach ($linked as $a => $link) {
				$link->relatedid = $model->id;
				$link->save();
			}
			Yii::$app->session->remove('tempid');
          	
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('@modules/cors/views/vehicle/create', [
                'model' => $model,
            ]);
        }
    }
	public function actionAddrow() {
		$r1 = new \modules\cors\models\Vehicleservicingbe();
    	$r1->relatedid = Yii::$app->request->post('id');
		if ($r1->save()) {
			return json_encode(['result' => 200, 'message' => 'Added']);
		} else {
			return json_encode(['result' => 100, 'message' => $r1->errors]);
		}    
	}
	
	public function actionDeleterow() {
		$row = \modules\cors\models\Vehicleservicingbe::find()->where(['id' => Yii::$app->request->post('id')])->one();
		$row->status = 'deleted';
		$row->deleted_at = strtotime('now');
		$row->deleted_by = Yii::$app->user->identity->id;
		if ($row->save()) {	
			return json_encode(['result' => 200, 'message' => 'Deleted']);
		} else {
			return json_encode(['result' => 100, 'message' => $row->errors]);
		}
	}
	public function actionSavefield() {
		$field = Yii::$app->request->post('field');
		$value = Yii::$app->request->post('value');
		$row = \modules\cors\models\Vehicleservicingbe::find()->where(['id' => Yii::$app->request->post('id')])->one();
		$row->$field = $value;
		if ($row->save()) {	
			return json_encode(['result' => 200, 'message' => 'Saved']);
		} else {
			return json_encode(['result' => 100, 'message' => $row->errors]);
		}
	}
    /**
     * Updates an existing vehicle 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('@modules/cors/views/vehicle/update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing vehicle 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->deleted_at = strtotime('now');
        $model->deleted = TRUE;
        $model->save();
        return $this->redirect(['index']);
    }
    public function actionUndelete($id)
    {
        $model = $this->findModel($id);
        $model->deleted_at = null;
        $model->deleted = null;
        $model->save();
        return $this->redirect(['index']);
    }

	
    public function actionDeleted() {
        $searchModel = new VehiclebeSearch();
        $dataProvider = $searchModel->deleted(Yii::$app->request->queryParams);

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

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