<?php

namespace app\controllers\mikrotik\routing\rip;

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\routing\rip\Interfacesrip;
use app\models\mikrotik\routing\rip\Network;
use app\models\mikrotik\routing\rip\Key;
use app\models\mikrotik\routing\rip\Neighbor;
use app\models\mikrotik\routing\rip\Routes;
use app\models\mikrotik\routing\rip\Rip;
use app\models\mikrotik\Interfaces;

class RipController extends Controller {

    public function behaviors() {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingViewRIP'],
                    ],
                    [
                        'actions' => ['settings'],
                        'allow' => true,
                        'roles' => ['mikrotik_routingCreateRIPSettings'],
                    ],
                    [
                        '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();
        $bdf = new Interfacesrip();
        $bdfs = $bdf->getAll();


        $provide['interface'] = new ArrayDataProvider([
            'allModels' => $bdfs,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'interface',
                    'receive',
                    'send',
                    'authentication',
                    'key_chain',
                    'passive',
                    'in_prefix_list',
                    'out_prefix_list',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $net = new Network();
        $nets = $net->getAll();

        $provide['net'] = new ArrayDataProvider([
            'allModels' => $nets,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'network',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $key = new Key();
        $keys = $key->getAll();


        $provide['key'] = new ArrayDataProvider([
            'allModels' => $keys,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'chain',
                    'key_id',
                    'key',
                    'from_date',
                    'to_date',
                    'passive',
                    'in_prefix_list',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $neighbor = new Neighbor();
        $neighbors = $neighbor->getAll();


        $provide['neighbor'] = new ArrayDataProvider([
            'allModels' => $neighbors,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'address',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

        $route = new Routes();
        $routes = $route->getAll();
        $provide['route'] = new ArrayDataProvider([
            'allModels' => $routes,
            'key' => 'id',
            'sort' => [
                'attributes' => [
                    'dst_address',
                    'gateway',
                    'from',
                    'metric',
                    'timeout',
                ],
            ],
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);

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

    public function actionSettings() {
        $menu = new MikrotikSideMenu();
        $model = new Rip();
        $model->getAll();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {


            $c = $model->update();
            if ($c != '1') {
                Yii::$app->session->setFlash('message', $c);
            } else {
                return $this->redirect(Url::toRoute('mikrotik/routing/rip/rip/index'));
            }
        }

        return $this->render('/mikrotik/routing/rip/settings', [
                    'menu' => $menu->getMenu(),
                    'model' => $model,
        ]);
    }

}
