<?php
/**
 * Created by PhpStorm.
 * User: PHOENIX
 * Date: 28.04.16
 * Time: 17:23
 */

namespace common\modules\cases\controllers;

use Yii;
use common\components\AdminController;
use common\modules\cases\models\CasesBids;
use yii\data\ActiveDataProvider;
use yii\web\NotFoundHttpException;

class CasesBidsAdminController extends AdminController{
    public static function actionsTitles(){
        return [
            'Manage'          => 'Управление подписками',
            'Create'          => 'Добавление подписки',
            'Update'          => 'Редактирование подписки',
            'View'            => 'Просмотр подписки',
            'Delete'          => 'Удаление подписки',
        ];
    }

    /**
     * Lists all Bid models.
     * @return mixed
     */
    public function actionManage()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => CasesBids::find()
        ]);

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


    /**
     * @return string|\yii\web\Response
     */
    public function actionCreate() {
        $model = new CasesBids();

        if ($model->load(Yii::$app->request->post())) {
            $datetime=new \DateTime();
            $model->date=$datetime->format('Y-m-d H:i:s');
            if ($model->save())
                return $this->redirect([
                    'manage'
                ]);
        }
        $form = new \common\components\BaseForm('/common/modules/blog/forms/BidForm', $model);
        return $this->render(
            'create',
            [
                'form' => $form
            ]
        );
    }

    /**
     * @param $id
     * @return string|\yii\web\Response
     */
    public function actionUpdate($id){
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post())) {
            $datetime=new \DateTime();;
            $model->date=$datetime->format('Y-m-d H:i:s');
            if ($model->save())
                return $this->redirect([
                    'manage'
                ]);
        } else {
            $form = new \common\components\BaseForm('/common/modules/blog/forms/BidForm', $model);
            return $this->render(
                'update',
                [
                    'form' => $form
                ]
            );
        }
    }

    /**
     * @param $id
     * @return \yii\web\Response
     */
    public function actionDelete($id){
        $this->findModel($id)->delete();

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

    /**
     * @param $id
     * @return null|CasesBids
     * @throws NotFoundHttpException
     */
    protected function findModel($id)
    {
        if (($model = CasesBids::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
} 