<?php

namespace app\controllers\mikrotik\capsman;

use yii\helpers\Url;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\mikrotik\MikrotikSideMenu;
use yii\data\ArrayDataProvider;
use app\models\mikrotik\capsman\Configuration;

class ConfigurationController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'except' => ['index'],
                'rules' => [
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_capsmanCreateConfiguration'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_capsmanDeleteConfiguration'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_capsmanDisableConfiguration'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_capsmanEnableConfiguration'],
                    ], [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_capsmanUpdateConfiguration'],
                    ],
                    [
                        '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() {
        $model = new Configuration();
        return var_dump($model->getAll());
    }

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

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            if ($_POST['Configuration']['rx_chains'] != '') {
                $opt = implode(",", $_POST['Configuration']['rx_chains']);
                $model->rx_chains = $opt;
            }
            if ($_POST['Configuration']['tx_chains'] != '') {
                $opt = implode(",", $_POST['Configuration']['tx_chains']);
                $model->tx_chains = $opt;
            }
            if ($_POST['Configuration']['securityauthentication_types'] != '') {
                $opt = implode(",", $_POST['Configuration']['securityauthentication_types']);
                $model->securityauthentication_types = $opt;
            }
            if ($_POST['Configuration']['securityencryption'] != '') {
                $opt = implode(",", $_POST['Configuration']['securityencryption']);
                $model->securityencryption = $opt;
            }

            if ($_POST['Configuration']['securityeap_methods'] != '') {
                $opt = implode(",", $_POST['Configuration']['securityeap_methods']);
                $model->securityeap_methods = $opt;
            }
            $c = $model->update($id);
            if ($c != '1') {
                Yii::$app->session->setFlash('message', $c);
            } else {
                return $this->redirect(Url::toRoute('mikrotik/capsman/capsman/index'));
            }
        } else {
            $model->get($id);
        }
        $model->rx_chains = explode(',', $model->rx_chains);
        $model->tx_chains = explode(',', $model->tx_chains);
        $model->securityauthentication_types = explode(',', $model->securityauthentication_types);
        $model->securityencryption = explode(',', $model->securityencryption);
        $model->securityeap_methods = explode(',', $model->securityeap_methods);
        return $this->render('/mikrotik/capsman/configurations', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'update',
                    'channel' => $model->getChannel(),
                    'datapath' => $model->getDatapath(),
                    'bridge' => $model->getBridge(),
                    'security' => $model->getSecurity(),
                    'cert' => $model->getCert(),
        ]);
    }

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

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            if ($_POST['Configuration']['rx_chains'] != '') {
                $opt = implode(",", $_POST['Configuration']['rx_chains']);
                $model->rx_chains = $opt;
            }
            if ($_POST['Configuration']['tx_chains'] != '') {
                $opt = implode(",", $_POST['Configuration']['tx_chains']);
                $model->tx_chains = $opt;
            }
            if ($_POST['Configuration']['securityauthentication_types'] != '') {
                $opt = implode(",", $_POST['Configuration']['securityauthentication_types']);
                $model->securityauthentication_types = $opt;
            }
            if ($_POST['Configuration']['securityencryption'] != '') {
                $opt = implode(",", $_POST['Configuration']['securityencryption']);
                $model->securityencryption = $opt;
            }

            if ($_POST['Configuration']['securityeap_methods'] != '') {
                $opt = implode(",", $_POST['Configuration']['securityeap_methods']);
                $model->securityeap_methods = $opt;
            }
            $c = $model->create();
            if ($c != '1') {
                Yii::$app->session->setFlash('message', $c);
            } else {
                return $this->redirect(Url::toRoute('mikrotik/capsman/capsman/index'));
            }
        }

        return $this->render('/mikrotik/capsman/configurations', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'create',
                    'channel' => $model->getChannel(),
                    'datapath' => $model->getDatapath(),
                    'bridge' => $model->getBridge(),
                    'security' => $model->getSecurity(),
                    'cert' => $model->getCert(),
        ]);
    }

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

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

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

}
