<?php

namespace app\controllers\mikrotik\system;

use yii\helpers\Url;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\mikrotik\MikrotikSideMenu;
use yii\data\ArrayDataProvider;
use app\models\mikrotik\system\Watchdog;

class WatchdogController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'except' => ['index'],
                'rules' => [
                    [

                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemUpdateWatchdog'],
                    ],
                    [
                        'actions' => [],
                        'allow' => true,
                        'roles' => ['admin'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    public function actions() {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    public function actionUpdate() {
        $menu = new MikrotikSideMenu();
        $model = new Watchdog();

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {

            $c = $model->update();
            if ($c != '1') {
                Yii::$app->session->setFlash('message', $c);
            } else {
                return $this->redirect(Url::toRoute('mikrotik/system/watchdog/update'));
            }
        } else {
            $model->get();
        }

        return $this->render('/mikrotik/system/watchdog', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
        ]);
    }

}
