<?php

namespace app\controllers\mikrotik\interfaces;

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\interfaces\Bonding;
use app\models\mikrotik\Interfaces;

class BondingController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'except' => ['index'],
                'rules' => [
                   [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_interfacesCreateBonding'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_interfacesDeleteBonding'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_interfacesDisableBonding'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_interfacesEnableBonding'],
                    ], [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_interfacesUpdateBonding'],
                    ],

                    [
                        '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 Bonding();

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            if ($_POST['Bonding']['slaves'] != '') {
                $opt = implode(",", $_POST['Bonding']['slaves']);
            }
            $model->slaves = $opt;
            $c = $model->update($id);
            if ($c != '1') {
                Yii::$app->session->setFlash('message', $c);
            } else {
                return $this->redirect(Url::toRoute('mikrotik/interfaces/interfaces/index'));
            }
        } else {
            $model->get($id);
        }
        $inter = new Interfaces();
        $et = $inter->getNames();
        $eth['none'] = 'none';
        foreach ($et as $value) {
            $eth[$value] = $value;
        }
        $model->slaves = explode(',', $model->slaves);
        return $this->render('/mikrotik/interfaces/bonding', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'eth' => $eth,
                    'action' => 'update',
        ]);
    }

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

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            if ($_POST['Bonding']['slaves'] != '') {
                $opt = implode(",", $_POST['Bonding']['slaves']);
            }
            $model->slaves = $opt;
            $c = $model->create();
            if ($c != '1') {
                Yii::$app->session->setFlash('message', $c);
            } else {
                return $this->redirect(Url::toRoute('mikrotik/interfaces/interfaces/index'));
            }
        }
        $inter = new Interfaces();
        $et = $inter->getNames();
        $eth['none'] = 'none';
        foreach ($et as $value) {
            $eth[$value] = $value;
        }
        $model->slaves = explode(',', $model->slaves);
        return $this->render('/mikrotik/interfaces/bonding', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'eth' => $eth,
                    'action' => 'create',
        ]);
    }

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

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

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

}
