'use client'; import { useState, useEffect, useRef } from 'react'; import L from 'leaflet'; import 'leaflet/dist/leaflet.css'; delete L.Icon.Default.prototype._getIconUrl; L.Icon.Default.mergeOptions({ iconRetinaUrl: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-icon-2x.png', iconUrl: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-icon.png', shadowUrl: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/images/marker-shadow.png', }); export default function PropertyMapWithMarkers({ properties = [], onPropertyClick }) { const mapRef = useRef(null); const mapInstanceRef = useRef(null); const markersRef = useRef([]); const [mapLoaded, setMapLoaded] = useState(false); useEffect(() => { if (!mapRef.current || mapInstanceRef.current) return; const map = L.map(mapRef.current).setView([33.5138, 38.9968], 7); L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { attribution: '© OpenStreetMap', maxZoom: 19, }).addTo(map); mapInstanceRef.current = map; setMapLoaded(true); return () => { if (mapInstanceRef.current) { mapInstanceRef.current.remove(); mapInstanceRef.current = null; } }; }, []); useEffect(() => { if (!mapInstanceRef.current || !mapLoaded) return; markersRef.current.forEach(marker => marker.remove()); markersRef.current = []; properties.forEach(property => { if (property.lat && property.lng) { const marker = L.marker([property.lat, property.lng]).addTo(mapInstanceRef.current); const popupContent = `

${property.title || 'عقار'}

${property.address || property.location?.address || ''}

${formatPrice(property)}
${property.images && property.images.length > 0 ? `${property.title}` : ''}
${property.type ? `

النوع: ${property.type}

` : ''} ${property.bedrooms > 0 ? `

غرف نوم: ${property.bedrooms}

` : ''} ${property.bathrooms > 0 ? `

حمامات: ${property.bathrooms}

` : ''}
`; marker.bindPopup(popupContent); marker.on('click', () => { if (onPropertyClick) { onPropertyClick(property); } }); markersRef.current.push(marker); } }); if (markersRef.current.length > 0) { const group = L.featureGroup(markersRef.current); mapInstanceRef.current.fitBounds(group.getBounds(), { padding: [50, 100] }); } }, [properties, mapLoaded]); const formatPrice = (property) => { if (property.priceUnit === 'monthly') { return `${property.price?.toLocaleString() || 0} ل.س/شهر`; } else if (property.priceUnit === 'daily') { return `${property.price?.toLocaleString() || 0} ل.س/يوم`; } else { return `${property.price?.toLocaleString() || 0} ل.س`; } }; return (
); }