"use client"; import { useState, useEffect } from "react"; import { motion, AnimatePresence } from "framer-motion"; import { useRouter } from "next/navigation"; import Link from "next/link"; import { useTranslation } from "react-i18next"; import { PlusCircle, Building, Home, DollarSign, MapPin, Bed, Bath, Square, Edit, Trash2, Eye, Calendar, TrendingUp, Users, FileText, Image as ImageIcon, CheckCircle, XCircle, AlertCircle, ChevronRight, ChevronLeft, Loader2, Clock, Wifi, Zap, Flame, Droplets, Cigarette, Dog, Music, Warehouse, Layers, Sofa, DoorOpen, Wind, Pencil, Save, X, Star, Ban, Check, School, Hospital, Store, GraduationCap, TreePine, } from "lucide-react"; import toast, { Toaster } from "react-hot-toast"; import AuthService from "../../services/AuthService"; import { buildRentPropertyPayload, getMyRentListings, getMySaleListings, editRentProperty, editSaleProperty, updateRentPropertyStatus, updateSalePropertyStatus, } from "../../utils/api"; const serviceI18nKeys = { Electricity: "propertyService.electricity", Internet: "propertyService.internet", Heating: "propertyService.heating", Water: "propertyService.water", Pool: "propertyService.pool", PrivateGarden: "propertyService.privateGarden", Parking: "propertyService.parking", Security247: "propertyService.security247", CentralHeating: "propertyService.centralHeating", CentralAirConditioning: "propertyService.centralAirConditioning", EquippedKitchen: "propertyService.equippedKitchen", MaidsRoom: "propertyService.maidsRoom", Elevator: "propertyService.elevator", Gym: "propertyService.gym", Sauna: "propertyService.sauna", Jacuzzi: "propertyService.jacuzzi", Balcony: "propertyService.balcony", Rooftop: "propertyService.rooftop", Furnished: "rentCondition.furnished", AirConditioning: "propertyService.airConditioning", SatelliteTV: "propertyService.satelliteTV", Fireplace: "propertyService.fireplace", StudyRoom: "propertyService.studyRoom", Storage: "propertyService.storage", Laundry: "propertyService.laundry", SmartHome: "propertyService.smartHome", }; const termI18nKeys = { NoSmoking: "propertyTerm.noSmoking", NoAnimals: "propertyTerm.noAnimals", NoParties: "propertyTerm.noParties", NoAlcohol: "propertyTerm.noAlcohol", SuitableForChildren: "propertyTerm.suitableForChildren", SuitableForFamilies: "propertyTerm.suitableForFamilies", SuitableForStudents: "propertyTerm.suitableForStudents", SuitableForElderly: "propertyTerm.suitableForElderly", OnlyFemales: "propertyTerm.onlyFemales", OnlyMales: "propertyTerm.onlyMales", }; const proximityI18nKeys = { School: "proximity.school", Hospital: "proximity.hospital", Restaurant: "proximity.restaurant", University: "proximity.university", Park: "proximity.park", Mall: "proximity.mall", Supermarket: "proximity.supermarket", Pharmacy: "proximity.pharmacy", Mosque: "proximity.mosque", Bank: "proximity.bank", Airport: "proximity.airport", BusStation: "proximity.busStation", }; const DeleteConfirmationModal = ({ isOpen, onClose, onConfirm, propertyTitle, }) => { const { t } = useTranslation(); if (!isOpen) return null; return ( e.stopPropagation()} >

{t('deleteConfirmTitle')}

{t('deleteConfirmMessage', { title: propertyTitle })}

{t('deleteIrreversible')}

); }; const DeactivateConfirmationModal = ({ isOpen, onClose, onConfirm, propertyTitle, isActivating, }) => { const { t } = useTranslation(); if (!isOpen) return null; return ( e.stopPropagation()} >

{isActivating ? t('activateTitle') : t('deactivateTitle')}

"{propertyTitle}"

{!isActivating ? (

⚠️ {t('warning')}

{t('deactivateWarning')}

) : (

✅ {t('activateInfoTitle')}

{t('activateInfo')}

)}
); }; const PropertyViewModal = ({ isOpen, onClose, property }) => { const { t } = useTranslation(); if (!isOpen || !property) return null; return ( e.stopPropagation()} >

{property.title}

{t('addedOn')} {new Date(property.createdAt).toLocaleDateString("ar-SA")}

{property.images && property.images.length > 0 && (

{t('propertyImages')}

{property.images.map((image, index) => (
{t('imageAlt',
))}
)}

{t('basicInfo')}

{t('propertyTypeLabel')} {property.propertyTypeLabel || t('offer')}

{t('displayTypeLabel')} {property.displayType === "Daily rent" ? t('dailyRent') : property.displayType === "Monthly rent" ? t('monthlyRent') : property.displayType === "Both" ? t('dailyAndMonthly') : property.displayType === "For sale" ? t('forSale') : property.rentType === "daily" ? t('dailyRent') : property.rentType === "monthly" ? t('monthlyRent') : (property.dailyPrice > 0 && property.monthlyPrice > 0) ? t('dailyAndMonthly') : property.monthlyPrice > 0 ? t('monthlyRent') : property.dailyPrice > 0 ? t('dailyRent') : property.purpose === "sale" ? t('forSale') : t('offer')}

{property.purpose === "rent" && (

{t('furnishedStatusLabel')} {property.furnished ? t('furnished') : t('unfurnished')}

)}

{t('propertyStatusLabel')} {property.status === "available" ? t('available') : t('rented')}

{property.description && (

{t('descriptionLabel')} {property.description}

)}

{t('basicInfo')}

{property.bedrooms} {t('rooms')}
{property.bathrooms} {t('bathrooms')}
{property.area} {t('sqm')}
{property.floor > 0 && (
{property.floor} {t('floor')}
)} {property.salons > 0 && (
{property.salons} {t('salons')}
)} {property.balconies > 0 && (
{property.balconies} {t('balconies')}
)} {property.bookedCount > 0 && (
{property.bookedCount} {t('bookings')}
)}

{t('location')}

{property.address || t('addressNotSpecified')} {property.city && `، ${property.city}`} {property.district && `، ${property.district}`}

{property.services && (Array.isArray(property.services) ? property.services.length > 0 : Object.keys(property.services).length > 0) && (

{t('availableServices')}

{Array.isArray(property.services) ? property.services.map((svc, i) => ( {t(serviceI18nKeys[svc]) || svc} )) : Object.entries(property.services).map(([key, value]) => { if (!value) return null; const detail = typeof value === "object" && value.detail ? value.detail : typeof value === "string" ? value : null; return ( {t(serviceI18nKeys[key]) || key} {detail && ( · {detail} )} ); })}
)} {property.proximity && Object.keys(property.proximity).length > 0 && (

{t('proximityToServices')}

{Object.entries(property.proximity).map(([key, val]) => { if (!val) return null; const dist = typeof val === "object" ? val.distance : val; return (
{key === "School" && } {key === "Hospital" && } {key === "Restaurant" && } {key === "University" && } {key === "Park" && } {key === "Mall" && } {!["School","Hospital","Restaurant","University","Park","Mall"].includes(key) && }
{t(proximityI18nKeys[key]) || key} {dist} {typeof dist === "number" ? t('km') : ""}
); })}
)} {property.terms && Object.keys(property.terms).length > 0 && (

{t('termsOfUse')}

{Object.entries(property.terms).map(([key, value]) => { if (!value) return null; return (
{key.startsWith("No") || key.startsWith("Only") ? ( ) : ( )} {t(termI18nKeys[key]) || key}
); })}
)}

{t('priceInfo')}

{property.purpose === "rent" ? (
{property.dailyPrice > 0 && (

{t('dailyPriceLabel')} {Number(property.dailyPrice).toLocaleString()} {t('syp')}

)} {property.monthlyPrice > 0 && (

{t('monthlyPriceLabel')} {Number(property.monthlyPrice).toLocaleString()} {t('syp')}

)} {property.deposit > 0 && (

{t('depositLabel')} {Number(property.deposit).toLocaleString()} {t('syp')}

)}

{t('rentTypeLabel')}{" "} {property.rentType === "daily" ? t('daily') : property.rentType === "monthly" ? t('monthly') : t('dailyAndMonthly')}

{property.rating > 0 && (

{t('ratingLabel')} {Number(property.rating).toFixed(1)}{" "}

)}
) : (

{t('salePriceLabel')} {Number(property.salePrice).toLocaleString()} {t('syp')}

)}
); }; const PropertyEditModal = ({ isOpen, onClose, property, onSave }) => { const { t } = useTranslation(); const [formData, setFormData] = useState({ propertyType: 'apartment', furnished: false, description: '', bedrooms: 0, bathrooms: 0, floor: 0, salons: 0, balconies: 0, area: 0, services: {}, serviceDetails: {}, terms: {}, customTerms: [], nearbySchool: '', nearbyHospital: '', nearbyRestaurant: '', nearbyUniversity: '', nearbyPark: '', nearbyMall: '', purpose: 'rent', currencyId: 1, dailyPrice: 0, monthlyPrice: 0, deposit: 0, rentType: 'monthly', allowedPaymentPeriod: '', salePrice: 0, }); const [newCustomTerm, setNewCustomTerm] = useState(''); const [isSaving, setIsSaving] = useState(false); useEffect(() => { if (!property || !isOpen) return; const raw = property._raw || {}; const info = raw.propertyInformation || {}; let details = {}; try { details = typeof info.detailsJSON === 'object' && info.detailsJSON ? info.detailsJSON : JSON.parse(info.detailsJSON || '{}'); } catch { details = {}; } const propServices = property.services || {}; const services = {}; const serviceDetails = {}; Object.keys(serviceI18nKeys).forEach((key) => { const val = propServices[key]; if (val && typeof val === 'string') { services[key] = true; serviceDetails[key] = val; } else if (val && typeof val === 'object' && val.detail) { services[key] = true; serviceDetails[key] = val.detail; } else { services[key] = !!val; serviceDetails[key] = details.serviceDetails?.[key] || ''; } }); const propTerms = property.terms || {}; const terms = {}; Object.keys(termI18nKeys).forEach((key) => { terms[key] = !!propTerms[key]; }); const prox = property.proximity || details.nearbyDistances || {}; setFormData({ propertyType: property.propertyType || 'apartment', furnished: property.furnished ?? false, description: property.description || '', bedrooms: property.bedrooms || 0, bathrooms: property.bathrooms || 0, floor: property.floor ?? details.floorNumber ?? details.floor ?? 0, salons: property.salons ?? details.numberOfSalons ?? details.salons ?? 0, balconies: property.balconies ?? details.numberOfBalconies ?? details.balconies ?? 0, area: property.area || 0, services, serviceDetails, terms, customTerms: details.customTerms || [], nearbySchool: prox.School ?? prox.school ?? '', nearbyHospital: prox.Hospital ?? prox.hospital ?? '', nearbyRestaurant: prox.Restaurant ?? prox.restaurant ?? '', nearbyUniversity: prox.University ?? prox.university ?? '', nearbyPark: prox.Park ?? prox.park ?? '', nearbyMall: prox.Mall ?? prox.mall ?? '', purpose: property.purpose || 'rent', currencyId: property.currencyId || 1, ...(property.purpose === 'rent' ? { dailyPrice: property.dailyPrice || 0, monthlyPrice: property.monthlyPrice || 0, deposit: property.deposit || 0, rentType: property.rentType || 'monthly', allowedPaymentPeriod: property.allowedPaymentPeriod || details.allowedPaymentPeriod || '', } : { salePrice: property.salePrice || 0 }), }); setNewCustomTerm(''); }, [property, isOpen]); const handleChange = (field, value) => { setFormData((prev) => ({ ...prev, [field]: value })); }; const handleServiceToggle = (key, checked) => { setFormData((prev) => ({ ...prev, services: { ...prev.services, [key]: checked }, })); }; const handleServiceDetail = (key, value) => { setFormData((prev) => ({ ...prev, serviceDetails: { ...prev.serviceDetails, [key]: value }, })); }; const handleTermToggle = (key, checked) => { setFormData((prev) => ({ ...prev, terms: { ...prev.terms, [key]: checked }, })); }; const addCustomTerm = () => { const term = newCustomTerm.trim(); if (!term) return; setFormData((prev) => ({ ...prev, customTerms: [...(prev.customTerms || []), term], })); setNewCustomTerm(''); }; const removeCustomTerm = (index) => { setFormData((prev) => ({ ...prev, customTerms: prev.customTerms.filter((_, i) => i !== index), })); }; const handleSave = async () => { setIsSaving(true); try { await onSave(formData); } catch { setIsSaving(false); } }; if (!isOpen || !property) return null; return ( e.stopPropagation()} >

{t('editProperty')}

{t('editSubtitle')}

{/* Basic Info */}

{t('basicInfo')}

{[ { value: true, label: t('furnished') }, { value: false, label: t('unfurnished') }, ].map((opt) => ( ))}