<?php

namespace app\controllers\mikrotik\ppp;

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\ppp\L2tpsecrets;
use app\models\mikrotik\ppp\Active;
use app\models\mikrotik\ppp\Profiles;
use app\models\mikrotik\ppp\Secrets;
use app\models\mikrotik\ppp\Servers;
use app\models\mikrotik\ppp\Interfaces;
use app\models\mikrotik\ppp\L2tpbinding;
use app\models\mikrotik\ppp\Ovpnbinding;
use app\models\mikrotik\ppp\Pppoebinding;
use app\models\mikrotik\ppp\Pptpbinding;
use app\models\mikrotik\ppp\Sstpbinding;
use app\models\mikrotik\ppp\L2tpclient;
use app\models\mikrotik\ppp\Ovpnclient;
use app\models\mikrotik\ppp\Pppoeclient;
use app\models\mikrotik\ppp\Pptpclient;
use app\models\mikrotik\ppp\Sstpclient;
use app\models\mikrotik\ppp\Pppclient;
use app\models\mikrotik\ppp\Pptp;
use app\models\mikrotik\ppp\Sstp;
use app\models\mikrotik\ppp\L2tp;
use app\models\mikrotik\ppp\Ovpn;

class PppController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_pppViewPpp'],
                    ],
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_pppCreatePpp'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_pppDeletePpp'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_pppDisablePpp'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_pppEnablePpp'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_pppUpdatePpp'],
                    ],
                    [
                        '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();

        $model = new L2tpsecrets();
        $provider = $model->getAll();

        $provide['l2tpsecret'] = new ArrayDataProvider([
            'allModels' => $provider,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'address',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $active = new Active();

        $provide['active'] = new ArrayDataProvider([
            'allModels' => $active->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'service',
                    'caller_id',
                    'address',
                    'uptime',
                    'encoding',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $profiles = new Profiles();
        $provide['profiles'] = new ArrayDataProvider([
            'allModels' => $profiles->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'local_address',
                    'remote_address',
                    'bridge',
                    'rate_limit',
                    'only_one',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $secrets = new Secrets();
        $provide['secrets'] = new ArrayDataProvider([
            'allModels' => $secrets->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'service',
                    'caller_id',
                    'profile',
                    'local_address',
                    'remote_address',
                    'last_logged_out',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $server = new Servers();
        $provide['server'] = new ArrayDataProvider([
            'allModels' => $server->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'service_name',
                    'interface',
                    'max_mtu',
                    'max_mru',
                    'mrru',
                    'default_profile',
                    'authentication',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $interfaces = new Interfaces();
        $provide['interface'] = new ArrayDataProvider([
            'allModels' => $interfaces->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'type',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);


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

    public function actionUpdate() {
        $id = Yii::$app->request->get('id');
        $type = Yii::$app->request->get('type');
        $menu = new MikrotikSideMenu();
        switch ($type) {
            case 'ovpn-server':
                $model = new Ovpnbinding();
                $model->type = 'ovpn-server';
                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/ppp/ppp/index'));
                    }
                } else {
                    $model->get($id);
                }

                return $this->render('/mikrotik/ppp/binding', [
                            'menu' => $menu->getMenu(),
                            'model' => $model,
                            'action' => 'update',
                ]);
                break;
            case 'pppoe-server':
                $model = new Pppoebinding();
                $model->type = 'pppoe-server';
                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/ppp/ppp/index'));
                    }
                } else {
                    $model->get($id);
                }

                return $this->render('/mikrotik/ppp/binding', [
                            'menu' => $menu->getMenu(),
                            'model' => $model,
                            'action' => 'update',
                ]);
                break;
            case 'pptp-server':
                $model = new Pptpbinding();
                $model->type = 'pptp-server';

                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/ppp/ppp/index'));
                    }
                } else {
                    $model->get($id);
                }

                return $this->render('/mikrotik/ppp/binding', [
                            'menu' => $menu->getMenu(),
                            'model' => $model,
                            'action' => 'update',
                ]);
                break;
            case 'sstp-server':
                $model = new Sstpbinding();
                $model->type = 'sstp-server';

                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/ppp/ppp/index'));
                    }
                } else {
                    $model->get($id);
                }

                return $this->render('/mikrotik/ppp/binding', [
                            'menu' => $menu->getMenu(),
                            'model' => $model,
                            'action' => 'update',
                ]);
                break;
            case 'ppp-server':
                $model = new Pppserver();
                $model->type = 'ppp-server';

                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/ppp/ppp/index'));
                    }
                } else {
                    $model->get($id);
                }

                return $this->render('/mikrotik/ppp/pppserver', [
                            'menu' => $menu->getMenu(),
                            'model' => $model,
                            'action' => 'update',
                ]);
                break;
            case 'l2tp-server':
                $model = new L2tpbinding();
                $model->type = 'l2tp-server';

                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/ppp/ppp/index'));
                    }
                } else {
                    $model->get($id);
                }

                return $this->render('/mikrotik/ppp/binding', [
                            'menu' => $menu->getMenu(),
                            'model' => $model,
                            'action' => 'update',
                ]);
                break;
            case 'l2tp-client':
                $model = new L2tpclient();
                $model->type = 'l2tp-client';
                if ($model->load(Yii::$app->request->post()) && $model->validate()) {
                    if ($_POST['L2tpclient']['allow'] != '') {
                        $opt = implode(",", $_POST['L2tpclient']['allow']);
                        $model->allow = $opt;
                    }

                    $c = $model->update($id);
                    if ($c != '1') {
                        Yii::$app->session->setFlash('message', $c);
                    } else {
                        return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                    }
                } else {
                    $model->get($id);
                }
                $model->allow = explode(',', $model->allow);
                return $this->render('/mikrotik/ppp/clientl2tp', [
                            'menu' => $menu->getMenu(),
                            'model' => $model,
                            'action' => 'update',
                            'items' => $model->getProfiles(),
                ]);
                break;
            case 'ovpn-client':
                $model = new Ovpnclient();
                $model->type = 'ovpn-client';
                //return var_dump($model->getAll());
                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/ppp/ppp/index'));
                    }
                } else {
                    $model->get($id);
                }

                return $this->render('/mikrotik/ppp/clientovpn', [
                            'menu' => $menu->getMenu(),
                            'model' => $model,
                            'action' => 'update',
                            'key' => $model->getKeys(),
                            'profile' => $model->getProfiles(),
                ]);
                break;
            case 'pppoe-client':
                $model = new Pppoeclient();
                $model->type = 'pppoe-client';

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

                    if ($_POST['Pppoeclient']['allow'] != '') {
                        $opt = implode(",", $_POST['Pppoeclient']['allow']);
                        $model->allow = $opt;
                    }

                    $c = $model->update($id);
                    if ($c != '1') {
                        Yii::$app->session->setFlash('message', $c);
                    } else {
                        return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                    }
                } else {
                    $model->get($id);
                }
                $model->interface = explode(',', $model->interface);
                $model->allow = explode(',', $model->allow);
                return $this->render('/mikrotik/ppp/clientpppoe', [
                            'menu' => $menu->getMenu(),
                            'model' => $model,
                            'action' => 'update',
                            'interfaces' => $model->getInterfaces(),
                            'profile' => $model->getProfiles(),
                ]);
                break;
            case 'pptp-client':
                $model = new Pptpclient();
                $model->type = 'pptp-client';
                if ($model->load(Yii::$app->request->post()) && $model->validate()) {
                    if ($_POST['Pptpclient']['allow'] != '') {
                        $opt = implode(",", $_POST['Pptpclient']['allow']);
                        $model->allow = $opt;
                    }

                    $c = $model->update($id);
                    if ($c != '1') {
                        Yii::$app->session->setFlash('message', $c);
                    } else {
                        return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                    }
                } else {
                    $model->get($id);
                }
                $model->allow = explode(',', $model->allow);
                return $this->render('/mikrotik/ppp/clientpptp', [
                            'menu' => $menu->getMenu(),
                            'model' => $model,
                            'action' => 'update',
                            'profile' => $model->getProfiles(),
                ]);
                break;
            case 'sstp-client':
                $model = new Sstpclient();
                $model->type = 'sstp-client';

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

                    $c = $model->update($id);
                    if ($c != '1') {
                        Yii::$app->session->setFlash('message', $c);
                    } else {
                        return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                    }
                } else {
                    $model->get($id);
                }
                $model->authentication = explode(',', $model->authentication);
                return $this->render('/mikrotik/ppp/clientsstp', [
                            'menu' => $menu->getMenu(),
                            'model' => $model,
                            'action' => 'update',
                            'items' => $model->getProfiles(),
                            'key' => $model->getKeys(),
                ]);
                break;
            case 'ppp-client':
                $model = new Pppclient();
                $model->type = 'ppp-client';

                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/ppp/ppp/index'));
                    }
                } else {
                    $model->get($id);
                }

                return $this->render('/mikrotik/ppp/clientppp', [
                            'menu' => $menu->getMenu(),
                            'model' => $model,
                            'action' => 'update',
                            'ports' => $model->getPorts(),
                ]);
                break;
            case 'pptp':
                $model = new Pptp();

                //return var_dump($model->getAll());
                if ($model->load(Yii::$app->request->post()) && $model->validate()) {
                    if ($_POST['Pptp']['authentication'] != '') {
                        $opt = implode(",", $_POST['Pptp']['authentication']);
                        $model->authentication = $opt;
                    }
                    $c = $model->update($id);
                    if ($c != '1') {
                        Yii::$app->session->setFlash('message', $c);
                    } else {
                        return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                    }
                } else {
                    $model->get();
                }
                $model->authentication = explode(',', $model->authentication);
                return $this->render('/mikrotik/ppp/pptp', [
                            'menu' => $menu->getMenu(),
                            'model' => $model,
                            'action' => 'update',
                            'items' => $model->getProfiles(),
                ]);
                break;
            case 'l2tp':
                $model = new L2tp();

                //return var_dump($model->get());
                if ($model->load(Yii::$app->request->post()) && $model->validate()) {
                    if ($_POST['L2tp']['authentication'] != '') {
                        $opt = implode(",", $_POST['L2tp']['authentication']);
                        $model->authentication = $opt;
                    }
                    $c = $model->update($id);
                    if ($c != '1') {
                        Yii::$app->session->setFlash('message', $c);
                    } else {
                        return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                    }
                } else {
                    $model->get();
                }
                $model->authentication = explode(',', $model->authentication);
                return $this->render('/mikrotik/ppp/l2tp', [
                            'menu' => $menu->getMenu(),
                            'model' => $model,
                            'action' => 'update',
                            'items' => $model->getProfiles(),
                ]);
                break;
            case 'sstp':
                $model = new Sstp();

                // return var_dump($model->get());
                if ($model->load(Yii::$app->request->post()) && $model->validate()) {
                    if ($_POST['Sstp']['authentication'] != '') {
                        $opt = implode(",", $_POST['Sstp']['authentication']);
                        $model->authentication = $opt;
                    }
                    $c = $model->update($id);
                    if ($c != '1') {
                        Yii::$app->session->setFlash('message', $c);
                    } else {
                        return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                    }
                } else {
                    $model->get();
                }
                $model->authentication = explode(',', $model->authentication);
                return $this->render('/mikrotik/ppp/sstp', [
                            'menu' => $menu->getMenu(),
                            'model' => $model,
                            'action' => 'update',
                            'items' => $model->getProfiles(),
                            'certs' => $model->getKeys(),
                ]);
                break;
            case 'ovpn':
                $model = new Ovpn();

                //return var_dump($model->get());
                if ($model->load(Yii::$app->request->post()) && $model->validate()) {
                    if ($_POST['Ovpn']['auth'] != '') {
                        $opt = implode(",", $_POST['Ovpn']['auth']);
                        $model->auth = $opt;
                    }
                    if ($_POST['Ovpn']['cipher'] != '') {
                        $opt = implode(",", $_POST['Ovpn']['cipher']);
                        $model->cipher = $opt;
                    }
                    $c = $model->update($id);
                    if ($c != '1') {
                        Yii::$app->session->setFlash('message', $c);
                    } else {
                        return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                    }
                } else {
                    $model->get();
                }
                $model->auth = explode(',', $model->auth);
                $model->cipher = explode(',', $model->cipher);
                return $this->render('/mikrotik/ppp/ovpn', [
                            'menu' => $menu->getMenu(),
                            'model' => $model,
                            'action' => 'update',
                            'items' => $model->getProfiles(),
                            'certs' => $model->getKeys(),
                ]);
                break;
        }
        return;
    }

    public function actionCreate() {
        $type = Yii::$app->request->get('type');
        $menu = new MikrotikSideMenu();
        switch ($type) {
            case 'ovpn-server':
                $model = new Ovpnbinding();
                $model->type = 'ovpn-server';
                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/ppp/ppp/index'));
                    }
                }
                return $this->render('/mikrotik/ppp/binding', [
                            'menu' => $menu->getMenu(),
                            'model' => $model,
                            'action' => 'create',
                ]);
                break;
            case 'pppoe-server':
                $model = new Pppoebinding();
                $model->type = 'pppoe-server';
                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/ppp/ppp/index'));
                    }
                } else {
                    
                }

                return $this->render('/mikrotik/ppp/binding', [
                            'menu' => $menu->getMenu(),
                            'model' => $model,
                            'action' => 'create',
                ]);
                break;
            case 'pptp-server':
                $model = new Pptpbinding();
                $model->type = 'pptp-server';

                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/ppp/ppp/index'));
                    }
                } else {
                    
                }

                return $this->render('/mikrotik/ppp/binding', [
                            'menu' => $menu->getMenu(),
                            'model' => $model,
                            'action' => 'create',
                ]);
                break;
            case 'sstp-server':
                $model = new Sstpbinding();
                $model->type = 'sstp-server';

                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/ppp/ppp/index'));
                    }
                } else {
                    
                }

                return $this->render('/mikrotik/ppp/binding', [
                            'menu' => $menu->getMenu(),
                            'model' => $model,
                            'action' => 'create',
                ]);
                break;
            case 'ppp-server':
                $model = new Pppserver();
                $model->type = 'ppp-server';

                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/ppp/ppp/index'));
                    }
                } else {
                    
                }

                return $this->render('/mikrotik/ppp/pppserver', [
                            'menu' => $menu->getMenu(),
                            'model' => $model,
                            'action' => 'create',
                ]);
                break;
            case 'l2tp-server':
                $model = new L2tpbinding();
                $model->type = 'l2tp-server';

                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/ppp/ppp/index'));
                    }
                } else {
                    
                }

                return $this->render('/mikrotik/ppp/binding', [
                            'menu' => $menu->getMenu(),
                            'model' => $model,
                            'action' => 'create',
                ]);
                break;
            case 'l2tp-client':
                $model = new L2tpclient();
                $model->type = 'l2tp-client';
                if ($model->load(Yii::$app->request->post()) && $model->validate()) {
                    if ($_POST['L2tpclient']['allow'] != '') {
                        $opt = implode(",", $_POST['L2tpclient']['allow']);
                        $model->allow = $opt;
                    }

                    $c = $model->create();
                    if ($c != '1') {
                        Yii::$app->session->setFlash('message', $c);
                    } else {
                        return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                    }
                } else {
                    
                }
                $model->allow = explode(',', $model->allow);
                return $this->render('/mikrotik/ppp/clientl2tp', [
                            'menu' => $menu->getMenu(),
                            'model' => $model,
                            'action' => 'create',
                            'items' => $model->getProfiles(),
                ]);
                break;
            case 'ovpn-client':
                $model = new Ovpnclient();
                $model->type = 'ovpn-client';
                //return var_dump($model->getAll());
                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/ppp/ppp/index'));
                    }
                } else {
                    
                }

                return $this->render('/mikrotik/ppp/clientovpn', [
                            'menu' => $menu->getMenu(),
                            'model' => $model,
                            'action' => 'create',
                            'key' => $model->getKeys(),
                            'profile' => $model->getProfiles(),
                ]);
                break;
            case 'pppoe-client':
                $model = new Pppoeclient();
                $model->type = 'pppoe-client';

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

                    if ($_POST['Pppoeclient']['allow'] != '') {
                        $opt = implode(",", $_POST['Pppoeclient']['allow']);
                        $model->allow = $opt;
                    }

                    $c = $model->create();
                    if ($c != '1') {
                        Yii::$app->session->setFlash('message', $c);
                    } else {
                        return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                    }
                } else {
                    
                }
                $model->interface = explode(',', $model->interface);
                $model->allow = explode(',', $model->allow);
                return $this->render('/mikrotik/ppp/clientpppoe', [
                            'menu' => $menu->getMenu(),
                            'model' => $model,
                            'action' => 'create',
                            'interfaces' => $model->getInterfaces(),
                            'profile' => $model->getProfiles(),
                ]);
                break;
            case 'pptp-client':
                $model = new Pptpclient();
                $model->type = 'pptp-client';
                if ($model->load(Yii::$app->request->post()) && $model->validate()) {
                    if ($_POST['Pptpclient']['allow'] != '') {
                        $opt = implode(",", $_POST['Pptpclient']['allow']);
                        $model->allow = $opt;
                    }

                    $c = $model->create();
                    if ($c != '1') {
                        Yii::$app->session->setFlash('message', $c);
                    } else {
                        return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                    }
                } else {
                    
                }
                $model->allow = explode(',', $model->allow);
                return $this->render('/mikrotik/ppp/clientpptp', [
                            'menu' => $menu->getMenu(),
                            'model' => $model,
                            'action' => 'create',
                            'profile' => $model->getProfiles(),
                ]);
                break;
            case 'sstp-client':
                $model = new Sstpclient();
                $model->type = 'sstp-client';

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

                    $c = $model->create();
                    if ($c != '1') {
                        Yii::$app->session->setFlash('message', $c);
                    } else {
                        return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                    }
                } else {
                    
                }
                $model->authentication = explode(',', $model->authentication);
                return $this->render('/mikrotik/ppp/clientsstp', [
                            'menu' => $menu->getMenu(),
                            'model' => $model,
                            'action' => 'create',
                            'items' => $model->getProfiles(),
                            'key' => $model->getKeys(),
                ]);
                break;
            case 'ppp-client':
                $model = new Pppclient();
                $model->type = 'ppp-client';

                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/ppp/ppp/index'));
                    }
                } else {
                    
                }

                return $this->render('/mikrotik/ppp/clientppp', [
                            'menu' => $menu->getMenu(),
                            'model' => $model,
                            'action' => 'create',
                            'ports' => $model->getPorts(),
                ]);
                break;
        }
        return;
    }

    public function actionDisable() {
        $id = Yii::$app->request->get('id');
        $type = Yii::$app->request->get('type');
        switch ($type) {
            case 'ovpn-server':
                $model = new Ovpnbinding();
                $model->disable($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'pppoe-server':
                $model = new Pppoebinding();
                $model->disable($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'pptp-server':
                $model = new Pptpbinding();
                $model->disable($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'sstp-server':
                $model = new Sstpbinding();
                $model->disable($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'ppp-server':
                $model = new Pppserver();
                $model->disable($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'l2tp-server':
                $model = new L2tpbinding();
                $model->disable($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'l2tp-client':
                $model = new L2tpclient();
                $model->disable($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'ovpn-client':
                $model = new Ovpnclient();
                $model->disable($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'pppoe-client':
                $model = new Pppoeclient();
                $model->disable($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'pptp-client':
                $model = new Pptpclient();
                $model->disable($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'sstp-client':
                $model = new Sstpclient();
                $model->disable($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'ppp-client':
                $model = new Pppclient();
                $model->disable($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
        }
    }

    public function actionDelete() {
        $id = Yii::$app->request->get('id');
        $type = Yii::$app->request->get('type');

        switch ($type) {
            case 'ovpn-server':
                $model = new Ovpnbinding();
                $model->delete($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'pppoe-server':
                $model = new Pppoebinding();
                $model->delete($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'pptp-server':
                $model = new Pptpbinding();
                $model->delete($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'sstp-server':
                $model = new Sstpbinding();
                $model->delete($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'ppp-server':
                $model = new Pppserver();
                $model->delete($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'l2tp-server':
                $model = new L2tpbinding();
                $model->delete($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'l2tp-client':
                $model = new L2tpclient();
                $model->delete($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'ovpn-client':
                $model = new Ovpnclient();
                $model->delete($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'pppoe-client':
                $model = new Pppoeclient();
                $model->delete($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'pptp-client':
                $model = new Pptpclient();
                $model->delete($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'sstp-client':
                $model = new Sstpclient();
                $model->delete($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'ppp-client':
                $model = new Pppclient();
                $model->delete($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
        }
    }

    public function actionEnable() {
        $id = Yii::$app->request->get('id');
        $type = Yii::$app->request->get('type');
        switch ($type) {
            case 'ovpn-server':
                $model = new Ovpnbinding();
                $model->enable($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'pppoe-server':
                $model = new Pppoebinding();
                $model->enable($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'pptp-server':
                $model = new Pptpbinding();
                $model->enable($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'sstp-server':
                $model = new Sstpbinding();
                $model->enable($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'ppp-server':
                $model = new Pppserver();
                $model->enable($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'l2tp-server':
                $model = new L2tpbinding();
                $model->enable($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'l2tp-client':
                $model = new L2tpclient();
                $model->enable($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'ovpn-client':
                $model = new Ovpnclient();
                $model->enable($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'pppoe-client':
                $model = new Pppoeclient();
                $model->enable($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'pptp-client':
                $model = new Pptpclient();
                $model->enable($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'sstp-client':
                $model = new Sstpclient();
                $model->enable($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
            case 'ppp-client':
                $model = new Pppclient();
                $model->enable($id);
                return $this->redirect(Url::toRoute('mikrotik/ppp/ppp/index'));
                break;
        }
    }

}
