<?php

namespace app\controllers\mikrotik\ip\packing;

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\packing\Packing;
use app\models\mikrotik\Interfaces;

class PackingController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipViewPacking'],
                    ],
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipCreatePacking'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDeletePacking'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDisablePacking'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipEnablePacking'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipUpdatePacking'],
                    ],
                    [
                        '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 Packing();
        $provider = $model->getAll();

        $provide = new ArrayDataProvider([
            'allModels' => $provider,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'interface',
                    'packing',
                    'unpacking',
                    'aggregated_size',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

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

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

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

            $model->create();
            return $this->redirect(Url::toRoute('mikrotik/ip/packing/packing/index'));
        }
        $intName = $interfaces->names();
        foreach ($intName as $value) {
            $intNames[$value] = $value;
        }
        return $this->render('/mikrotik/ip/packing/update', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'intName' => $intNames,
                    'action' => 'create',
        ]);
    }

    public function actionUpdate() {
        $menu = new MikrotikSideMenu();
        $model = new Packing();
        $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/packing/packing/index'));
        }
        $interfaces = new Interfaces();
        $intName = $interfaces->names();
        foreach ($intName as $value) {
            $intNames[$value] = $value;
        }

        $model->get($id);
        return $this->render('/mikrotik/ip/packing/update', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'intName' => $intNames,
                    'action' => 'update',
        ]);
    }

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

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

}
