import React, { useEffect, useState } from 'react'; import { MapContainer, TileLayer, Marker, Popup, useMap } from 'react-leaflet'; import L from 'leaflet'; import 'leaflet/dist/leaflet.css'; import { useTourStore } from './useTourStore.js'; import { X, Navigation, Image as ImageIcon, LogOut, Settings, Edit2, Trash2 } from 'lucide-react'; import { UserManagementModal } from './UserManagementModal.js'; import { CreateTourModal } from './CreateTourModal.js'; // Fix lỗi icon mặc định của Leaflet const DefaultIcon = L.icon({ iconUrl: 'https://unpkg.com/leaflet@1.9.4/dist/images/marker-icon.png', shadowUrl: 'https://unpkg.com/leaflet@1.9.4/dist/images/marker-shadow.png', iconSize: [25, 41], iconAnchor: [12, 41], }); L.Marker.prototype.options.icon = DefaultIcon; // Component Helper để cập nhật tâm bản đồ khi vị trí người dùng thay đổi function RecenterMap({ position }: { position: [number, number] }) { const map = useMap(); useEffect(() => { map.setView(position, map.getZoom()); }, [position, map]); return null; } export const ExploreMap = ({ onBack, onLogout, user, onViewTour }: { onBack: () => void, onLogout?: () => void, user?: any, onViewTour: (id: string) => void }) => { const { publicTours, fetchPublicTours, fetchTour, updateTour, deleteTour } = useTourStore(); const [userPos, setUserPos] = useState<[number, number]>([10.7769, 106.7009]); // Mặc định TP.HCM const [isAdminModalOpen, setIsAdminModalOpen] = useState(false); const [isCreateModalOpen, setIsCreateModalOpen] = useState(false); useEffect(() => { fetchPublicTours(); navigator.geolocation.getCurrentPosition( (pos) => setUserPos([pos.coords.latitude, pos.coords.longitude]), () => console.log("Không thể lấy vị trí người dùng") ); }, []); const handleEditTour = async (tour: any) => { const newTitle = window.prompt("Nhập tên mới cho Tour:", tour.title); if (newTitle && newTitle !== tour.title) { try { await updateTour(tour.id, { title: newTitle }); } catch (err: any) { alert(err.message); } } }; const handleDeleteTour = async (id: string) => { if (window.confirm("Bạn có chắc chắn muốn xóa Tour này và toàn bộ dữ liệu liên quan?")) { try { await deleteTour(id); } catch (err: any) { alert(err.message); } } }; return (