<?php

namespace app\controllers\mikrotik\bridge;

use yii\helpers\Url;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\mikrotik\MikrotikSideMenu;
use app\models\mikrotik\bridge\Port;
use app\models\mikrotik\bridge\Bridge;
use app\models\mikrotik\Interfaces;
use yii\data\ArrayDataProvider;

class PortController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'except' => ['index'],
                'rules' => [
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_bridgeCreatePort'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_bridgeDeletePort'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_bridgeDisablePort'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_bridgeEnablePort'],
                    ], [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_bridgeUpdatePort'],
                    ],
                    [
                        '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 Port();
        $bridge = $model->getAll();

        $provide['bridge'] = new ArrayDataProvider([
            'allModels' => $bridge,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'type',
                    'l2mtu',
                    'mac_address',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        return $this->render('/mikrotik/bridge/index', [
                    'provider' => $provide,
                    'menu' => $menu->getMenu(),
                    'model' => $model,
        ]);
    }

    public function actionUpdate() {
        $id = Yii::$app->request->get('id');
        $menu = new MikrotikSideMenu();
        $model = new Port();
        $inter = new Interfaces();
        $et = $inter->getNames();
        foreach ($et as $value) {
            $eth[$value] = $value;
        }
        $bridge = new Bridge();
        $br = $bridge->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/bridge/bridge/index'));
            }
        } else {
            $model->get($id);
        }

        return $this->render('/mikrotik/bridge/port', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'update',
                    'interfaces' => $eth,
                    'bridge' => $br,
        ]);
    }

    public function actionCreate() {
        $menu = new MikrotikSideMenu();
        $model = new Port();
        $inter = new Interfaces();
        $et = $inter->getNames();
        foreach ($et as $value) {
            $eth[$value] = $value;
        }
        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/bridge/bridge/index'));
            }
        }
        $bridge = new Bridge();
        $br = $bridge->getNames();
        return $this->render('/mikrotik/bridge/port', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'create',
                    'interfaces' => $eth,
                    'bridge' => $br,
        ]);
    }

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

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

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

}
