<?php

namespace app\controllers\mikrotik\wireless;

use yii\helpers\Url;
use Yii;
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\wireless\Accesslist;
use app\models\mikrotik\wireless\Channels;
use app\models\mikrotik\wireless\Connectlist;
use app\models\mikrotik\wireless\Interfaces;
use app\models\mikrotik\wireless\Wds;
use app\models\mikrotik\wireless\Registration;
use app\models\mikrotik\wireless\Securityprofiles;
use app\models\mikrotik\wireless\Cap;

//use app\models\mikrotik\Interfaces;

class WirelessController extends Controller {

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

        $accesslist = new Accesslist();
        $provide['accesslist'] = new ArrayDataProvider([
            'allModels' => $accesslist->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'mac_address',
                    'interface',
                    'signal_range',
                    'authentication',
                    'forwarding',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $channels = new Channels();
        $provide['channels'] = new ArrayDataProvider([
            'allModels' => $channels->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'list',
                    'name',
                    'frequency',
                    'width',
                    'band',
                    'extension_channel',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $connectlist = new Connectlist();
        $provide['connectlist'] = new ArrayDataProvider([
            'allModels' => $connectlist->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'interface',
                    'mac_address',
                    'connect',
                    'area_prefix',
                    'signal_range',
                    'security_profile',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $interfaces = new Interfaces();
        $provide['interfaces'] = new ArrayDataProvider([
            'allModels' => $interfaces->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'interface_type',
                    'l2mtu',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $wds = new Wds();
        $provide['wds'] = new ArrayDataProvider([
            'allModels' => $wds->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'type',
                    'mtu'
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $registration = new Registration();
        $provide['registration'] = new ArrayDataProvider([
            'allModels' => $registration->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'radio_name',
                    'mac_address',
                    'interface',
                    'uptime',
                    'ap',
                    'wds',
                    'last_activity',
                    'tx_rate',
                    'rx_rate',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $securityprofiles = new Securityprofiles();
        $provide['securityprofiles'] = new ArrayDataProvider([
            'allModels' => $securityprofiles->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'mode',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);


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

    public function actionCap() {
        $model = new Cap();
        $menu = new MikrotikSideMenu();
        $model->get();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            if ($_POST['Cap']['interfaces'] != '') {
                $opt = implode(",", $_POST['Cap']['interfaces']);
                $model->interfaces = $opt;
            }
            if ($_POST['Cap']['discovery_interfaces'] != '') {
                $opt = implode(",", $_POST['Cap']['discovery_interfaces']);
                $model->discovery_interfaces = $opt;
            }
            $c = $model->update();
            if ($c != '1') {
                Yii::$app->session->setFlash('message', $c);
            } else {
                return $this->redirect(Url::toRoute('mikrotik/wireless/wireless/index'));
            }
        }
        $model->interfaces = explode(',', $model->interfaces);
        $model->discovery_interfaces = explode(',', $model->discovery_interfaces);
        return $this->render('/mikrotik/wireless/cap', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'create',
                    'key' => $model->getKeys(),
                    'items' => $model->getWireless(),
                    'discovery' => $model->getDiscovery(),
                    'bridge' => $model->getBridge(),
        ]);
    }

}
