<?php

require_once __DIR__ . '/../lib/init.php';
Auth::requireLogin();
Auth::requireRole('admin', 'tech');

$requests   = ReservationRequest::getAll();
$page_title = 'Demandes de réservation';
require_once APP_ROOT . '/lib/layout_header.php';

$flash = $_SESSION['flash'] ?? null;
unset($_SESSION['flash']);
?>

<?php if ($flash): ?>
<div class="alert alert-<?= h($flash['type']) ?> alert-dismissible fade show">
    <?= h($flash['message']) ?>
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
<?php endif; ?>

<div class="card">
    <div class="card-header py-3 d-flex justify-content-between align-items-center">
        <span><i class="bi bi-envelope-plus me-2"></i>Demandes en attente</span>
        <span class="badge bg-secondary"><?= count($requests) ?></span>
    </div>
    <div class="card-body p-0">
        <?php if ($requests): ?>
        <table class="table table-hover mb-0">
            <thead class="table-light">
                <tr>
                    <th>Date</th>
                    <th>Nom</th>
                    <th>Email</th>
                    <th>MAC</th>
                    <th>Réseau</th>
                    <th>Description</th>
                    <th style="width:160px"></th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($requests as $r): ?>
            <tr>
                <td class="text-nowrap">
                    <small class="text-muted"><?= h(substr($r['created_at'], 0, 16)) ?></small>
                </td>
                <td class="fw-semibold"><?= h($r['requester_name']) ?></td>
                <td><a href="mailto:<?= h($r['requester_email']) ?>" class="text-decoration-none small"><?= h($r['requester_email']) ?></a></td>
                <td><code><?= h($r['mac_address']) ?></code></td>
                <td><?= h($r['subnet_name'] ?? '—') ?></td>
                <td class="text-muted small" style="max-width:220px">
                    <?php if ($r['description']): ?>
                        <span title="<?= h($r['description']) ?>">
                            <?= h(mb_strlen($r['description']) > 60 ? mb_substr($r['description'], 0, 60) . '…' : $r['description']) ?>
                        </span>
                    <?php else: ?>
                        —
                    <?php endif; ?>
                </td>
                <td class="text-end text-nowrap">
                    <?php
                    $desc = 'Demande de ' . $r['requester_name'] . ' (' . $r['requester_email'] . ')';
                    if ($r['description']) {
                        $desc .= ' : ' . $r['description'];
                    }
                    $acceptUrl = 'reservation_add.php?' . http_build_query([
                        'subnet_id'   => $r['subnet_id'],
                        'mac'         => $r['mac_address'],
                        'description' => $desc,
                        'request_id'  => $r['id'],
                    ]);
                    ?>
                    <a href="<?= h($acceptUrl) ?>" class="btn btn-sm btn-success me-1" title="Accepter — ouvrir le formulaire de réservation">
                        <i class="bi bi-check-lg"></i> Accepter
                    </a>
                    <form method="post" action="request_delete.php" class="d-inline"
                          onsubmit="return confirm('Refuser et supprimer la demande de <?= h(addslashes($r['requester_name'])) ?> ?')">
                        <input type="hidden" name="request_id" value="<?= $r['id'] ?>">
                        <button type="submit" class="btn btn-sm btn-outline-danger" title="Refuser">
                            <i class="bi bi-x-lg"></i> Refuser
                        </button>
                    </form>
                </td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        <?php else: ?>
        <div class="p-5 text-center text-muted">
            <i class="bi bi-envelope-check fs-1"></i>
            <p class="mt-3 mb-0">Aucune demande en attente.</p>
        </div>
        <?php endif; ?>
    </div>
</div>

<?php require_once APP_ROOT . '/lib/layout_footer.php'; ?>
