<?php

namespace app\controllers\mikrotik\ip\dns;

require_once 'PEAR2_Net_RouterOS-1.0.0b5.phar';

use Yii;
use yii\helpers\Url;
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\ip\dns\Dns;
use app\models\mikrotik\ip\dns\Pool;
use app\models\mikrotik\Interfaces;

class DnsController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipViewDNS'],
                    ],
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipCreateDNS'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDeleteDNS'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDisableDNS'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipEnableDNS'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipUpdateDNS'],
                    ],
                    [
                        'actions' => ['config'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipConfigDNS'],
                    ],
                    [
                        'actions' => ['resetcounters'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipResetcountersDNS'],
                    ],
                    [
                        '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 Dns();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $model->update();
            //return $this->redirect(Url::toRoute('mikrotik/ip/dns/dns/index'));
        }
        $model->getAll();
        $model->dynamic_servers = explode(",", $model->dynamic_servers);
        return $this->render('/mikrotik/ip/dns/index', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
        ]);
    }

    public function actionCreate() {
        $menu = new MikrotikSideMenu();
        $model = new Dhcpserver();
        $inter = new Interfaces();
        $items = $inter->getNames();
        $pool = new Pool();
        $poolNames = $pool->getNames();
        $poolNames['static-only'] = 'static-only';
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $model->interface = $items[$model->interface];
            $ret = $model->create();
            return $this->redirect(Url::toRoute('mikrotik/ip/dns/dns/index'));
        }

        return $this->render('create', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'items' => $items,
                    'pool' => $poolNames,
        ]);
    }

    public function actionUpdate() {
        $id = Yii::$app->request->get('id');
        $menu = new MikrotikSideMenu();
        $model = new Dhcpserver();
        $inter = new Interfaces();
        $items = $inter->getNames();
        $pool = new Pool();
        $poolNames = $pool->getNames();
        $poolNames['static-only'] = 'static-only';
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $model->interface = $items[$model->interface];

            $model->update($id);

            return $this->redirect(Url::toRoute('mikrotik/ip/dns/dns/index'));
        }
        $model->get($id);


        $flipped = array_flip($items);
        $x = $flipped[$model->interface];

        if ($model->disabled == false) {
            $action = 'disable';
        } else {
            $action = 'enable';
        }
        return $this->render('/mikrotik/ip/dns/update', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'items' => $items,
                    'x' => $x,
                    'action' => $action,
                    'pool' => $poolNames,
        ]);
    }

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

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

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

    public function actionResetcounters() {
        $id = Yii::$app->request->get('id');
        $model = new Dhcpserver();
        $model->reset($id);
        return $this->redirect(Url::toRoute('mikrotik/ip/dns/dns/index'));
    }

    public function actionConfig() {
        $menu = new MikrotikSideMenu();
        $model = new Dhcpserverconfig();

        if (isset($_POST['Dhcpserveroptions']['store_leases_disk'])) {
            $model->store_leases_disk = $_POST['Dhcpserveroptions']['store_leases_disk'];
            $model->update();

            return $this->redirect(Url::toRoute('mikrotik/ip/dns/dns/index'));
        }
        $model->get();
        return $this->render('/mikrotik/ip/dns/config', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
        ]);
    }

}
