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

if($_POST){
    $id_activite      = $_POST['id_activite'];
    $id_coach         = !empty($_POST['id_coach']) ? $_POST['id_coach'] : null;
    $date_heure_debut = $_POST['date_heure_debut'];
    $duree_minutes    = $_POST['duree_minutes'];
    $capacite_max     = $_POST['capacite_max'];

    if(empty($_POST['id_seance'])){
        $pdo->prepare("INSERT INTO seance(id_activite, id_coach, date_heure_debut, duree_minutes, capacite_max) VALUES(?,?,?,?,?)")
            ->execute([$id_activite, $id_coach, $date_heure_debut, $duree_minutes, $capacite_max]);
    } else {
        $pdo->prepare("UPDATE seance SET id_activite=?, id_coach=?, date_heure_debut=?, duree_minutes=?, capacite_max=? WHERE id_seance=?")
            ->execute([$id_activite, $id_coach, $date_heure_debut, $duree_minutes, $capacite_max, $_POST['id_seance']]);
    }
}

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

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

$data = $pdo->query("
    SELECT s.*,
           a.nom_activite,
           c.nom AS nom_coach, c.prenom AS prenom_coach
    FROM seance s
    JOIN activite a  ON s.id_activite = a.id_activite
    LEFT JOIN coach c ON s.id_coach   = c.id_coach
    ORDER BY s.date_heure_debut
")->fetchAll();

$activites = $pdo->query("SELECT id_activite, nom_activite FROM activite ORDER BY nom_activite")->fetchAll();
$coachs    = $pdo->query("SELECT id_coach, nom, prenom FROM coach ORDER BY nom, prenom")->fetchAll();

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

<?php
$hero_title    = "🗓️ Séances";
$hero_subtitle = "Gestion des séances 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>Séances</h3>
<form class="reservation-form" method="post">
    <input type="hidden" name="id_seance" value="<?= $edit['id_seance']??'' ?>">

    <select name="id_activite">
        <option value="">-- Activité --</option>
        <?php foreach($activites as $a): ?>
            <option value="<?= $a['id_activite'] ?>" <?= ($edit['id_activite']??'')==$a['id_activite']?'selected':'' ?>>
                <?= $a['nom_activite'] ?>
            </option>
        <?php endforeach; ?>
    </select>

    <select name="id_coach">
        <option value="">-- Coach (optionnel) --</option>
        <?php foreach($coachs as $c): ?>
            <option value="<?= $c['id_coach'] ?>" <?= ($edit['id_coach']??'')==$c['id_coach']?'selected':'' ?>>
                <?= $c['nom'].' '.$c['prenom'] ?>
            </option>
        <?php endforeach; ?>
    </select>

    <input type="datetime-local" name="date_heure_debut"
           value="<?= isset($edit['date_heure_debut']) ? date('Y-m-d\TH:i', strtotime($edit['date_heure_debut'])) : '' ?>">

    <input type="number" name="duree_minutes" value="<?= $edit['duree_minutes']??'' ?>" placeholder="Durée (min)">
    <input type="number" name="capacite_max"  value="<?= $edit['capacite_max']??''  ?>" placeholder="Capacité max">

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

<table class="reservation-table">
    <tr>
        <th>Activité</th>
        <th>Coach</th>
        <th>Date</th>
        <th>Durée</th>
        <th>Capacité</th>
        <th>Actions</th>
    </tr>
    <?php foreach($data as $r): ?>
        <tr>
            <td><?= $r['nom_activite'] ?></td>
            <td><?= $r['nom_coach'] ? $r['nom_coach'].' '.$r['prenom_coach'] : 'N/A' ?></td>
            <td><?= date('d/m/Y H:i', strtotime($r['date_heure_debut'])) ?></td>
            <td><?= $r['duree_minutes'] ?> min</td>
            <td><?= $r['capacite_max'] ?></td>
            <td>
                <a href="?edit=<?= $r['id_seance'] ?>">✏</a>
                <a href="?delete=<?= $r['id_seance'] ?>">🗑</a>
            </td>
        </tr>
    <?php endforeach; ?>
</table>

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