<?php

namespace app\controllers\mikrotik\ip\dhcpclient;

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\dhcpclient\Dhcpclient;
use app\models\mikrotik\Interfaces;
use app\models\mikrotik\ip\dhcpclient\Dhcpclientoption;

class DhcpclientController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipViewDHCPClient'],
                    ],
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipCreateDHCPClient'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDeleteDHCPClient'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDisableDHCPClient'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipEnableDHCPClient'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipUpdateDHCPClient'],
                    ],
                    [
                        'actions' => ['release'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipReleaseDHCPClient'],
                    ],
                    [
                        'actions' => ['renew'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipRenewDHCPClient'],
                    ],
                    [
                        '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();
        $options = new Dhcpclientoption();
        $model = new Dhcpclient();
        $provider = $model->getAll();

        $provide['DHCP Client Options'] = new ArrayDataProvider([
            'allModels' => $options->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'code',
                    'value',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $provide['DHCP Client'] = new ArrayDataProvider([
            'allModels' => $provider,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'interface',
                    'address',
                    'expires_after',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        return $this->render('/mikrotik/ip/dhcpclient/index', [
                    'provider' => $provide,
                    'menu' => $menu->getMenu(),
                    'model' => $model,
        ]);
    }

    public function actionCreate() {
        $menu = new MikrotikSideMenu();
        $model = new Dhcpclient();
        $inter = new Interfaces();
        $items = $inter->getNames();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $model->interface = $items[$model->interface];
            $opt = implode(",", $_POST['Dhcpclient']['dhcp_options']);
            $model->dhcp_options = $opt;
            $model->create();
            return $this->redirect(Url::toRoute('mikrotik/ip/dhcpclient/dhcpclient/index'));
        }
        $options = $model->getNames();
        $model->dhcp_options = explode(',', $model->dhcp_options);
        return $this->render('/mikrotik/ip/dhcpclient/create', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'items' => $items,
                    'items_option' => $options,
        ]);
    }

    public function actionUpdate() {
        $id = Yii::$app->request->get('id');
        $menu = new MikrotikSideMenu();
        $model = new Dhcpclient();
        $inter = new Interfaces();
        $items = $inter->getNames();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $model->interface = $items[$model->interface];
            $dump = implode(",", $_POST['Dhcpclient']['dhcp_options']);
            $model->dhcp_options = $dump;
            $model->update($id);

            return $this->redirect(Url::toRoute('mikrotik/ip/dhcpclient/dhcpclient/index'));
        }
        $model->getDHCP($id);
        $flipped = array_flip($items);
        $x = $flipped[$model->interface];

        if ($model->disabled == 'false') {
            $action = 'disable';
        } else {
            $action = 'enable';
        }
        $options = $model->getNames();
        $model->dhcp_options = explode(',', $model->dhcp_options);

        return $this->render('/mikrotik/ip/dhcpclient/update', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'items' => $items,
                    'x' => $x,
                    'action' => $action,
                    'items_option' => $options,
        ]);
    }

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

    public function actionRelease() {
        $id = Yii::$app->request->get('id');
        $model = new Dhcpclient();
        $model->release($id);
        return $this->redirect(Url::toRoute('mikrotik/ip/dhcpclient/dhcpclient/index'));
    }

    public function actionRenew() {
        $id = Yii::$app->request->get('id');
        $model = new Dhcpclient();
        $model->renew($id);
        return $this->redirect(Url::toRoute('mikrotik/ip/dhcpclient/dhcpclient/index'));
    }

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

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

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

        $menu = new MikrotikSideMenu();
        $model = new Dhcpclient();

        $model->getDHCP($id);

        return $this->render('/mikrotik/ip/dhcpclient/view', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
        ]);
    }

    public function actionCreateoption() {
        $menu = new MikrotikSideMenu();
        $model = new Dhcpclientoption();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {

            $model->create();
            return $this->redirect(Url::toRoute('mikrotik/ip/dhcpclient/dhcpclient/index'));
        }

        return $this->render('/mikrotik/ip/dhcpclient/createoption', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
        ]);
    }

}
