<?php

namespace app\controllers\mikrotik\system;

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\system\Scripts;
use app\models\mikrotik\system\Jobs;

class ScriptsController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemViewScripts'],
                    ],
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemCreateScripts'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemDeleteScripts'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemDisableScripts'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemEnableScripts'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemUpdateScripts'],
                    ],
                    [
                        '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();
        $scep = new Scripts();


        $provide['scep'] = new ArrayDataProvider([
            'allModels' => $scep->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'owner',
                    'run_count',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        $j = new Jobs();
        $provide['jobs'] = new ArrayDataProvider([
            'allModels' => $j->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'started',
                    'owner',
                    'type',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

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

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

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            if ($_POST['Scripts']['policy'] != '') {
                $opt = implode(",", $_POST['Scripts']['policy']);
                $model->policy = $opt;
            }
            $c = $model->update($id);
            if ($c != '1') {
                Yii::$app->session->setFlash('message', $c);
            } else {
                return $this->redirect(Url::toRoute('mikrotik/system/scripts/index'));
            }
        } else {
            $model->get($id);
        }
        $model->policy = explode(',', $model->policy);
        return $this->render('/mikrotik/system/scripts', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'update',
        ]);
    }

    public function actionCreate() {

        $menu = new MikrotikSideMenu();
        $model = new Scripts();

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            if ($_POST['Scripts']['policy'] != '') {
                $opt = implode(",", $_POST['Scripts']['policy']);
                $model->policy = $opt;
            }
            $c = $model->create();
            if ($c != '1') {
                Yii::$app->session->setFlash('message', $c);
            } else {
                return $this->redirect(Url::toRoute('mikrotik/system/scripts/index'));
            }
        }

        return $this->render('/mikrotik/system/scripts', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'create',
        ]);
    }

    public function actionRun() {
        $id = Yii::$app->request->get('id');
        $model = new Scripts();
        $model->run($id);
        return $this->redirect(Url::toRoute('mikrotik/system/scripts/index'));
    }

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

}
