<?php

namespace app\controllers\mikrotik\ip\dhcprelay;

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\dhcprelay\Dhcprelay;
use app\models\mikrotik\Interfaces;

class DhcprelayController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipViewDHCPRelay'],
                    ],
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipCreateDHCPRelay'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDeleteDHCPRelay'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDisableDHCPRelay'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipEnableDHCPRelay'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipUpdateDHCPRelay'],
                    ],
                    [
                        'actions' => ['resetcounters'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipResetcountersDHCPRelay'],
                    ],
                    [
                        '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();
        $model = new Dhcprelay();

        $provider = $model->getAll();

        $provide = new ArrayDataProvider([
            'allModels' => $provider,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'interface',
                    'dhcp_server',
                    'local_address',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        return $this->render('/mikrotik/ip/dhcprelay/index', [
                    'provider' => $provide,
                    'menu' => $menu->getMenu(),
                    'model' => $model,
        ]);
    }

    public function actionCreate() {
        $menu = new MikrotikSideMenu();
        $model = new Dhcprelay();
        $inter = new Interfaces();
        $items = $inter->getNames();
        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/dhcprelay/dhcprelay/index'));
            }
        }
        return $this->render('/mikrotik/ip/dhcprelay/create', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'items' => $items,
        ]);
    }

    public function actionUpdate() {
        $id = Yii::$app->request->get('id');
        $menu = new MikrotikSideMenu();
        $model = new Dhcprelay();
        $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/dhcprelay/dhcprelay/index'));
            }
        } else {
            $model->get($id);
        }

        $flipped = array_flip($items);
        $x = $flipped[$model->interface];

        if ($model->disabled == false) {
            $action = 'disable';
        } else {
            $action = 'enable';
        }
        return $this->render('/mikrotik/ip/dhcprelay/update', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'items' => $items,
                    'x' => $x,
                    'action' => $action,
        ]);
    }

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

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

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

    public function actionResetcounters() {
        $id = Yii::$app->request->get('id');
        $model = new Dhcprelay();
        $model->reset($id);
        return $this->redirect(Url::toRoute('mikrotik/ip/dhcprelay/dhcprelay/index'));
    }

}
