// Variables globales let map; let draw; let currentMarker = null; // Initialisation quand le DOM est chargé document.addEventListener('DOMContentLoaded', function() { initializeForm(); initializeMap(); setupEventHandlers(); }); function initializeForm() { if (!window.eventData) { console.error('Aucune donnée d\'événement disponible'); return; } const properties = window.eventData.properties || {}; const geometry = window.eventData.geometry || {}; // Remplir les champs du formulaire document.getElementById('label').value = properties.label || ''; document.getElementById('type').value = properties.type || 'scheduled'; document.getElementById('what').value = properties.what || ''; document.getElementById('what_series').value = properties['what:series'] || ''; document.getElementById('where').value = properties.where || ''; // Convertir les dates pour les inputs datetime-local if (properties.start) { const startDate = new Date(properties.start); document.getElementById('start').value = formatDateTimeLocal(startDate); } if (properties.stop) { const stopDate = new Date(properties.stop); document.getElementById('stop').value = formatDateTimeLocal(stopDate); } console.log('✅ Formulaire initialisé avec les données de l\'événement'); } function formatDateTimeLocal(date) { // Convertir une date en format compatible avec datetime-local const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); const day = String(date.getDate()).padStart(2, '0'); const hours = String(date.getHours()).padStart(2, '0'); const minutes = String(date.getMinutes()).padStart(2, '0'); return `${year}-${month}-${day}T${hours}:${minutes}`; } function initializeMap() { if (!window.maplibregl) { console.error('MapLibre GL JS non disponible'); return; } const geometry = window.eventData?.geometry || {}; const coordinates = geometry.coordinates || [2.3522, 48.8566]; // Paris par défaut // Initialiser la carte map = new maplibregl.Map({ container: 'map', style: 'https://tiles.openfreemap.org/styles/liberty', center: coordinates, zoom: coordinates[0] === 0 && coordinates[1] === 0 ? 2 : 12 }); // Ajouter les contrôles map.addControl(new maplibregl.NavigationControl()); map.addControl(new maplibregl.GeolocateControl({ positionOptions: { enableHighAccuracy: true } })); // Ajouter un marqueur pour la position actuelle if (coordinates[0] !== 0 || coordinates[1] !== 0) { currentMarker = new maplibregl.Marker() .setLngLat(coordinates) .addTo(map); } // Gérer les clics sur la carte pour changer la position map.on('click', function(e) { const coords = [e.lngLat.lng, e.lngLat.lat]; // Supprimer l'ancien marqueur if (currentMarker) { currentMarker.remove(); } // Ajouter un nouveau marqueur currentMarker = new maplibregl.Marker() .setLngLat(coords) .addTo(map); console.log('📍 Position mise à jour:', coords); }); console.log('✅ Carte initialisée'); } function setupEventHandlers() { // Gestionnaire de soumission du formulaire const form = document.getElementById('eventForm'); if (form) { form.addEventListener('submit', handleFormSubmit); } // Gestionnaire du bouton de suppression const deleteButton = document.getElementById('deleteButton'); if (deleteButton) { deleteButton.addEventListener('click', handleDelete); } } async function handleFormSubmit(e) { e.preventDefault(); const resultElement = document.getElementById('result'); resultElement.innerHTML = '