<?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\Users;
use app\models\mikrotik\system\Groups;
use app\models\mikrotik\system\Keys;
use app\models\mikrotik\system\Privates;
use app\models\mikrotik\system\Active;

class UsersController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemViewUsers'],
                    ],
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemCreateUsers'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemDeleteUsers'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemDisableUsers'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemEnableUsers'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemUpdateUsers'],
                    ],
                    [
                        '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();
        $scep = new Users();


        $provide['users'] = new ArrayDataProvider([
            'allModels' => $scep->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'group',
                    'last_logged_in',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $j = new Groups();

        $provide['groups'] = new ArrayDataProvider([
            'allModels' => $j->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'policy',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $k = new Keys();
        $provide['keys'] = new ArrayDataProvider([
            'allModels' => $k->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'user',
                    'key_owner',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $p = new Privates();
        $provide['private'] = new ArrayDataProvider([
            'allModels' => $p->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'user',
                    'key_owner',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $a = new Active();

        $provide['active'] = new ArrayDataProvider([
            'allModels' => $a->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'when',
                    'address',
                    'via',
                    'group',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

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

    public function actionUpdate() {
        $id = Yii::$app->request->get('id');
        $menu = new MikrotikSideMenu();
        $model = new Users();

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

            $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);
        }

        return $this->render('/mikrotik/system/user', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'update',
                    'group' => $model->getGroups(),
        ]);
    }

    public function actionCreate() {

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

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

            $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/user', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'create',
                    'group' => $model->getGroups(),
        ]);
    }

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

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

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

}
