<?php

namespace app\controllers\mikrotik\ip\firewall;

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\firewall\Filter;
use app\models\mikrotik\ip\firewall\Addresslists;
use app\models\mikrotik\ip\firewall\Layer7protocols;
use app\models\mikrotik\ip\firewall\Mangle;
use app\models\mikrotik\Interfaces;

class AddresslistsController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'except' => ['index'],
                'rules' => [
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipCreateFirewallAddresslists'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDeleteFirewallAddresslists'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDisableFirewallAddresslists'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipEnableFirewallAddresslists'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipUpdateFirewallAddresslists'],
                    ],
                    [
                        '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 Addresslists();


//        $model = new Accounting(['scenario' => 'update']);
//        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
//
//            $model->update();
//
//            return $this->redirect(Url::toRoute('mikrotik/ip/accounting/index'));
//        }
//        $model->getAll();
//
//        return $this->render('/mikrotik/ip/firewall/filter/index', [
//                    'menu' => $menu->getMenu(),
//                    'model' => $model,
//        ]);
    }

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

            $model->update($id);
            return $this->redirect(Url::toRoute('mikrotik/ip/firewall/firewall/index'));
        }
        $menu = new MikrotikSideMenu();
        $model->get($id);
        return $this->render('/mikrotik/ip/firewall/addresslists/index', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'update',
        ]);
    }

    public function actionCreate() {
        $model = new Addresslists();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $model->create();
            return $this->redirect(Url::toRoute('mikrotik/ip/firewall/firewall/index'));
        }
        $menu = new MikrotikSideMenu();
        return $this->render('/mikrotik/ip/firewall/addresslists/index', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'create',
        ]);
    }

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

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

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

}
