<?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 yii\data\ArrayDataProvider;
use app\models\mikrotik\MikrotikSideMenu;
use app\models\mikrotik\system\Resources;
use app\models\mikrotik\system\Routerboard;

class SystemController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'except' => ['index'],
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemViewResources'],
                    ],
                    [
                        'actions' => ['reboot'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemReboot'],
                    ],
                    [
                        'actions' => ['doreboot'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemRebootExecute'],
                    ],
                    [
                        'actions' => ['rb'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemRouterBoardInfo'],
                    ],
                    [
                        '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 actionIndex() {
        $menu = new MikrotikSideMenu();
        $model = new Resources();
        $model->getAll();


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

    public function actionDoreboot() {
        $model = new Resources();
        $model->reboot();
        $session = Yii::$app->session;
        $session->open();
        $session->set('active', 'no');
        $session->remove('ip');
        $session->remove('login');
        $session->remove('pwd');
        return $this->redirect(Url::toRoute('mikrotik/index'));
    }

    public function actionReboot() {
        $menu = new MikrotikSideMenu();
        $model = new Resources();

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

    public function actionRb() {
        $menu = new MikrotikSideMenu();
        $model = new Routerboard();
        $model->get();

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

}
