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

class ConnectionsController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipViewWebproxyConnection'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDeleteWebproxyConnection'],
                    ],
                    [
                        '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 Connections();

        $provider = $model->getAll();
        $provide = new ArrayDataProvider([
            'allModels' => $provider,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'dst_address',
                    'protocol',
                    'rx_bytes',
                    'src_address',
                    'state',
                    'tx_bytes',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);


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

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

}
