<?php

namespace app\controllers\mikrotik\queues;

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\queues\Interfaces;
use app\models\mikrotik\queues\Tree;

class TreeController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'except' => ['index'],
                'rules' => [
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_queuesCreateTree'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_queuesDeleteTree'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_queuesDisableTree'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_queuesEnableTree'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_queuesUpdateTree'],
                    ],
                    [
                        '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 Tree();

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

        return $this->render('/mikrotik/queues/tree', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'update',
                    'type' => $model->getTypes(),
                    'parent' => $model->getInterfaces(),
                    'trees' => $model->getTreeName(),
        ]);
    }

    public function actionCreate() {

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

        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/queues/queues/index'));
            }
        }

        return $this->render('/mikrotik/queues/tree', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'create',
                    'type' => $model->getTypes(),
                    'parent' => $model->getInterfaces(),
                    'trees' => $model->getTreeName(),
        ]);
    }

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

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

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

}
