<?php

namespace app\controllers\mikrotik\ip\pool;

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\pool\Pool;

class PoolController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipViewPool'],
                    ],
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipCreatePool'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDeletePool'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDisablePool'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipEnablePool'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipUpdatePool'],
                    ],
                    [
                        '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();
        return $this->render('/mikrotik/ip/pool/index', [
                    'menu' => $menu->getMenu(),
        ]);
    }

    public function actionPool() {
        $menu = new MikrotikSideMenu();
        $pool = new Pool();
        $provider['Pools'] = $pool->getAll();
        $tab = ['Pools'];
        foreach ($tab as $value) {
            $provide[$value] = new ArrayDataProvider([
                'allModels' => $provider[$value],
                'key' => 'name',
                'sort' => [
                    'attributes' => [
                        'name',
                        'ranges',
                    ],
                ],
                'pagination' => [
                    'pageSize' => 20,
                ],
            ]);
        }

        return $this->render('/mikrotik/ip/pool/pool', [
                    'provider' => $provide,
                    'menu' => $menu->getMenu(),
                    'ret' => $provider['Pools'],
        ]);
    }

    public function actionCreate() {
        $menu = new MikrotikSideMenu();
        $model = new Pool();
        $items = $model->GetPoolNames();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $model->getPoolNames($model->next_pool);
            $model->createPool();
            return $this->redirect(Url::toRoute('mikrotik/ip/pool/pool/pool'));
        }
        return $this->render('/mikrotik/ip/pool/poolCreate', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'items' => $items,
        ]);
    }

    public function actionUpdate() {
        $id = Yii::$app->request->get('id');

        $menu = new MikrotikSideMenu();
        $model = new Pool();
        $items = $model->GetPoolNames();
        $x = 0;
        $rng2 = Yii::$app->request->post('ranges2', '');
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $model->getPoolNames($model->next_pool);
            $model->setPool($id);

            return $this->redirect(Url::toRoute('mikrotik/ip/pool/pool/pool'));
        }
        $model->getPool($id);
        foreach ($items as $key => $value) {
            if ($value == $model->next_pool) {
                $x = $key;
                continue;
            }
        }
        return $this->render('/mikrotik/ip/pool/update', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'items' => $items,
                    'x' => $x,
        ]);
    }

    public function actionDelete() {
        $name = Yii::$app->request->get('id');
        $model = new Pool();
        $model->deletePool($name);
        return $this->redirect(Url::toRoute('mikrotik/ip/pool/pool/pool'));
    }

}
