<?php

namespace app\controllers\mikrotik\routing\bgp;

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\bgp\Vrf;
use app\models\mikrotik\routing\bgp\Instances;

class VrfController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'except' => ['index'],
                'rules' => [
                     [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingCreateBGPVrf'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingDeleteBGPVrf'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingDisableBGPVrf'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingEnableBGPVrf'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingUpdateBGPVrf'],
                    ],
                    [
                        '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 Vrf();
        $marks = $model->getNames();
        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/bgp/bgp/index'));
            }
        } else {
            $model->get($id);
        }
        $instances = new Instances();
        $names = $instances->getNames();

        
        return $this->render('/mikrotik/routing/bgp/vrf', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'update',
                    'names' => $names,
                    'mark' => $marks,
        ]);
    }

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

        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/bgp/bgp/index'));
            }
        }
        $instances = new Instances();
        $names = $instances->getNames();
        $marks = $model->getNames();
        return $this->render('/mikrotik/routing/bgp/vrf', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'create',
                    'names' => $names,
                    'mark' => $marks,
        ]);
    }

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

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

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

}
