<?php

namespace app\controllers\mikrotik\switchm;

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\switchm\Rule;
use app\models\mikrotik\switchm\Switchm;
use app\models\mikrotik\interfaces\Ethernet;

class RuleController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'except' => ['index'],
                'rules' => [
                   [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_switchCreateRule'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_switchDeleteRule'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_switchDisableRule'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_switchEnableRule'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_switchUpdateRule'],
                    ],
                    [
                        '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() {
        $model = new Rule();
    }

    public function actionUpdate() {
        $id = Yii::$app->request->get('id');
        $menu = new MikrotikSideMenu();
        $model = new Rule();
        $inter = new Ethernet();
        $eth = $inter->getNames();
        $sw = new Switchm();
        $swName = $sw->getNames();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $opt = $_POST['Rule']['ports'];
            if ($opt != '') {
                $opt = implode(",", $opt);
            }
            $model->ports = $opt;
            $opt = $_POST['Rule']['new_dst_ports'];
            if ($opt != '') {
                $opt = implode(",", $opt);
            }
            $model->new_dst_ports = $opt;
            $c = $model->update($id);
            if ($c != '1') {
                Yii::$app->session->setFlash('message', $c);
            } else {
                return $this->redirect(Url::toRoute('mikrotik/switchm/switchm/index'));
            }
        } else {
            $model->get($id);
        }
        $model->ports = explode(',', $model->ports);
        $model->new_dst_ports = explode(',', $model->new_dst_ports);
        return $this->render('/mikrotik/switchm/rule', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'update',
                    'interfaces' => $eth,
                    'swName' => $swName,
        ]);
    }

    public function actionCreate() {

        $menu = new MikrotikSideMenu();
        $model = new Rule();
        $inter = new Ethernet();
        $sw = new Switchm();
        $swName = $sw->getNames();
        $eth = $inter->getNames();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $opt = $_POST['Rule']['ports'];
            if ($opt != '') {
                $opt = implode(",", $opt);
            }
            $model->ports = $opt;
            $opt = $_POST['Rule']['new_dst_ports'];
            if ($opt != '') {
                $opt = implode(",", $opt);
            }
            $model->new_dst_ports = $opt;
            $c = $model->create();
            if ($c != '1') {
                Yii::$app->session->setFlash('message', $c);
            } else {
                return $this->redirect(Url::toRoute('mikrotik/switchm/switchm/index'));
            }
        }

        return $this->render('/mikrotik/switchm/rule', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'create',
                    'interfaces' => $eth,
                    'swName' => $swName,
        ]);
    }

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

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

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

}
