<?php
/**
 * View: Demo Oracle — accueil du module.
 *
 * Variables : $databases, $isManager, $hasRoom, $maxDbs, $selectedDb,
 *             $csrfToken, $pageTitle
 */
include WEBROOT_PATH . '/includes/header.php';
?>

<div class="page-header">
    <h1>
        <img src="/modules/demo_oracle/assets/img/logo_demo_oracle_512.png" class="module-home-logo" alt="">
        <?= t('demo_oracle.title') ?>
    </h1>
    <?php if ($isManager): ?>
    <div class="page-actions">
        <a href="<?= demo_oracle_url('db.list') ?>" class="btn btn-secondary"><?= t('demo_oracle.db_manage') ?></a>
        <a href="<?= demo_oracle_url('admin') ?>" class="btn btn-secondary"><?= t('demo_oracle.admin_title') ?></a>
    </div>
    <?php endif; ?>
</div>

<div class="card" style="margin-bottom:1rem">
    <p style="margin:0;color:var(--color-text-muted)"><?= t('demo_oracle.intro') ?></p>
</div>

<?php if ($selectedDb): ?>
<div class="alert alert-info" style="display:flex;align-items:center;justify-content:space-between;gap:1rem;flex-wrap:wrap">
    <span>
        <?= t('demo_oracle.currently_connected') ?>
        <strong><?= htmlspecialchars((string)$selectedDb['name']) ?></strong>
    </span>
    <div style="display:flex;gap:.5rem">
        <a href="<?= demo_oracle_url('report') ?>" class="btn btn-primary btn-sm"><?= t('demo_oracle.go_to_reports') ?></a>
        <a href="<?= demo_oracle_url('disconnect') ?>" class="btn btn-secondary btn-sm"><?= t('demo_oracle.disconnect') ?></a>
    </div>
</div>
<?php endif; ?>

<div class="card">
    <h2 style="margin:0 0 .5rem;font-size:1rem;display:flex;align-items:center;gap:.5rem">
        <?= t('demo_oracle.databases') ?>
        <span class="badge"><?= count($databases) ?> / <?= (int)$maxDbs ?></span>
    </h2>

    <?php if (empty($databases)): ?>
        <p><?= $isManager ? t('demo_oracle.no_db_manager') : t('demo_oracle.no_db') ?></p>
    <?php else: ?>
    <table class="data-table">
        <thead>
            <tr>
                <th><?= t('demo_oracle.db_name') ?></th>
                <th><?= t('demo_oracle.db_host') ?></th>
                <th><?= t('demo_oracle.db_service') ?></th>
                <th><?= t('demo_oracle.db_user') ?></th>
                <th><?= t('demo_oracle.db_description') ?></th>
                <th></th>
            </tr>
        </thead>
        <tbody>
        <?php foreach ($databases as $d): ?>
            <tr>
                <td><strong><?= htmlspecialchars((string)$d['name']) ?></strong></td>
                <td><?= htmlspecialchars((string)$d['host']) ?>:<?= (int)$d['port'] ?></td>
                <td><?= htmlspecialchars((string)$d['service_name']) ?></td>
                <td><?= htmlspecialchars((string)($d['username'] ?? '')) ?><?php if (!empty($d['sysdba'])): ?>
                    <span class="badge badge-warning">SYSDBA</span><?php endif; ?></td>
                <td><?= htmlspecialchars((string)($d['description'] ?? '')) ?></td>
                <td style="text-align:right;white-space:nowrap">
                    <form method="post" action="<?= demo_oracle_url('connect') ?>" style="display:inline">
                        <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($csrfToken) ?>">
                        <input type="hidden" name="db_id" value="<?= (int)$d['id'] ?>">
                        <button type="submit" class="btn btn-primary btn-sm"><?= t('demo_oracle.connect') ?></button>
                    </form>
                    <?php if ($isManager): ?>
                    <a href="<?= demo_oracle_url('db.edit', ['id' => (int)$d['id']]) ?>"
                       class="btn btn-secondary btn-sm"><?= t('demo_oracle.edit') ?></a>
                    <?php endif; ?>
                </td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
    <?php endif; ?>

    <?php if ($isManager): ?>
        <?php if ($hasRoom): ?>
        <p style="margin:1rem 0 0">
            <a href="<?= demo_oracle_url('db.new') ?>" class="btn btn-primary btn-sm"><?= t('demo_oracle.db_add') ?></a>
        </p>
        <?php else: ?>
        <p style="margin:1rem 0 0;display:flex;align-items:center;gap:.6rem;flex-wrap:wrap">
            <button type="button" class="btn btn-primary btn-sm"<?= DemoOracleDemo::disabled() ?>>
                <?= t('demo_oracle.db_add') ?>
            </button>
            <?= DemoOracleDemo::badge(t('demo_oracle.db_limit_hint', ['max' => (string)$maxDbs])) ?>
        </p>
        <?php endif; ?>
    <?php endif; ?>
</div>

<?php include WEBROOT_PATH . '/includes/footer.php'; ?>
