<?php

declare(strict_types=1);

/**
 * @var yii\web\View               $this
 * @var array<string, array<string, mixed>> $connections
 */

use yii\helpers\Html;

$this->title = 'DataDash — Database Connections';
?>

<div class="datadash-config-index">

    <div class="d-flex align-items-center justify-content-between mb-3">
        <h1 class="mb-0"><?= Html::encode($this->title) ?></h1>
        <?= Html::a('+ Add Connection', ['/ngauge-datadash/config/create'], ['class' => 'btn btn-primary']) ?>
    </div>

    <?php foreach (['success', 'error', 'warning', 'info'] as $type) { ?>
        <?php if (Yii::$app->session->hasFlash($type)) { ?>
            <div class="alert alert-<?= $type === 'error' ? 'danger' : $type ?> alert-dismissible">
                <button type="button" class="close" data-dismiss="alert">&times;</button>
                <pre class="mb-0" style="white-space:pre-wrap"><?= Html::encode((string) Yii::$app->session->getFlash($type)) ?></pre>
            </div>
        <?php } ?>
    <?php } ?>

    <?php if (empty($connections)) { ?>
        <div class="alert alert-info">
            No database connections configured yet.
            <?= Html::a('Add the first connection', ['/ngauge-datadash/config/create']) ?>.
        </div>
    <?php } else { ?>
        <div class="table-responsive">
            <table class="table table-bordered table-hover table-striped">
                <thead class="thead-dark">
                    <tr>
                        <th>Name</th>
                        <th>Label</th>
                        <th>Driver</th>
                        <th>Host</th>
                        <th>Database</th>
                        <th>Component ID</th>
                        <th>Status</th>
                        <th style="width:200px">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($connections as $name => $conn) { ?>
                        <tr>
                            <td><code><?= Html::encode($name) ?></code></td>
                            <td><?= Html::encode((string) ($conn['label'] ?? '')) ?></td>
                            <td><span class="badge badge-secondary"><?= Html::encode((string) ($conn['driver'] ?? 'mysql')) ?></span></td>
                            <td><?= Html::encode((string) ($conn['host'] ?? '')) ?></td>
                            <td><?= Html::encode((string) ($conn['dbname'] ?? '')) ?></td>
                            <td><code>datadash_<?= Html::encode($name) ?></code></td>
                            <td>
                                <?php if (!empty($conn['enabled'])) { ?>
                                    <span class="badge badge-success">Active</span>
                                <?php } else { ?>
                                    <span class="badge badge-secondary">Disabled</span>
                                <?php } ?>
                            </td>
                            <td>
                                <?= Html::a('Edit', ['/ngauge-datadash/config/update', 'name' => $name], ['class' => 'btn btn-sm btn-outline-primary']) ?>
                                <?= Html::a('Generate', ['/ngauge-datadash/config/generate', 'name' => $name], ['class' => 'btn btn-sm btn-outline-info']) ?>
                                <?= Html::beginForm(['/ngauge-datadash/config/delete'], 'post', ['style' => 'display:inline']) ?>
                                    <?= Html::hiddenInput('name', $name) ?>
                                    <?= Html::submitButton('Delete', [
                                        'class'   => 'btn btn-sm btn-outline-danger',
                                        'onclick' => 'return confirm(' . json_encode('Delete connection "' . $name . '"?') . ');',
                                    ]) ?>
                                <?= Html::endForm() ?>
                            </td>
                        </tr>
                    <?php } ?>
                </tbody>
            </table>
        </div>
    <?php } ?>

    <div class="card border-info mt-4">
        <div class="card-header bg-info text-white">Using Connections in Code</div>
        <div class="card-body">
            <p class="mb-1">Each connection is available as a Yii DB component named <code>datadash_{name}</code>:</p>
            <pre class="bg-light p-2 mb-0"><code>Yii::$app->get('datadash_myconn')->createCommand('SELECT ...')-&gt;queryAll();</code></pre>
            <p class="mt-2 mb-0">In ActiveRecord models generated via <strong>Generate Models</strong>, <code>getDb()</code> returns the component automatically.</p>
        </div>
    </div>

</div>
