<?php

namespace app\controllers\mikrotik\ip\services;

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\services\Services;

class ServicesController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipViewServices'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDisableServices'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipEnableServices'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipUpdateServices'],
                    ],
                    [
                        '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 Services();
        $provider = $model->getAll();

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


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

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

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

    public function actionUpdate() {
        $menu = new MikrotikSideMenu();
        $model = new Services();
        $id = Yii::$app->request->get('id');
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {

            $model->update($id);
            return $this->redirect(Url::toRoute('mikrotik/ip/services/services/index'));
        }

        $model->get($id);

        return $this->render('/mikrotik/ip/services/service', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
        ]);
    }

}
