<?php

namespace app\controllers\mikrotik\ip\smb;

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\smb\Smb;
use app\models\mikrotik\Interfaces;

class SmbController extends Controller {

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

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $opt = implode(",", $_POST['Smb']['interfaces']);
            $model->interfaces = $opt;
            $model->update();
            return $this->redirect(Url::toRoute('mikrotik/ip/smb/smb/index'));
        }
        $model->getAll();
        $interfaces = new Interfaces();
        $intName = $interfaces->names();
        $intNames['all'] = 'all';
        foreach ($intName as $value) {
            $intNames[$value] = $value;
        }
        $model->interfaces = explode(',', $model->interfaces);

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

}
