<?php

namespace app\controllers\mikrotik\interfaces;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

use Yii;
use yii\helpers\Url;
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\interfaces\Interfaces;
use app\models\mikrotik\interfaces\Ethernet;
use app\models\mikrotik\interfaces\Eoip;
use app\models\mikrotik\interfaces\Ip;
use app\models\mikrotik\interfaces\Gre;
use app\models\mikrotik\interfaces\Vlan;
use app\models\mikrotik\interfaces\Vrrp;
use app\models\mikrotik\interfaces\Bonding;

class InterfacesController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index'], 
                        'allow' => true, 
                        'roles' => ['mikrotik_interfacesView'], 
                    ],
                    [
                        '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 Interfaces();
        $provider = $model->getAll();
        $provide['interfaces'] = new ArrayDataProvider([
            'allModels' => $provider,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'type',
                    'l2mtu',
                    'tx_byte',
                    'rx_byte',
                    'tx_packet',
                    'rx_packet',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $ethernet = new Ethernet();
        $eth = $ethernet->getAll();
        $provide['ethernet'] = new ArrayDataProvider([
            'allModels' => $eth,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'type',
                    'l2mtu',
                    'tx_bytes',
                    'rx_bytes',
                    'master_port',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $eoip = new Eoip();
        $eo = $eoip->getAll();
        $provide['eoip'] = new ArrayDataProvider([
            'allModels' => $eo,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'type',
                    'actual_mtu',
                    'l2mtu',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $ip = new Ip();
        $ipip = $ip->getAll();
        $provide['ip'] = new ArrayDataProvider([
            'allModels' => $ipip,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'type',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $gre = new Gre();
        $gr = $gre->getAll();
        $provide['gre'] = new ArrayDataProvider([
            'allModels' => $gr,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'type',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $vlan = new Vlan();
        $vl = $vlan->getAll();
        $provide['vlan'] = new ArrayDataProvider([
            'allModels' => $vl,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'type',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $vrrp = new Vrrp();
        $vr = $vrrp->getAll();
        $provide['vrrp'] = new ArrayDataProvider([
            'allModels' => $vr,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'type',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $bonding = new Bonding();
        $bond = $bonding->getAll();
        $provide['bonding'] = new ArrayDataProvider([
            'allModels' => $bond,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'type',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        return $this->render('/mikrotik/interfaces/index', [
                    'provider' => $provide,
                    'menu' => $menu->getMenu(),
                    'model' => $model,
        ]);
    }

}
