<?php

namespace backend\controllers;

use backend\models\MasterSkema;
use backend\models\MasterSkemaSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * MasterSkemaController implements the CRUD actions for MasterSkema model.
 */
class MasterSkemaController extends Controller
{
    /**
     * @inheritDoc
     */
    public function behaviors()
    {
        return array_merge(
            parent::behaviors(),
            [
                'verbs' => [
                    'class' => VerbFilter::className(),
                    'actions' => [
                        'delete' => ['POST'],
                    ],
                ],
            ]
        );
    }

    /**
     * Lists all MasterSkema models.
     *
     * @return string
     */
    public function actionIndex()
    {
        $searchModel = new MasterSkemaSearch();
        $dataProvider = $searchModel->search($this->request->queryParams);

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

    /**
     * Displays a single MasterSkema model.
     * @param string $SKEMA_UUID Skema Uuid
     * @return string
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($SKEMA_UUID)
    {
        return $this->render('view', [
            'model' => $this->findModel($SKEMA_UUID),
        ]);
    }

    /**
     * Creates a new MasterSkema model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return string|\yii\web\Response
     */
    public function actionCreate()
    {
        $model = new MasterSkema();

        if ($this->request->isPost) {
            if ($model->load($this->request->post()) && $model->save()) {
                return $this->redirect(['view', 'SKEMA_UUID' => $model->SKEMA_UUID]);
            }
        } else {
            $model->loadDefaultValues();
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing MasterSkema model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $SKEMA_UUID Skema Uuid
     * @return string|\yii\web\Response
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($SKEMA_UUID)
    {
        $model = $this->findModel($SKEMA_UUID);

        if ($this->request->isPost && $model->load($this->request->post()) && $model->save()) {
            return $this->redirect(['view', 'SKEMA_UUID' => $model->SKEMA_UUID]);
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    /**
     * Deletes an existing MasterSkema model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $SKEMA_UUID Skema Uuid
     * @return \yii\web\Response
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($SKEMA_UUID)
    {
        $this->findModel($SKEMA_UUID)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the MasterSkema model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $SKEMA_UUID Skema Uuid
     * @return MasterSkema the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($SKEMA_UUID)
    {
        if (($model = MasterSkema::findOne(['SKEMA_UUID' => $SKEMA_UUID])) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}
