<?php

namespace app\controllers\mikrotik\routing\mme;

use yii\helpers\Url;
use Yii;
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\routing\mme\Interfacesmme;
use app\models\mikrotik\routing\mme\Network;
use app\models\mikrotik\routing\mme\Originators;
use app\models\mikrotik\Interfaces;

class InterfacesController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingViewMMEInterfaces'],
                    ],
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingCreateMMEInterfaces'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingDeleteMMEInterfaces'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingDisableMMEInterfaces'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingEnableMMEInterfaces'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingUpdateMMEInterfaces'],
                    ],
                    [
                        '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();
        $bdf = new Interfacesmme();
        $bdfs = $bdf->getAll();


        $provide['interface'] = new ArrayDataProvider([
            'allModels' => $bdfs,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'interface',
                    'passive',
                    'primary',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $net = new Network();
        $nets = $net->getAll();


        $provide['net'] = new ArrayDataProvider([
            'allModels' => $nets,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'network',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $orig = new Originators();
        $origs = $orig->getAll();


        $provide['orig'] = new ArrayDataProvider([
            'allModels' => $origs,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'originator',
                    'gateway',
                    'gateway_class',
                    'last_packet_before',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        return $this->render('/mikrotik/routing/mme/index', [
                    'provider' => $provide,
                    'menu' => $menu->getMenu(),
        ]);
    }

    public function actionUpdate() {
        $id = Yii::$app->request->get('id');
        $menu = new MikrotikSideMenu();
        $model = new Interfacesmme();

        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/routing/mme/interfaces/index'));
            }
        } else {
            $model->get($id);
        }
        $interface = new Interfaces();
        $int = $interface->names();
        return $this->render('/mikrotik/routing/mme/interfaces', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'update',
                    'int' => $int,
        ]);
    }

    public function actionCreate() {
        $menu = new MikrotikSideMenu();
        $model = new Interfacesmme();

        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/routing/mme/interfaces/index'));
            }
        }
        $interface = new Interfaces();
        $int = $interface->names();
        return $this->render('/mikrotik/routing/mme/interfaces', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'create',
                    'int' => $int,
        ]);
    }

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

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

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

}
