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

if($_POST){
    if(empty($_POST['id_activite'])){
        $pdo->prepare("INSERT INTO activite(nom_activite,description,difficulte) VALUES(?,?,?)")
        ->execute([$_POST['nom_activite'],$_POST['description'],$_POST['difficulte']]);
    } else { 
        $pdo->prepare("UPDATE activite SET nom_activite=?,description=?,difficulte=? WHERE id_activite=?")
        ->execute([$_POST['nom_activite'],$_POST['description'],$_POST['difficulte'],$_POST['id_activite']]);
    }}
if(isset($_GET['edit'])){
    $e=$pdo->prepare("SELECT * FROM activite WHERE id_activite=?");
    $e->execute([$_GET['edit']]);
    $edit=$e->fetch();
}
if(isset($_GET['delete'])){
    $pdo->prepare("DELETE FROM activite WHERE id_activite=?")->execute([$_GET['delete']]);
}
$data=$pdo->query("SELECT * FROM activite")->fetchAll();
include "includes/header.php"; include "includes/menu.php";
?>
<?php
$hero_title = "🏋️ Activite";
$hero_subtitle = "Gestion des activites";
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>Activites</h3>
<form class="reservation-form" method="post">
<input type="hidden" name="id_activite" value="<?= $edit['id_activite']??'' ?>">
<input name="nom_activite" value="<?= $edit['nom_activite']??'' ?>" placeholder="nom_activité">
<input name="description" value="<?= $edit['description']??'' ?>" placeholder="description">
    <select name="difficulte">
        <option <?= ($edit['difficulte']??'')=='Facile' ? 'selected' : '' ?>>Facile</option>
        <option <?= ($edit['difficulte']??'')=='Moyen' ? 'selected' : '' ?>>Moyen</option>
        <option <?= ($edit['difficulte']??'')=='Difficile' ? 'selected' : '' ?>>Difficile</option>
    </select>
<button>Enregistrer</button>
</form>
<table class="reservation-table">
    <tr>
        <th>Nom activité</th>
        <th>Description</th>
        <th>Difficulté</th>
        <th>Actions</th>
    <?php foreach($data as $r): ?>
    <tr>
        <td><?= $r['nom_activite'] ?></td><td><?= $r['description'] ?></td><td><?= $r['difficulte'] ?></td>
        <td><a href="?edit=<?= $r['id_activite'] ?>">✏</a> <a href="?delete=<?= $r['id_activite'] ?>">🗑</a></td>
    </tr>
    <?php endforeach; ?>
</table>
<?php include "includes/footer.php"; ?>

