<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    exit(0);
}

require_once 'config.php';

try {
    $siteID = $_GET['SiteID'] ?? '';

    if (empty($siteID)) {
        echo json_encode(["success" => false, "message" => "SiteID manquant"]);
        exit();
    }

    // Récupérer le dernier numéro de ticket
    $stmt = $pdo->prepare("SELECT numero_ticket FROM Tickets_Parking WHERE SiteID = ? ORDER BY numero_ticket DESC LIMIT 1");
    $stmt->execute([$siteID]);
    $lastTicket = $stmt->fetchColumn();

    echo json_encode([
        "success" => true,
        "lastTicketNumber" => $lastTicket ?: null
    ]);

} catch (Exception $e) {
    error_log("Erreur get_last_ticket: " . $e->getMessage());
    echo json_encode([
        "success" => false,
        "message" => "Erreur serveur: " . $e->getMessage()
    ]);
}
?>
