<?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\Identity;

class IdentityController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'except' => ['index'],
                'rules' => [
                    [
///access controll//////////////////////////////////////////////////////////
                        'actions' => ['index'], //what should be controlled - actionPing 
                        'allow' => true, //allow or deny this action   
                        'roles' => ['mikrotik_systemUpdateIdentity'], //"controller's name"_"rule name"
                    ],
                    [
                        '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 Identity();

        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/identity/index'));
            }
        } else {
            $model->get();
        }

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

}
