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

class QueuesController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_queuesViewQueues'],
                    ],
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_queuesCreateQueues'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_queuesDeleteQueues'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_queuesDisableQueues'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_queuesEnableQueues'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_queuesUpdateQueues'],
                    ],
                    [
                        '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 Tree();
//        return var_dump($model->getAll());
        $simple = new Simple();
        $provide['simple'] = new ArrayDataProvider([
            'allModels' => $simple->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'target',
                    'max_limit',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $model = new Interfaces();
        $provide['interface'] = new ArrayDataProvider([
            'allModels' => $model->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'interface',
                    'queue',
                    'active_queue',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $type = new Types();

        $provide['type'] = new ArrayDataProvider([
            'allModels' => $type->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'kind',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $tree = new Tree();

        $provide['tree'] = new ArrayDataProvider([
            'allModels' => $tree->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'parent',
                    'packet_mark',
                    'limit_at',
                    'max_limit',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);


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

}
