<?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 DhcpclientoptionsController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'except' => ['index'],
                'rules' => [
                    [
                        'actions' => ['createoption'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipCreateDHCPClientOptions'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDeleteDHCPClientOptions'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDisableDHCPClientOptions'],
                    ],
                    [
                        '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 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,
        ]);
    }

    public function actionUpdate() {
        $id = Yii::$app->request->get('id');
        $menu = new MikrotikSideMenu();
        $model = new Dhcpclientoption();

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $model->update($id);
            return $this->redirect(Url::toRoute('mikrotik/ip/dhcpclient/dhcpclient/index'));
        }

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

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

}
