<?php

namespace app\controllers\mikrotik\mpls\traffic;

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\Interfaces;
use app\models\mikrotik\mpls\traffic\Tunnel;
use app\models\mikrotik\mpls\traffic\Traffic;

class TrafficengController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'except' => ['index'],
                'rules' => [
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_trafficCreateTrafficEng'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_trafficDeleteTrafficEng'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_trafficDisableTrafficEng'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_trafficEnableTrafficEng'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_trafficUpdateTrafficEng'],
                    ],
                    [
                        '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 Traffic();
        $path = new Tunnel();
        $paths = $path->names();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $opt = $_POST['Traffic']['secondary_paths'];
            if ($opt != '') {
                $opt = implode(",", $opt);
                $model->secondary_paths = $opt;
            }
            $c = $model->update($id);
            if ($c != '1') {
                Yii::$app->session->setFlash('message', $c);
            } else {
                return $this->redirect(Url::toRoute('mikrotik/mpls/traffic/traffic/index'));
            }
        } else {
            $model->get($id);
        }
        $model->secondary_paths = explode(',', $model->secondary_paths);
        return $this->render('/mikrotik/mpls/traffic/enginterface', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'update',
                    'paths' => $paths,
        ]);
    }

    public function actionCreate() {
        $menu = new MikrotikSideMenu();
        $model = new Traffic();
        $path = new Tunnel();
        $paths = $path->names();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $opt = $_POST['Traffic']['secondary_paths'];
            if ($opt != '') {
                $opt = implode(",", $opt);
                $model->secondary_paths = $opt;
            }
            $c = $model->create();
            if ($c != '1') {
                Yii::$app->session->setFlash('message', $c);
            } else {
                return $this->redirect(Url::toRoute('mikrotik/mpls/traffic/traffic/index'));
            }
        }

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

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

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

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

}
