<?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\Dhcpserveralerts;
use app\models\mikrotik\ip\Pool;
use app\models\mikrotik\Interfaces;

class DhcpserveralertsController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'except' => ['index'],
                'rules' => [
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipCreateDHCPServerAlerts'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDeleteDHCPServerAlerts'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDisableDHCPServerAlerts'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipEnableDHCPServerAlerts'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipUpdateDHCPServerAlerts'],
                    ],
                    [
                        '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 actionCreatealert() {
        $menu = new MikrotikSideMenu();
        $inter = new Interfaces();
        $items = $inter->getNames();
        $model = new Dhcpserveralerts();
        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/createalert', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'items' => $items,
        ]);
    }

    public function actionUpdate() {
        $id = Yii::$app->request->get('id');
        $menu = new MikrotikSideMenu();
        $model = new Dhcpserveralerts();
        $inter = new Interfaces();
        $items = $inter->getNames();
        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];

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

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

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

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

}
