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

if($_POST){

    $erreur = '';

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

    if(empty($erreur)){
        if(empty($_POST['id_type_abo'])){
            $pdo->prepare("INSERT INTO typeabonnement(libelle, duree_jours, prix_initial) VALUES(?,?,?)")
                ->execute([$_POST['libelle'],$_POST['duree_jours'],$_POST['prix_initial']]);
        } else {
        $pdo->prepare("UPDATE typeabonnement SET libelle=?,duree_jours=?, prix_initial=? WHERE id_type_abo=?")
            ->execute([$_POST['libelle'],$_POST['duree_jours'],$_POST['prix_initial'], $_POST['id_type_abo']]);
    }}

}

if(isset($_GET['edit'])){
    $e=$pdo->prepare("SELECT * FROM typeabonnement WHERE id_type_abo=?");
    $e->execute([$_GET['edit']]);
    $edit=$e->fetch();
}
if(isset($_GET['delete'])){
    $pdo->prepare("DELETE FROM typeabonnement WHERE id_type_abo=?")->execute([$_GET['delete']]);
}
$data=$pdo->query("SELECT * FROM typeabonnement")->fetchAll();
include "includes/header.php"; include "includes/menu.php";
?>
<?php
$hero_title = "👥 Type 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>Type abonnements</h3>
<form class="reservation-form" method="post">
    <input type="hidden" name="id_type_abo" value="<?= $edit['id_type_abo']??'' ?>">
    <input name="libelle" value="<?= $edit['libelle']??'' ?>" placeholder="Libelle">
    <input name="duree_jours" value="<?= $edit['duree_jours']??'' ?>" placeholder="duree_jours">
    <input name="prix_initial" value="<?= $edit['prix_initial']??'' ?>" placeholder="prix_initial">
    <button>Enregistrer</button>

<?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">

</form>
<table class="reservation-table">
    <tr>
        <th>Libelle</th>
        <th>Duree jours</th>
        <th>Prix initial</th>
        <th>Actions</th>
    <?php foreach($data as $r): ?>
        <tr>
            <td><?= $r['libelle'] ?></td><td><?= $r['duree_jours'] ?></td><td><?= $r['prix_initial'] ?></td>
            <td><a href="?edit=<?= $r['id_type_abo'] ?>">✏</a> <a href="?delete=<?= $r['id_type_abo'] ?>">🗑</a></td>
        </tr>
    <?php endforeach; ?>
</table>

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

    