<?php

namespace common\modules\school\controllers;

use Yii;
use common\components\AdminController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

use common\modules\school\models\Lessons;
use common\modules\school\models\SearchLessons;

class LessonsAdminController extends AdminController
{
    public static function actionsTitles()
    {
        return array(
            'View'   => 'Просмотр урока',
            'Create' => 'Создание урока',
            'Update' => 'Редактирование урока',
            'Delete' => 'Удаление урока',
            'Manage' => 'Управление уроками',
        );
    }

    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    public function actionView($id)
    {
        $model = $this->findModel($id);

        Yii::$app->controller->page_title  = 'Просмотр урока';

        Yii::$app->controller->breadcrumbs = [
            ['Список уроков' => '/school/courses-admin/manage'],
            'Добавить урок'
        ];

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

    public function actionCreate()
    {
        $model = new Lessons;

        Yii::$app->controller->page_title = 'Добавить урок';
        Yii::$app->controller->breadcrumbs = [
            ['Список курсов' => '/school/courses-admin/manage'],
            'Добавить урок'
        ];

        if ($model->load(Yii::$app->request->post()) && $model->save()){
            return $this->redirect(['manage']);
        } 
        else 
        {
            $form = new \common\components\BaseForm('/common/modules/school/forms/LessonForm', $model);
            return $this->render('create', [
                'model' => $model,
                'form' => $form->out
            ]);
        }
    }

    public function actionUpdate($id)
    {
        Yii::$app->controller->page_title = 'Редактировать урок';
        Yii::$app->controller->breadcrumbs = [
            ['Список уроков' => '/school/courses-admin/manage'],
            'Редактировать урок'
        ];
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['manage']);
        } 
        else 
        {
            $form = new \common\components\BaseForm('/common/modules/school/forms/LessonForm', $model);
            return $this->render('update', [
                'model' => $model,
                'form' => $form->out
            ]);
        }
    }

    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

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

    public function actionManage()
    {
        $searchModel = new SearchLessons();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams["SearchLessons"]);

        \yii::$app->controller->page_title = 'Управление уроками';

        \yii::$app->controller->breadcrumbs = [
                    'Управление уроками',
        ];

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

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