<?php

namespace app\controllers\mikrotik\ip\ipsec;

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\ip\ipsec\Peers;

class PeersController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'except' => ['index'],
                'rules' => [
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipCreateIPsecPeers'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDeleteIPsecPeers'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDisableIPsecPeers'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipEnableIPsecPeers'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipUpdateIPsecPeers'],
                    ],
                    [
                        '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 Peers();
        return var_dump($model->getAll());
    }

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

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            if ($_POST['Peers']['enc_algorithm'] != '') {
                $opt = implode(",", $_POST['Peers']['enc_algorithm']);
                $model->enc_algorithm = $opt;
            }
            if ($_POST['Peers']['dh_group'] != '') {
                $opt = implode(",", $_POST['Peers']['dh_group']);
                $model->dh_group = $opt;
            }
            $c = $model->update($id);
            if ($c != '1') {
                Yii::$app->session->setFlash('message', $c);
            } else {
                return $this->redirect(Url::toRoute('mikrotik/ip/ipsec/ipsec/index'));
            }
        } else {
            $model->get($id);
        }
        $model->enc_algorithm = explode(',', $model->enc_algorithm);
        $model->dh_group = explode(',', $model->dh_group);
        return $this->render('/mikrotik/ip/ipsec/peers', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'update',
                    'policyGroup' => $model->getPolicy(),
                    'modeConfig' => $model->getMode(),
                    'key' => $model->getKeys(),
        ]);
    }

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

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            if ($_POST['Peers']['enc_algorithm'] != '') {
                $opt = implode(",", $_POST['Peers']['enc_algorithm']);
                $model->enc_algorithm = $opt;
            }
            if ($_POST['Peers']['dh_group'] != '') {
                $opt = implode(",", $_POST['Peers']['dh_group']);
                $model->dh_group = $opt;
            }
            $c = $model->create();
            if ($c != '1') {
                Yii::$app->session->setFlash('message', $c);
            } else {
                return $this->redirect(Url::toRoute('mikrotik/ip/ipsec/ipsec/index'));
            }
        }

        return $this->render('/mikrotik/ip/ipsec/peers', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'create',
                    'policyGroup' => $model->getPolicy(),
                    'modeConfig' => $model->getMode(),
                    'key' => $model->getKeys(),
        ]);
    }

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

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

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

}
