'use client';
import { useState, useEffect } from 'react';
import { motion, AnimatePresence } from 'framer-motion';
import {
Search,
MapPin,
Bed,
Bath,
Square,
DollarSign,
Filter,
Grid3x3,
List,
Heart,
Share2,
ChevronDown,
Star,
Camera,
Home,
Building2,
Trees,
Waves,
Warehouse,
Sparkles,
Shield,
Calendar,
Phone,
Mail,
MessageCircle
} from 'lucide-react';
import Image from 'next/image';
import Link from 'next/link';
import { getRentProperties, getSaleProperties } from '../utils/api';
// Map API data to UI format
function mapApiProperty(item, index) {
const info = item.propertyInformation || {};
const dailyPrice = item.dailyRent ?? item.monthlyRent ?? item.price ?? 0;
const monthlyPrice = item.monthlyRent ?? 0;
const buildingTypeMap = { 0: 'apartment', 1: 'villa', 2: 'house' };
const propType = buildingTypeMap[info.buildingType] ?? buildingTypeMap[item.type] ?? 'apartment';
const statusMap = { 0: 'available', 1: 'booked', 2: 'maintenance' };
const status = statusMap[info.status] ?? statusMap[item.status] ?? 'available';
const features = [];
if (item.isSmokeAllow) features.push('يسمح بالتدخين');
if (item.isVisitorAllow) features.push('يسمح بالزوار');
if (item.specializedFor) features.push('متخصص');
if (info.numberOfBedRooms) features.push(`${info.numberOfBedRooms} غرف نوم`);
if (info.numberOfBathRooms) features.push(`${info.numberOfBathRooms} حمامات`);
// Extract images from API and build full URLs
const apiBase = typeof window !== 'undefined' ? (process.env.NEXT_PUBLIC_API_URL || 'http://45.93.137.91/api') : '';
const rawImages = Array.isArray(info.images) ? info.images : [];
const images = rawImages.length > 0
? rawImages.map(img => img.startsWith('http') ? img : `${apiBase}${img.startsWith('/') ? '' : '/'}${img}`)
: ['/property-placeholder.jpg'];
return {
id: item.id ?? index + 1,
title: info.address || `عقار #${item.id || index + 1}`,
description: info.description || '',
type: propType,
price: dailyPrice,
priceUnit: 'daily',
location: {
city: extractCity(info.address) || 'دمشق',
district: info.address || '',
},
bedrooms: info.numberOfBedRooms || 0,
bathrooms: info.numberOfBathRooms || 0,
area: info.space || 0,
features,
images,
status,
rating: item.rating || 4.5,
isNew: false,
_raw: item,
};
}
function extractCity(address) {
if (!address) return '';
const cities = ['دمشق', 'حلب', 'حمص', 'اللاذقية', 'درعا', 'طرطوس', 'السويداء', 'دير الزور', 'الرقة', 'إدلب', 'الحسكة', 'القامشلي', 'ريف دمشق'];
for (const city of cities) {
if (address.includes(city)) return city;
}
return '';
}
// API-only — no fallback data
const PropertyCard = ({ property, viewMode = 'grid' }) => {
const [isFavorite, setIsFavorite] = useState(false);
const [currentImage, setCurrentImage] = useState(0);
const formatCurrency = (amount) => {
return amount?.toLocaleString() + ' ل.س';
};
const getPropertyTypeIcon = (type) => {
switch (type) {
case 'villa': return ;
case 'apartment': return ;
case 'house': return ;
case 'studio': return ;
default: return ;
}
};
const getPropertyTypeLabel = (type) => {
switch (type) {
case 'villa': return 'فيلا';
case 'apartment': return 'شقة';
case 'house': return 'بيت';
case 'studio': return 'استوديو';
default: return type;
}
};
if (viewMode === 'list') {
return (
{property.images.length > 1 && (
{property.images.map((_, idx) => (
)}
{getPropertyTypeIcon(property.type)}
{getPropertyTypeLabel(property.type)}
{property.status === 'available' ? 'متاح' : 'محجوز'}
{property.title}
{property.location.city}، {property.location.district}
{formatCurrency(property.price)}
/{property.priceUnit === 'daily' ? 'يوم' : 'شهر'}
{property.bedrooms} غرف
{property.bathrooms} حمامات
{property.area} م²
{property.description}
);
}
return (
{getPropertyTypeIcon(property.type)}
{getPropertyTypeLabel(property.type)}
{property.status === 'available' && (
متاح
)}
{property.title}
{property.location.city}، {property.location.district}
{formatCurrency(property.price)}
/{property.priceUnit === 'daily' ? 'يوم' : 'شهر'}
{property.bedrooms}
{property.bathrooms}
{property.area}م²
{property.rating || 4.5}
عرض التفاصيل
);
};
const FilterBar = ({ filters, onFilterChange }) => {
const [showFilters, setShowFilters] = useState(false);
const propertyTypes = [
{ id: 'all', label: 'الكل' },
{ id: 'apartment', label: 'شقة', icon: Building2 },
{ id: 'villa', label: 'فيلا', icon: Home },
{ id: 'house', label: 'بيت', icon: Home },
];
const priceRanges = [
{ id: 'all', label: 'جميع الأسعار' },
{ id: '0-500000', label: 'أقل من 500,000' },
{ id: '500000-1000000', label: '500,000 - 1,000,000' },
{ id: '1000000-2000000', label: '1,000,000 - 2,000,000' },
{ id: '2000000-5000000', label: '2,000,000 - 5,000,000' },
{ id: '5000000+', label: 'أكثر من 5,000,000' }
];
const cities = [
{ id: 'all', label: 'جميع المدن' },
{ id: 'دمشق', label: 'دمشق' },
{ id: 'حلب', label: 'حلب' },
{ id: 'حمص', label: 'حمص' },
{ id: 'اللاذقية', label: 'اللاذقية' },
{ id: 'درعا', label: 'درعا' }
];
return (
{showFilters && (
{propertyTypes.map((type) => {
const Icon = type.icon;
return (
);
})}
)}
);
};
export default function PropertiesPage() {
const [viewMode, setViewMode] = useState('grid');
const [sortBy, setSortBy] = useState('newest');
const [properties, setProperties] = useState([]);
const [loading, setLoading] = useState(true);
const [filters, setFilters] = useState({
search: '',
propertyType: 'all',
city: 'all',
priceRange: 'all',
bedrooms: 'all',
minArea: '',
maxArea: '',
features: []
});
useEffect(() => {
async function fetchProperties() {
try {
const [rentData, saleData] = await Promise.all([
getRentProperties().catch(() => []),
getSaleProperties().catch(() => []),
]);
const rentList = Array.isArray(rentData) ? rentData : [];
const saleList = Array.isArray(saleData) ? saleData : [];
const mapped = [
...rentList.map((p, i) => mapApiProperty(p, i)),
...saleList.map((p, i) => mapApiProperty(p, rentList.length + i)),
];
if (mapped.length > 0) {
setProperties(mapped);
}
} catch (err) {
console.error('[Properties] Failed to fetch properties:', err);
} finally {
setLoading(false);
}
}
fetchProperties();
}, []);
const filteredProperties = properties
.filter(property => {
if (filters.search && !property.title.includes(filters.search) && !property.description.includes(filters.search)) {
return false;
}
if (filters.propertyType !== 'all' && property.type !== filters.propertyType) {
return false;
}
if (filters.city !== 'all' && property.location.city !== filters.city) {
return false;
}
if (filters.priceRange !== 'all') {
const [min, max] = filters.priceRange.split('-');
if (max) {
if (property.price < parseInt(min) || property.price > parseInt(max)) return false;
} else if (filters.priceRange.endsWith('+')) {
const minVal = parseInt(filters.priceRange.replace('+', ''));
if (property.price < minVal) return false;
}
}
if (filters.bedrooms !== 'all' && property.bedrooms < parseInt(filters.bedrooms)) {
return false;
}
if (filters.minArea && property.area < parseInt(filters.minArea)) return false;
if (filters.maxArea && property.area > parseInt(filters.maxArea)) return false;
return true;
})
.sort((a, b) => {
switch (sortBy) {
case 'price_asc': return a.price - b.price;
case 'price_desc': return b.price - a.price;
case 'rating': return b.rating - a.rating;
default: return 0;
}
});
return (
عقارات للإيجار
أفضل العقارات في سوريا
{loading && (
)}
{filteredProperties.length} عقار متاح
{filteredProperties.map((property) => (
))}
{filteredProperties.length === 0 && (
لا توجد عقارات
جرب تغيير معايير البحث
)}
);
}