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

class DhcpservernetworkController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'except' => ['index'],
                'rules' => [
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipCreateDHCPServerNetworks'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDeleteDHCPServerNetworks'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipUpdateDHCPServerNetworks'],
                    ],
                    [
                        '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 Dhcpservernetwork();
        $provider = $model->getAllTest();
    }

    public function actionCreate() {
        $menu = new MikrotikSideMenu();
        $model = new Dhcpservernetwork();
        $options = new Dhcpserveroptions();
        $items = $options->getNames();
        $d = null;
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $opt = $_POST['Dhcpservernetwork']['dhcp_option'];
            if ($opt != null) {
                $opt = implode(",", $opt);
            }
            $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/createnetwork', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'items' => $items,
                    'x' => $d,
        ]);
    }

    public function actionUpdate() {
        $id = Yii::$app->request->get('id');
        $menu = new MikrotikSideMenu();
        $model = new Dhcpservernetwork();
        $options = new Dhcpserveroptions();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $opt = $_POST['Dhcpservernetwork']['dhcp_option'];
            if ($opt != null) {
                $opt = implode(",", $opt);
            }
            $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);
        $items = $options->getNames();
        $x = null;

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

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

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

}
