<?php

use yii\bootstrap\Alert;

if (Yii::$app->session->getFlash('message') != null) {
    echo Alert::widget([
        'options' => [
            'class' => 'alert-info',
        ],
        'body' => Yii::$app->session->getFlash('message'),
    ]);
}

use yii\helpers\Html;
use yii\widgets\Menu;
use yii\widgets\ActiveForm;
use yii\web\Session;
use yii\grid\GridView;
use yii\bootstrap\Tabs;

$this->title = 'Mikrotik connection';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-index">
    <div class="row">
        <div class="col-lg-3">
            <?php
            $session = Yii::$app->session;
            if (($session->get('ip') != null)) {
                echo Menu::widget($menu);
            }
            ?> 
        </div>
        <div class="col-md-9">
            <h1 style="margin-top: 0px"><?= Html::encode($this->title) ?></h1>

            <?php
            $form = ActiveForm::begin(['method' => 'post',
                        'action' => ['/mikrotik/index']]);
            $session = Yii::$app->session;
            if ($session->get('ip') != null) {
                echo "<h3>Currently connected to: " . $session->get('ip') . " as: " . $session->get('login') . "</h3>";
            }
            ?>
            <?php
            if (($ip != null) and ! isset(Yii::$app->authManager->getRolesByUser(Yii::$app->user->getId())['admin'])) {
                $ips = (explode(",", $ip));

                foreach ($ips as $c => $value) {
                    $login = (explode("-", $value));
                    if (isset($login[1])) {
                        $mikrotik[$login[0] . ' as ' . $login[1]] = $login[0] . ' as ' . $login[1];
                    }
                }
                echo $form->field($model, 'ip')->dropDownList($mikrotik)->label('Connect to:');
                echo $form->field($model, 'pwd')->passwordInput()->label('Password');
                if ($session->get('ip') != null) {

                    echo Html::a('<span class="btn-label">Logout</span>', ['disconnect'], ['class' => 'btn btn-primary']);
                } else {
                    echo Html::submitButton('Log in', ['class' => 'btn btn-primary']);
                }
            } else if (!isset(Yii::$app->authManager->getRolesByUser(Yii::$app->user->getId())['admin'])) {

                echo $form->field($model, 'ip')->label('Connect to:');
                echo $form->field($model, 'login')->label('Login');
                echo $form->field($model, 'pwd')->passwordInput()->label('Password');
                if ($session->get('ip') != null) {

                    echo Html::a('<span class="btn-label">Logout</span>', ['disconnect'], ['class' => 'btn btn-primary']);
                } else {
                    echo Html::submitButton('Log in', ['class' => 'btn btn-primary']);
                }
            }


            if (isset(Yii::$app->authManager->getRolesByUser(Yii::$app->user->getId())['admin'])) {
                ?>

                <label class="control-label" for="mikrotikForm-connect">Connect to:</label>
                <input type="text" class="form-control" name="MikrotikForm[ip]" list="ip" >
                <datalist id="ip">

                    <?php
                    $ips = (explode(",", $ip));
                    if ($ips != null) {
                        foreach ($ips as $c => $value) {
                            $login = (explode("-", $value));
                            echo "<option value=$login[0]>$login[0]</option>";
                        }
                    }
                    ?>

                </datalist></br>
                <?= $form->field($model, 'login')->label('Login'); ?>
                <?= $form->field($model, 'pwd')->passwordInput()->label('Password') ?>
                <?php
                if ($session->get('ip') != null) {

                    echo Html::a('<span class="btn-label">Logout</span>', ['disconnect'], ['class' => 'btn btn-primary']);
                } else {
                    echo Html::submitButton('Log in', ['class' => 'btn btn-primary']);
                }
                echo '</br>';
                echo '</br>';
                if ($session->get('ip') != null) {
                    $tabs[] = ['label' => 'Neighbors', 'content' => GridView::widget([
                            'dataProvider' => $provider,
                            'id' => 'address',
                            'columns' => [
                                'address:text:IP Address',
                                'mac_address:text:MAC Address',
                                'identity:text:Identity',
                                'platform:text:Platform',
                                'version:text:Version',
                                [
                                    'class' => 'yii\grid\ActionColumn', 'template' => '{connect}',
                                    'controller' => 'mikrotik',
                                    'buttons' => [
                                        'connect' => function ($url, $key) {
                                            return Html::a('<span>connect</span>', $url, [
                                                        'title' => 'connect',
                                                            ]
                                            );
                                        },
                                            ],
                                        ],
                                    ],
                                    'showFooter' => false,
                                ])
                            ];
                            echo '</br>';
                            echo Tabs::widget([
                                'items' => $tabs]);
                        }
                    }
                    ?>


                    </br>

                    <?php
                    ActiveForm::end();
                    ?>

        </div>
    </div>
</div>