<?php

namespace app\controllers\mikrotik\ip\routes;

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\ip\firewall\Mangle;
use app\models\mikrotik\ip\routes\Routes;
use app\models\mikrotik\ip\routes\Nexthops;
use app\models\mikrotik\ip\routes\Rules;
use app\models\mikrotik\Interfaces;

class RulesController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'except' => ['index'],
                'rules' => [
                   [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipCreateRoutesRules'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDeleteRoutesRules'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDisableRoutesRules'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipEnableRoutesRules'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipUpdateRoutesRules'],
                    ],
                    [
                        '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 actionDelete() {
        $id = Yii::$app->request->get('id');
        $model = new Rules();
        $model->delete($id);
        return $this->redirect(Url::toRoute('mikrotik/ip/routes/routes/index'));
    }

    public function actionCreate() {
        $menu = new MikrotikSideMenu();
        $model = new Rules();
        $interfaces = new Interfaces();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {

            $model->create();
            return $this->redirect(Url::toRoute('mikrotik/ip/routes/routes/index'));
        }
        $intName = $interfaces->names();
        foreach ($intName as $value) {
            $intNames[$value] = $value;
        }
        $mangle = new Mangle();
        $packetMark['routing'] = $mangle->getRoutingMark();
        return $this->render('/mikrotik/ip/routes/rules', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'intName' => $intNames,
                    'action' => 'create',
                    'packetMark' => $packetMark,
        ]);
    }

    public function actionUpdate() {
        $menu = new MikrotikSideMenu();
        $model = new Rules();
        $id = Yii::$app->request->get('id');
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {

            $model->update($id);
            return $this->redirect(Url::toRoute('mikrotik/ip/routes/routes/index'));
        }
        $interfaces = new Interfaces();
        $intName = $interfaces->names();
        foreach ($intName as $value) {
            $intNames[$value] = $value;
        }

        $model->get($id);
        $mangle = new Mangle();
        $packetMark['routing'] = $mangle->getRoutingMark();
        return $this->render('/mikrotik/ip/routes/rules', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'intName' => $intNames,
                    'action' => 'update',
                    'packetMark' => $packetMark,
        ]);
    }

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

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

}
