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

if($_POST){
    $erreur = '';

    if(isset($_POST['prix_paye']) && $_POST['prix_paye'] !== '' && $_POST['prix_paye'] < 0){
        $erreur = "Le prix payé ne peut pas être négatif.";
    }

    if(empty($erreur)){
        if(empty($_POST['id_abonnement'])){
            $pdo->prepare("INSERT INTO abonnement(id_adherent, id_type_abo, date_debut, date_fin, prix_paye, etat) VALUES(?,?,?,?,?,?)")
                ->execute([$_POST['id_adherent'],$_POST['id_type_abo'],$_POST['date_debut'],$_POST['date_fin'],$_POST['prix_paye'],$_POST['etat']]);
        } else {
            $pdo->prepare("UPDATE abonnement SET id_adherent=?, id_type_abo=?, date_debut=?, date_fin=?, prix_paye=?, etat=? WHERE id_abonnement=?")
                ->execute([$_POST['id_adherent'],$_POST['id_type_abo'],$_POST['date_debut'],$_POST['date_fin'],$_POST['prix_paye'],$_POST['etat'],$_POST['id_abonnement']]);
        }
    }
}

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

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

// Récupère les abonnements avec nom/prénom de l'adhérent et libellé du type d'abonnement
$data = $pdo->query("
    SELECT ab.*, 
           ad.nom, ad.prenom,
           ta.libelle
    FROM abonnement ab
    JOIN adherent ad ON ab.id_adherent = ad.id_adherent
    JOIN typeabonnement ta ON ab.id_type_abo = ta.id_type_abo
")->fetchAll();

// Pour les selects du formulaire
$adherents = $pdo->query("SELECT id_adherent, nom, prenom FROM adherent")->fetchAll();
$types_abo  = $pdo->query("SELECT id_type_abo, libelle FROM typeabonnement")->fetchAll();

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

<?php
$hero_title = "📋 Abonnements";
$hero_subtitle = "Gestion des abonnements 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>Abonnements</h3>

<?php if(!empty($erreur)): ?>
    <div style="max-width:980px; margin:0 auto 12px; background:#f8d7da; color:#842029; border:1px solid #f5c2c7; padding:10px 15px; border-radius:6px;">
        ⚠️ <?= htmlspecialchars($erreur) ?>
    </div>
<?php endif; ?>

<form class="reservation-form" method="post">
    <input type="hidden" name="id_abonnement" value="<?= $edit['id_abonnement']??'' ?>">

    <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_type_abo">
        <option value="">-- Type abonnement --</option>
        <?php foreach($types_abo as $t): ?>
            <option value="<?= $t['id_type_abo'] ?>" <?= ($edit['id_type_abo']??'')==$t['id_type_abo']?'selected':'' ?>>
                <?= $t['libelle'] ?>
            </option>
        <?php endforeach; ?>
    </select>

    <input type="date" name="date_debut" value="<?= $edit['date_debut']??'' ?>" placeholder="Date début">
    <input type="date" name="date_fin"   value="<?= $edit['date_fin']??'' ?>"   placeholder="Date fin">
    <input name="prix_paye" value="<?= $edit['prix_paye']??'' ?>" placeholder="Prix payé">

    <select name="etat">
        <?php foreach(['Valide','Expiré','Annulé'] as $etat): ?>
            <option <?= ($edit['etat']??'')==$etat?'selected':'' ?>><?= $etat ?></option>
        <?php endforeach; ?>
    </select>

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

<table class="reservation-table">
    <tr>
        <th>Adhérent</th>
        <th>Type abonnement</th>
        <th>Date début</th>
        <th>Date fin</th>
        <th>Prix payé</th>
        <th>État</th>
        <th>Actions</th>
    <?php foreach($data as $r): ?>
        <tr>
            <td><?= $r['nom'].' '.$r['prenom'] ?></td>
            <td><?= $r['libelle'] ?></td>
            <td><?= $r['date_debut'] ?></td>
            <td><?= $r['date_fin'] ?></td>
            <td><?= $r['prix_paye'] ?> €</td>
            <td><?= $r['etat'] ?></td>
            <td>
                <a href="?edit=<?= $r['id_abonnement'] ?>">✏</a>
                <a href="?delete=<?= $r['id_abonnement'] ?>">🗑</a>
            </td>
        </tr>
    <?php endforeach; ?>
</table>

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