<?php

namespace app\controllers\mikrotik\ip\dhcpserver;

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\dhcpserver\Dhcpserver;
use app\models\mikrotik\ip\dhcpserver\Dhcpservernetwork;
use app\models\mikrotik\ip\dhcpserver\Dhcpserverleases;
use app\models\mikrotik\ip\dhcpserver\Dhcpclientoptions;
use app\models\mikrotik\ip\dhcpserver\Dhcpserveroptions;
use app\models\mikrotik\ip\dhcpserver\Dhcpserverconfig;
use app\models\mikrotik\ip\dhcpserver\Dhcpserveralerts;
use app\models\mikrotik\ip\Pool;
use app\models\mikrotik\Interfaces;

class DhcpserverController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipViewDHCPServer'],
                    ],
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipCreateDHCPServer'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDeleteDHCPServer'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDisableDHCPServer'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipEnableDHCPServer'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipUpdateDHCPServer'],
                    ],
                    [
                        'actions' => ['config'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipConfigDHCPServer'],
                    ],
                    [
                        'actions' => ['resetcounters'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipResetcountersDHCPServer'],
                    ],
                    [
                        '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 Dhcpserver();
        $network = new Dhcpservernetwork();
        $leases = new Dhcpserverleases();
        $options = new Dhcpserveroptions();
        $alerts = new Dhcpserveralerts();
        $provider['dhcp'] = $model->getAll();
        $provider['network'] = $network->getAll();
        $provider['leases'] = $leases->getAll();
        $provider['options'] = $options->getAll();
        $provider['alerts'] = $alerts->getAll();
        $provide['dhcp'] = new ArrayDataProvider([
            'allModels' => $provider['dhcp'],
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'interface',
                    'dhcp_server',
                    'relay',
                    'address_pool',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $provide['networks'] = new ArrayDataProvider([
            'allModels' => $provider['network'],
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'address',
                    'gateway',
                    'dns_server',
                    'domain',
                    'wins_server',
                    'next_server',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $provide['leases'] = new ArrayDataProvider([
            'allModels' => $provider['leases'],
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'address',
                    'mac_address',
                    'status',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $provide['options'] = new ArrayDataProvider([
            'allModels' => $provider['options'],
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'code',
                    'value',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $provide['alerts'] = new ArrayDataProvider([
            'allModels' => $provider['alerts'],
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'interface',
                    'alert_timeout',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        return $this->render('/mikrotik/ip/dhcpserver/index', [
                    'provider' => $provide,
                    '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];
            $c = $model->create();
            if ($c != '1') {
                Yii::$app->session->setFlash('message', $c);
            } else {
                return $this->redirect(Url::toRoute(('mikrotik/ip/dhcpserver/dhcpserver/index')));
            }
        }

        return $this->render('/mikrotik/ip/dhcpserver/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];

            $c = $model->update($id);
            if ($c != '1') {
                Yii::$app->session->setFlash('message', $c);
            } else {
                return $this->redirect(Url::toRoute(('mikrotik/ip/dhcpserver/dhcpserver/index')));
            }
        } else {
            $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/dhcpserver/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/dhcpserver/dhcpserver/index')));
    }

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

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

    public function actionResetcounters() {
        $id = Yii::$app->request->get('id');
        $model = new Dhcpserver();
        $model->reset($id);
        return $this->redirect(Url::toRoute('mikrotik/ip/dhcpserver/dhcpserver/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/dhcpserver/dhcpserver/index'));
        }
        $model->get();
        return $this->render('/mikrotik/ip/dhcpserver/config', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
        ]);
    }

}
