<?php

namespace app\controllers\mikrotik\mpls\vpls;

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\mpls\vpls\Vpls;
use app\models\mikrotik\mpls\vpls\Bgp;
use app\models\mikrotik\mpls\vpls\Cisco;

class VplsController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_vplsViewVpls'],
                    ],
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_vplsCreateVpls'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_vplsDeleteVpls'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_vplsDisableVpls'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_vplsEnableVpls'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_vplsUpdateVpls'],
                    ],
                    [
                        '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 Vpls();
        $provider = $model->getAll();


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

        $bgp = new Bgp();
        $bgps = $bgp->getAll();


        $provide['bgp'] = new ArrayDataProvider([
            'allModels' => $bgps,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'route_distinguisher',
                    'import_route_targets',
                    'export_route_targets',
                    'site_id',
                    'bridge',
                    'bridge_cost',
                    'bridge_horizon',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $cisco = new Cisco();
        $ciscos = $cisco->getAll();


        $provide['cisco'] = new ArrayDataProvider([
            'allModels' => $ciscos,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'route_distinguisher',
                    'import_route_targets',
                    'export_route_targets',
                    'vpls_id',
                    'bridge',
                    'bridge_cost',
                    'bridge_horizon',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

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

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

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

        return $this->render('/mikrotik/mpls/vpls/vpls', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'update',
        ]);
    }

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

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

        return $this->render('/mikrotik/mpls/vpls/vpls', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'create',
        ]);
    }

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

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

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

}
