<?php

namespace app\controllers\mikrotik\routing\rip;

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\rip\Interfacesrip;
use app\models\mikrotik\routing\rip\Key;
use app\models\mikrotik\routing\prefix\Prefix;
use app\models\mikrotik\Interfaces;

class InterfacesController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'except' => ['index'],
                'rules' => [
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingCreateRIPInterfaces'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingDeleteRIPInterfaces'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingDisableRIPInterfaces'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingEnableRIPInterfaces'],
                    ],
                     [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingUpdateRIPInterfaces'],
                    ],
                    [
                        '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 Interfacesrip();

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

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

        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/rip/rip/index'));
            }
        }
        $interface = new Interfaces();
        $int = $interface->names();
        $chain = new Key();
        $chains = $chain->getChains();
        $pref = new Prefix();
        $prefix = $pref->getChains();
        return $this->render('/mikrotik/routing/rip/interfaces', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'create',
                    'int' => $int,
                    'keys' => $chains,
                    'prefix' => $prefix,
        ]);
    }

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

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

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

}
