<?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\Instances;
use app\models\mikrotik\routing\bgp\Vrf;
use app\models\mikrotik\routing\bgp\Peer;
use app\models\mikrotik\routing\bgp\Networks;
use app\models\mikrotik\routing\bgp\Aggregate;
use app\models\mikrotik\routing\bgp\Route;
use app\models\mikrotik\routing\bgp\Advertisements;

class BgpController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
        
                'rules' => [
                 [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingViewBGP'],
                    ],
                    [
                        '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();
        $bdf = new Instances();
        $bdfs = $bdf->getAll();

        $provide['instances'] = new ArrayDataProvider([
            'allModels' => $bdfs,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'as',
                    'router_id',
                    'out_filter',
                    'cluster_id',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $vrf = new Vrf();
        $vrfs = $vrf->getAll();

        $provide['vrf'] = new ArrayDataProvider([
            'allModels' => $vrfs,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'instance',
                    'routing_mark',
                    'in_filter',
                    'out_filter',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $peer = new Peer();
        $peers = $peer->getAll();

        $provide['peer'] = new ArrayDataProvider([
            'allModels' => $peers,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'instance',
                    'remote_address',
                    'remote_as',
                    'multihop',
                    'route_reflect',
                    'ttl',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $network = new Networks();
        $networks = $network->getAll();

        $provide['network'] = new ArrayDataProvider([
            'allModels' => $networks,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'network',
                    'synchronize',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $agg = new Aggregate();
        $aggss = $agg->getAll();

        $provide['ags'] = new ArrayDataProvider([
            'allModels' => $aggss,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'prefix',
                    'summary_only',
                    'inherit_attributes',
                    'include_igp',
                    'attribute_filter',
                    'suppress_filter',
                    'advertise_filter',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $route = new Route();
        $routes = $route->getAll();

        $provide['route'] = new ArrayDataProvider([
            'allModels' => $routes,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'dst_address',
                    'gateway',
                    'in_label',
                    'interface',
                    'out_label',
                    'route_distinguisher',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $adv = new Advertisements();
        $advertisements = $adv->getAll();

        $provide['advertisements'] = new ArrayDataProvider([
            'allModels' => $advertisements,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'peer',
                    'prefix',
                    'nexthop',
                    'as_path',
                    'origin',
                    'local_pref',
                    'med',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        return $this->render('/mikrotik/routing/bgp/index', [
                    'provider' => $provide,
                    'menu' => $menu->getMenu(),
        ]);
    }

}
