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

class GroupsController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'except' => ['index'],
                'rules' => [
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemCreateUserGroups'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemDeleteUserGroups'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemDisableUserGroups'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemEnableUserGroups'],
                    ],
                     [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemUpdateUserGroups'],
                    ],
                    [
                        '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() {
        $id = Yii::$app->request->get('id');
        $menu = new MikrotikSideMenu();
        $model = new Groups();

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            if ($_POST['Groups']['policy'] != '') {
                $opt = implode(",", $_POST['Groups']['policy']);
                $model->policy = $opt;
            }
            $c = $model->update($id);
            if ($c != '1') {
                Yii::$app->session->setFlash('message', $c);
            } else {
                return $this->redirect(Url::toRoute('mikrotik/system/users/index'));
            }
        } else {
            $model->get($id);
        }
        $model->policy = explode(',', $model->policy);
        return $this->render('/mikrotik/system/groups', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'update',
        ]);
    }

    public function actionCreate() {

        $menu = new MikrotikSideMenu();
        $model = new Groups();

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            if ($_POST['Groups']['policy'] != '') {
                $opt = implode(",", $_POST['Groups']['policy']);
                $model->policy = $opt;
            }
            $c = $model->create();
            if ($c != '1') {
                Yii::$app->session->setFlash('message', $c);
            } else {
                return $this->redirect(Url::toRoute('mikrotik/system/users/index'));
            }
        }

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

    public function actionEnable() {
        $id = Yii::$app->request->get('id');
        $model = new Groups();
        $model->enable($id);
        return $this->redirect(Url::toRoute('mikrotik/system/users/index'));
    }

    public function actionDisable() {
        $id = Yii::$app->request->get('id');
        $model = new Groups();
        $model->disable($id);
        return $this->redirect(Url::toRoute('mikrotik/system/users/index'));
    }

    public function actionDelete() {
        $id = Yii::$app->request->get('id');
        $model = new Groups();
        $model->delete($id);
        return $this->redirect(Url::toRoute('mikrotik/system/users/index'));
    }

}
