<?php

namespace app\controllers\mikrotik\ip\accounting;

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\accounting\Accounting;

class AccountingController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'except' => ['index'],
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipUpdateAccounting'],
                    ],
                    [
                        'actions' => ['webaccess'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipUpdateAccountingWebaccess'],
                    ],
                    [
                        '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 Accounting(['scenario' => 'update']);
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {

            $model->update();

            return $this->redirect(Url::toRoute('mikrotik/ip/accounting/accounting/index'));
        }
        $model->getAll();

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

    public function actionWebaccess() {

        $menu = new MikrotikSideMenu();
        $model = new Accounting(['scenario' => 'actionWebaccess']);


        if ($model->load(Yii::$app->request->post()) && $model->validate()) {

            $model->updateWeb();
            return $this->redirect(Url::toRoute('mikrotik/ip/accounting/accounting/webaccess'));
        }
        $model->getAllWeb();
        return $this->render('/mikrotik/ip/accounting/webAccess', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
        ]);
    }

}
