<?php
require_once "includes/auth.php";
require_once "config/db.php";

if($_POST){
    if(empty($_POST['id_reservation'])){
        $pdo->prepare("INSERT INTO reservation(id_adherent, id_seance, date_reservation, statut_reservation) VALUES(?,?,?,?)")
            ->execute([$_POST['id_adherent'],$_POST['id_seance'],$_POST['date_reservation'],$_POST['statut_reservation']]);
    } else {
        $pdo->prepare("UPDATE reservation SET id_adherent=?, id_seance=?, date_reservation=?, statut_reservation=? WHERE id_reservation=?")
            ->execute([$_POST['id_adherent'],$_POST['id_seance'],$_POST['date_reservation'],$_POST['statut_reservation'],$_POST['id_reservation']]);
    }
}

if(isset($_GET['edit'])){
    $e = $pdo->prepare("SELECT * FROM reservation WHERE id_reservation=?");
    $e->execute([$_GET['edit']]);
    $edit = $e->fetch();
}

if(isset($_GET['delete'])){
    $pdo->prepare("DELETE FROM reservation WHERE id_reservation=?")->execute([$_GET['delete']]);
}

// Réservations avec nom/prénom adhérent + infos séance + activité
$data = $pdo->query("
    SELECT r.*,
           ad.nom, ad.prenom,
           s.date_heure_debut, s.duree_minutes, s.capacite_max,
           ac.nom_activite,
           CONCAT(c.nom, ' ', c.prenom) AS nom_coach
    FROM reservation r
    JOIN adherent ad ON r.id_adherent = ad.id_adherent
    JOIN seance s    ON r.id_seance   = s.id_seance
    JOIN activite ac ON s.id_activite = ac.id_activite
    LEFT JOIN coach c ON s.id_coach   = c.id_coach
    ORDER BY r.date_reservation DESC
")->fetchAll();

// Pour les selects du formulaire
$adherents = $pdo->query("SELECT id_adherent, nom, prenom FROM adherent ORDER BY nom, prenom")->fetchAll();
$seances   = $pdo->query("
    SELECT s.id_seance, s.date_heure_debut, ac.nom_activite
    FROM seance s
    JOIN activite ac ON s.id_activite = ac.id_activite
    ORDER BY s.date_heure_debut
")->fetchAll();

include "includes/header.php"; include "includes/menu.php";
?>

<?php
$hero_title    = "📅 Réservations";
$hero_subtitle = "Gestion des réservations de la salle";
include "includes/hero_banner.php";
?>

<style>
    .reservation-form,
    .reservation-table {
        width: 100%;
        max-width: 980px;
        margin: 0 auto 20px;
        font-family: Arial, Helvetica, sans-serif;
    }

    .reservation-form {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
        gap: 10px;
        background: #f7f9fb;
        padding: 15px;
        border-radius: 8px;
        box-shadow: 0 2px 8px rgba(0,0,0,.08);
    }

    .reservation-form select,
    .reservation-form input,
    .reservation-form button {
        height: 38px;
        padding: 6px 10px;
        border: 1px solid #cbd5e1;
        border-radius: 5px;
        font-size: 14px;
    }

    .reservation-form button {
        grid-column: span 2;
        background: #007bff;
        color: #fff;
        border: none;
        cursor: pointer;
    }

    .reservation-form button:hover {
        background: #0056d6;
    }

    .reservation-table {
        border-collapse: collapse;
        background: #fff;
        overflow-x: auto;
    }

    .reservation-table th,
    .reservation-table td {
        border: 1px solid #d0d7de;
        padding: 11px 12px;
        text-align: left;
    }

    .reservation-table th {
        background: #1f2937;
        color: #fff;
        text-transform: uppercase;
        font-size: 13px;
        letter-spacing: .04em;
    }

    .reservation-table tr:nth-child(even) {
        background: #f4f6f8;
    }

    .reservation-table a {
        display: inline-block;
        margin-right: 6px;
        color: #007bff;
        text-decoration: none;
    }

    .statut-confirmée {
        color: #0f5132;
        background: #d1e7dd;
        padding: 3px 8px;
        border-radius: 4px;
        display: inline-block;
        font-size: 13px;
    }

    .statut-annulée {
        color: #842029;
        background: #f8d7da;
        padding: 3px 8px;
        border-radius: 4px;
        display: inline-block;
        font-size: 13px;
    }
</style>

<h3>Réservations</h3>
<form class="reservation-form" method="post">
    <input type="hidden" name="id_reservation" value="<?= $edit['id_reservation']??'' ?>">

    <select name="id_adherent">
        <option value="">-- Adhérent --</option>
        <?php foreach($adherents as $a): ?>
            <option value="<?= $a['id_adherent'] ?>" <?= ($edit['id_adherent']??'')==$a['id_adherent']?'selected':'' ?>>
                <?= $a['nom'].' '.$a['prenom'] ?>
            </option>
        <?php endforeach; ?>
    </select>

    <select name="id_seance">
        <option value="">-- Séance --</option>
        <?php foreach($seances as $s): ?>
            <option value="<?= $s['id_seance'] ?>" <?= ($edit['id_seance']??'')==$s['id_seance']?'selected':'' ?>>
                <?= $s['nom_activite'].' — '.date('d/m/Y H:i', strtotime($s['date_heure_debut'])) ?>
            </option>
        <?php endforeach; ?>
    </select>

    <input type="datetime-local" name="date_reservation"
           value="<?= isset($edit['date_reservation']) ? date('Y-m-d\TH:i', strtotime($edit['date_reservation'])) : '' ?>"
           placeholder="Date de réservation">

    <select name="statut_reservation">
        <?php foreach(['Confirmée','Annulée'] as $statut): ?>
            <option <?= ($edit['statut_reservation']??'')==$statut?'selected':'' ?>><?= $statut ?></option>
        <?php endforeach; ?>
    </select>

    <button>Enregistrer</button>
</form>

<table class="reservation-table">
    <tr>
        <th>Adhérent</th>
        <th>Activité</th>
        <th>Date séance</th>
        <th>Coach</th>
        <th>Date réservation</th>
        <th>Statut</th>
        <th>Actions</th>
    </tr>
    <?php foreach($data as $r): ?>
        <tr>
            <td><?= $r['nom'].' '.$r['prenom'] ?></td>
            <td><?= $r['nom_activite'] ?></td>
            <td><?= date('d/m/Y H:i', strtotime($r['date_heure_debut'])) ?></td>
            <td><?= $r['nom_coach'] ?? 'N/A' ?></td>
            <td><?= date('d/m/Y H:i', strtotime($r['date_reservation'])) ?></td>
            <td>
                <?php $c = strtolower(str_replace('é','e',$r['statut_reservation'])); ?>
                <span class="statut-<?= $c ?>"><?= $r['statut_reservation'] ?></span>
            </td>
            <td>
                <a href="?edit=<?= $r['id_reservation'] ?>">✏</a>
                <a href="?delete=<?= $r['id_reservation'] ?>">🗑</a>
            </td>
        </tr>
    <?php endforeach; ?>
</table>

<?php include "includes/footer.php"; ?>