<?php

namespace app\controllers\mikrotik\ip\upnp;

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\upnp\Interfacesupnp;
use app\models\mikrotik\Interfaces;

class InterfacesController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipViewUPnPInterfaces'],
                    ],
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipCreateUPnPInterfaces'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDeleteUPnPInterfaces'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDisableUPnPInterfaces'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipEnableUPnPInterfaces'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipUpdateUPnPInterfaces'],
                    ],
                    [
                        '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 Interfacesupnp();

        $provider = $model->getAll();

        $provide = new ArrayDataProvider([
            'allModels' => $provider,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'interface',
                    'type',
                    'forced_external_ip',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);


        return $this->render('/mikrotik/ip/upnp/interfaces', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'provider' => $provide,
        ]);
    }

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

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

        return $this->render('/mikrotik/ip/upnp/interface', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'create',
                    'interfaces' => $intNames,
        ]);
    }

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

            $model->update($id);

            return $this->redirect(Url::toRoute('mikrotik/ip/upnp/interfaces/index'));
        }
        $model->get($id);
        $interfaces = new Interfaces();
        $intName = $interfaces->names();
        foreach ($intName as $value) {
            $intNames[$value] = $value;
        }
        return $this->render('/mikrotik/ip/upnp/interface', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'update',
                    'interfaces' => $intNames,
        ]);
    }

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

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

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

}
