<?php

namespace app\controllers\mikrotik\ip\webproxy;

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\webproxy\Access;
use app\models\mikrotik\Interfaces;

class AccessController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipViewWebproxyAccess'],
                    ],
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipCreateWebproxyAccess'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDeleteWebproxyAccess'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDisableWebproxyAccess'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipEnableWebproxyAccess'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipUpdateWebproxyAccess'],
                    ],
                    [
                        '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 Access();

        $provider = $model->getAll();
        $provide = new ArrayDataProvider([
            'allModels' => $provider,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'action',
                    'dst_address',
                    'dst_host',
                    'dst_port',
                    'local_port',
                    'method',
                    'path',
                    'redirect_to',
                    'src_address',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);


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

    public function actionCreate() {
        $menu = new MikrotikSideMenu();
        $model = new Access();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {

            $c = $model->create();
            if ($c != '1') {
                Yii::$app->session->setFlash('message', $c);
            } else {
                return $this->redirect(Url::toRoute('mikrotik/ip/webproxy/access/index'));
            }
        }

        return $this->render('/mikrotik/ip/webproxy/accesscreate', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'create',
        ]);
    }

    public function actionUpdate() {
        $id = Yii::$app->request->get('id');
        $menu = new MikrotikSideMenu();
        $model = new Access();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $c = $model->update($id);
            if ($c != '1') {
                Yii::$app->session->setFlash('message', $c);
            } else {
                return $this->redirect(Url::toRoute('mikrotik/ip/webproxy/access/index'));
            }
        } else {
            $model->get($id);
        }
        return $this->render('/mikrotik/ip/webproxy/accesscreate', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'update',
        ]);
    }

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

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

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

}
