<?php
require_once 'config.php';
require_once 'functions.php';
requireLogin();

$id = (int)($_GET['id'] ?? 0);

$stmt = $pdo->prepare("SELECT id, status, reservation_date FROM reservations WHERE id = ? AND user_id = ?");
$stmt->execute([$id, $_SESSION['user_id']]);
$res = $stmt->fetch();

if (!$res) redirect('my_reservations.php');

if ($res['status'] === 'confirmée' && strtotime($res['reservation_date']) >= strtotime(date('Y-m-d'))) {
    $pdo->prepare("UPDATE reservations SET status='annulée' WHERE id=?")->execute([$id]);
    $_SESSION['flash'] = "Réservation annulée avec succès.";
} else {
    $_SESSION['error'] = "Impossible d'annuler cette réservation.";
}

redirect('my_reservations.php');
