<?php
require_once 'session.php';
require_once 'db.php';

try {
    $stmt = $pdo->prepare("SELECT p.*, 
                            (SELECT COUNT(*) FROM coasters WHERE parc_id = p.id AND statut != 'Fermé') as nb_coasters 
                            FROM parcs p 
                            WHERE p.latitude IS NOT NULL AND p.longitude IS NOT NULL AND p.pays IS NOT NULL AND p.pays != ''");
    $stmt->execute();
    $parcs = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    $parcs = [];
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Carte Interactive des Parcs | ParcoTrip</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=" crossorigin=""/>
    <link rel="stylesheet" href="panel/mapview.css">
    <link rel="stylesheet" href="css/map.css">
</head>
<body>

    <!-- INCLUSION DU MENU SIDEBAR CENTRALISÉ AUTONOME -->
    <?php include_once 'sidebar.php'; ?>

    <!-- STRUCTURE STRICTEMENT IDENTIQUE POUR TA CARTE -->
    <main class="map-wrapper">
        <div id="map"></div>
    </main>

    <script>
        const PARCS_DATA = <?= json_encode($parcs); ?>;
    </script>
    
    <script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js" integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=" crossorigin=""></script>
    
    <script src="js/map_public.js?v=1.4"></script>
</body>
</html>