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

if($_POST){
    if(empty($_POST['id_coach'])){
        $pdo->prepare("INSERT INTO coach(nom, prenom, specialite, telephone) VALUES(?,?,?,?)")
            ->execute([$_POST['nom'],$_POST['prenom'],$_POST['specialite'],$_POST['telephone']]);
    } else {
        $pdo->prepare("UPDATE coach SET nom=?, prenom=?, specialite=?, telephone=? WHERE id_coach=?")
            ->execute([$_POST['nom'],$_POST['prenom'],$_POST['specialite'],$_POST['telephone'],$_POST['id_coach']]);
    }
}

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

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

$data = $pdo->query("SELECT * FROM coach ORDER BY nom, prenom")->fetchAll();

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

<?php
$hero_title    = "🏋️ Coachs";
$hero_subtitle = "Gestion des coachs 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>Coachs</h3>
<form class="reservation-form" method="post">
    <input type="hidden" name="id_coach" value="<?= $edit['id_coach']??'' ?>">

    <input name="nom"        value="<?= $edit['nom']??''        ?>" placeholder="Nom">
    <input name="prenom"     value="<?= $edit['prenom']??''     ?>" placeholder="Prénom">
    <input name="specialite" value="<?= $edit['specialite']??'' ?>" placeholder="Spécialité">
    <input name="telephone"  value="<?= $edit['telephone']??''  ?>" placeholder="Téléphone">

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

<table class="reservation-table">
    <tr>
        <th>Nom</th>
        <th>Prénom</th>
        <th>Spécialité</th>
        <th>Téléphone</th>
        <th>Actions</th>
    </tr>
    <?php foreach($data as $r): ?>
        <tr>
            <td><?= $r['nom'] ?></td>
            <td><?= $r['prenom'] ?></td>
            <td><?= $r['specialite'] ?? 'N/A' ?></td>
            <td><?= $r['telephone'] ?? 'N/A' ?></td>
            <td>
                <a href="?edit=<?= $r['id_coach'] ?>">✏</a>
                <a href="?delete=<?= $r['id_coach'] ?>">🗑</a>
            </td>
        </tr>
    <?php endforeach; ?>
</table>

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