<?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 app\models\mikrotik\MikrotikSideMenu;
use app\models\mikrotik\ip\dhcpserver\Dhcpserverleases;
use app\models\mikrotik\ip\dhcpserver\Dhcpserveroptions;
use app\models\mikrotik\ip\Pool;

class DhcpserverleasesController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'except' => ['index'],
                'rules' => [
                     [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipCreateDHCPServerLeases'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDeleteDHCPServerLeases'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDisableDHCPServerLeases'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipEnableDHCPServerLeases'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipUpdateDHCPServerLeases'],
                    ],
                    [
                        '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 Dhcpserverleases();
        $provider = $model->getAllTest();
    }

    public function actionCreate() {
        $menu = new MikrotikSideMenu();
        $model = new Dhcpserverleases();
        $options = new Dhcpserveroptions();
        $items = $options->getNames();
        $pool = new Pool();
        $poolNames = $pool->getNames();

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

                $model->dhcp_option = $opt;
            }

            $c = $model->create();
            if ($c != '1') {
                Yii::$app->session->setFlash('message', $c);
            } else {
                return $this->redirect(Url::toRoute(('mikrotik/ip/dhcpserver/dhcpserver/index')));
            }
        }
        $model->dhcp_option = explode(',', $model->dhcp_option);

        return $this->render('/mikrotik/ip/dhcpserver/createlease', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'items_option' => $items,
                    'pool' => $poolNames,
        ]);
    }

    public function actionUpdate() {
        $id = Yii::$app->request->get('id');
        $menu = new MikrotikSideMenu();
        $model = new Dhcpserverleases();
        $options = new Dhcpserveroptions();
        $items = $options->getNames();
        $pool = new Pool();
        $poolNames = $pool->getNames();

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

                $model->dhcp_option = $opt;
            }
            $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);
        }
        $model->dhcp_option = explode(',', $model->dhcp_option);
        return $this->render('/mikrotik/ip/dhcpserver/updatelease', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'items_option' => $items,
                    'pool' => $poolNames,
        ]);
    }

    public function actionDelete() {
        $id = Yii::$app->request->get('id');
        $model = new Dhcpserverleases();
        $model->delete($id);

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

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

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

}
