<?php

namespace app\controllers\mikrotik\routing\prefix;

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\routing\prefix\Prefix;
use app\models\mikrotik\Interfaces;

class PrefixController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingViewPrefix'],
                    ],
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingCreatePrefix'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingDeletePrefix'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingDisablePrefix'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingEnablePrefix'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingUpdatePrefix'],
                    ],
                    [
                        '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() {
        $model = new Prefix();
        $menu = new MikrotikSideMenu();
        $pref = $model->getAll();
        $provide = new ArrayDataProvider([
            'allModels' => $pref,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'chain',
                    'prefix',
                    'action',
                    'set_metric',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

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

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

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

        $chains = $model->getChains();
        return $this->render('/mikrotik/routing/prefix/prefix', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'update',
                    'chain' => $chains,
        ]);
    }

    public function actionCreate() {
        $menu = new MikrotikSideMenu();
        $model = new Prefix();

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

        $chains = $model->getChains();
        return $this->render('/mikrotik/routing/prefix/prefix', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'create',
                    'chain' => $chains,
        ]);
    }

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

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

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

}
