<?php

namespace app\controllers\mikrotik\switchm;

use yii\helpers\Url;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\mikrotik\switchm\Switchm;
use yii\data\ArrayDataProvider;
use app\models\mikrotik\MikrotikSideMenu;
use app\models\mikrotik\interfaces\Ethernet;
use app\models\mikrotik\switchm\Port;
use app\models\mikrotik\switchm\Host;
use app\models\mikrotik\switchm\Vlan;
use app\models\mikrotik\switchm\Rule;

class SwitchmController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_switchViewSwitch'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_switchUpdateSwitch'],
                    ],
                    [
                        '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 Switchm();
        $switch = $model->getAll();

        $provide['switch'] = new ArrayDataProvider([
            'allModels' => $switch,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'type',
                    'mirror_source',
                    'mirror_target',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $port = new Port();
        $por = $port->getAll();

        $provide['port'] = new ArrayDataProvider([
            'allModels' => $por,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'switch',
                    'vlan_mode',
                    'vlan_header',
                    'default_vlan_id',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $host = new Host();
        $hosts = $host->getAll();

        $provide['host'] = new ArrayDataProvider([
            'allModels' => $hosts,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'switch',
                    'mac_address',
                    'ports',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $vlan = new Vlan();
        $vlans = $vlan->getAll();

        $provide['vlan'] = new ArrayDataProvider([
            'allModels' => $vlans,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'switch',
                    'vlan_id',
                    'ports',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $rule = new Rule();
        $rules = $rule->getAll();

        $provide['rule'] = new ArrayDataProvider([
            'allModels' => $rules,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'switch',
                    'ports',
                    'src_mac_address',
                    'dst_mac_address',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

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

    public function actionUpdate() {
        $id = Yii::$app->request->get('id');
        $menu = new MikrotikSideMenu();
        $model = new Switchm();

        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/switchm/switchm/index'));
            }
        } else {
            $model->get($id);
        }
        $eth = new Ethernet();
        $mirrorsrc = $eth->getNames();
        $mirrorsrc['none'] = '';
        $mirrortarget = $mirrorsrc;
        $mirrortarget['switch1cpu'] = 'switch1 cpu';
        return $this->render('/mikrotik/switchm/switchm', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'mirrorsrc' => $mirrorsrc,
                    'mirrortarget' => $mirrortarget,
        ]);
    }

}
