diff --git a/app/owner/properties/add/page.js b/app/owner/properties/add/page.js index b0d4e19..fd74c6b 100644 --- a/app/owner/properties/add/page.js +++ b/app/owner/properties/add/page.js @@ -51,6 +51,7 @@ import { Move } from 'lucide-react'; import toast, { Toaster } from 'react-hot-toast'; +import { addRentProperty } from '../../../utils/api'; const MapContainer = dynamic(() => import('react-leaflet').then(mod => mod.MapContainer), { ssr: false }); const TileLayer = dynamic(() => import('react-leaflet').then(mod => mod.TileLayer), { ssr: false }); @@ -499,16 +500,65 @@ const handleMapClick = async (coords) => { if (!validateStep()) return; setIsLoading(true); + console.log('[AddProperty] Building RentPropertyDto payload...'); - setTimeout(() => { - console.log('Property Data:', formData); - setIsLoading(false); + // Map UI BuildingType to API enum: 0=Apartment, 1=Villa, 2=House + const buildingTypeMap = { apartment: 0, villa: 1, suite: 0, room: 0 }; + // Map UI offerType to API RentType: 0=day, 1=week, 2=month + const rentTypeMap = { daily: 0, monthly: 2, both: 2, sale: 2 }; + // Map UI propertyType to API RentPropertyType: 0=Family, 1=Person + const rentPropertyType = formData.terms?.suitableForChildren ? 0 : 1; + + // Services/terms go into DetailsJSON + const detailsJSON = JSON.stringify({ + services: formData.services, + terms: formData.terms, + furnished: formData.furnished, + livingRooms: formData.livingRooms, + }); + + const payload = { + PropertyInformation: { + CordsX: formData.lat ? String(formData.lat) : '', + CordsY: formData.lng ? String(formData.lng) : '', + Address: `${formData.city} - ${formData.district} - ${formData.address}`.trim(), + Description: formData.description || '', + NumberOfBathRooms: formData.bathrooms || 0, + NumberOfRooms: (formData.bedrooms || 0) + (formData.livingRooms || 0), + NumberOfBedRooms: formData.bedrooms || 0, + Space: parseFloat(formData.space) || 0, + DetailsJSON: detailsJSON, + BuildingType: buildingTypeMap[formData.propertyType] ?? 0, + Status: 0, + PropertyType: formData.offerType === 'sale' ? 1 : 0, + }, + Deposit: parseFloat(formData.deposit) || 0, + MonthlyRent: parseFloat(formData.monthlyPrice) || 0, + DailyRent: parseFloat(formData.dailyPrice) || 0, + Rating: 0, + CurrencyId: 1, + RentType: rentTypeMap[formData.offerType] ?? 0, + IsSmokeAllow: !formData.terms?.noSmoking, + SpecializedFor: false, + IsVisitorAllow: !formData.terms?.noParties, + Type: rentPropertyType, + }; + + console.log('[AddProperty] Payload:', JSON.stringify(payload, null, 2)); + + try { + const res = await addRentProperty(payload); + console.log('[AddProperty] API response:', res); toast.success('تم إضافة العقار بنجاح!'); - setTimeout(() => { router.push('/owner/properties'); }, 1500); - }, 2000); + } catch (err) { + console.error('[AddProperty] API error:', err); + toast.error(err.message || 'فشل في إضافة العقار'); + } finally { + setIsLoading(false); + } }; const fadeInUp = { diff --git a/app/utils/api.js b/app/utils/api.js index aeb018b..2a6bc9b 100644 --- a/app/utils/api.js +++ b/app/utils/api.js @@ -169,6 +169,16 @@ export async function getOwnerByUserId(userId) { return apiFetch(`/Owner/GetByUserId/${userId}`); } +// ─── Properties ─── + +export async function addRentProperty(data) { + console.log('[API] Adding rent property:', data.PropertyInformation?.Address); + return apiFetch('/RentProperties/AddRentProperty', { + method: 'POST', + body: JSON.stringify(data), + }); +} + // ─── Auth: Registration ─── /**