<?php

namespace app\controllers\mikrotik\ip\dns;

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\dns\Dnsstatic;
use app\models\mikrotik\ip\dns\Pool;
use app\models\mikrotik\Interfaces;

class DnsstaticController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipViewDNSStatic'],
                    ],
                    [
                        'actions' => ['create'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipCreateDNSStatic'],
                    ],
                    [
                        'actions' => ['delete'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDeleteDNSStatic'],
                    ],
                    [
                        'actions' => ['disable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipDisableDNSStatic'],
                    ],
                    [
                        'actions' => ['enable'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipEnableDNSStatic'],
                    ],
                    [
                        'actions' => ['update'],
                        'allow' => true,
                        'roles' => ['mikrotik_ipUpdateDNSStatic'],
                    ],
                    [
                        '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 Dnsstatic();
        $provider = $model->getAll();
        $provide = new ArrayDataProvider([
            'allModels' => $provider,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'name',
                    'address',
                    'ttl',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        return $this->render('/mikrotik/ip/dns/static', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
                    'provider' => $provide,
        ]);
    }

    public function actionCreate() {
        $menu = new MikrotikSideMenu();
        $model = new Dnsstatic();

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $model->create();
            return $this->redirect(Url::toRoute('/mikrotik/ip/dns/dnsstatic/index'));
        }

        return $this->render('/mikrotik/ip/dns/createstatic', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
        ]);
    }

    public function actionUpdate() {
        $id = Yii::$app->request->get('id');
        $menu = new MikrotikSideMenu();
        $model = new Dnsstatic();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {

            $model->update($id);

            return $this->redirect(Url::toRoute('/mikrotik/ip/dns/dnsstatic/index'));
        }
        $model->get($id);


        return $this->render('/mikrotik/ip/dns/updatestatic', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
        ]);
    }

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

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

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

}
