<?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\Scheduler;

class SchedulerController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                     [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemViewScheduler'],
                    ],
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemCreateScheduler'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemDeleteScheduler'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemDisableScheduler'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemEnableScheduler'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_systemUpdateScheduler'],
                    ],
                    [
                        '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 Scheduler();
        $provide['scep'] = new ArrayDataProvider([
            'allModels' => $scep->getAll(),
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'start_date',
                    'start_time',
                    'interval',
                    'on_event',
                    'run_count',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

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

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

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            if ($_POST['Scheduler']['policy'] != '') {
                $opt = implode(",", $_POST['Scheduler']['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/scheduler/index'));
            }
        } else {
            $model->get($id);
        }
        $model->policy = explode(',', $model->policy);
        return $this->render('/mikrotik/system/scheduler', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'action' => 'update',
        ]);
    }

    public function actionCreate() {

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

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

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

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

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

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

}
