'use client'; import { useState, useEffect, useCallback } from 'react'; import { motion } from 'framer-motion'; import { useRouter } from 'next/navigation'; import { Calendar, Clock, CheckCircle, XCircle, Eye, Loader2, Search, MapPin, DollarSign, Home, ArrowLeft, CreditCard, Timer, Star, } from 'lucide-react'; import toast, { Toaster } from 'react-hot-toast'; import AuthService from '../services/AuthService'; import { getRentProperties, getUserReservations, payDeposit } from '../utils/api'; import { addPropertyRating } from '../utils/ratings'; const API_BASE = process.env.NEXT_PUBLIC_API_URL || 'https://45.93.137.91.nip.io/api'; const STATUS_MAP = ['pending','ownerConfirmed','depositPaid','depositConfirmed','completed','cancelled']; const STATUS_UI = { pending: { label: 'قيد الانتظار', color: 'bg-yellow-100 text-yellow-800', icon: Clock }, ownerConfirmed: { label: 'مؤكد من المالك', color: 'bg-blue-100 text-blue-800', icon: CheckCircle }, depositPaid: { label: 'تم دفع السلفة', color: 'bg-indigo-100 text-indigo-800', icon: DollarSign }, depositConfirmed: { label: 'مؤكد', color: 'bg-green-100 text-green-800', icon: CheckCircle }, completed: { label: 'منتهي', color: 'bg-green-100 text-green-800', icon: CheckCircle }, cancelled: { label: 'ملغي', color: 'bg-gray-100 text-gray-800', icon: XCircle }, }; function statusLabel(code) { return STATUS_UI[STATUS_MAP[code]]?.label ?? String(code); } function statusColor(code) { return STATUS_UI[STATUS_MAP[code]]?.color ?? 'bg-gray-100 text-gray-700'; } function statusIcon(code) { return STATUS_UI[STATUS_MAP[code]]?.icon ?? Clock; } function StatusBadge({ code }) { const Icon = statusIcon(code); return ( {statusLabel(code)} ); } const propAddr = (p, r) => p?.address ?? r?.propertyAddress ?? ''; const propImages = (p, r) => { if (p?.images && Array.isArray(p.images)) return p.images; if (r?.property?.images && Array.isArray(r.property.images)) return r.property.images; return []; }; const propBeds = (p, r) => p?.numberOfBedRooms ?? r?.property?.numberOfBedRooms ?? 0; const propBaths = (p, r) => p?.numberOfBathRooms ?? r?.property?.numberOfBathRooms ?? 0; function parseTimeSpan(str) { if (!str) return 0; const clean = str.replace(/-/g, ''); const dotIdx = clean.indexOf('.'); let days = 0, timePart = clean; if (dotIdx !== -1) { days = parseInt(clean.substring(0, dotIdx), 10) || 0; timePart = clean.substring(dotIdx + 1); } const parts = timePart.split(':'); if (parts.length < 2) return days * 86400000; const hh = parseInt(parts[0], 10) || 0; const mm = parseInt(parts[1], 10) || 0; const ss = parts.length > 2 ? (parseInt(parts[2], 10) || 0) : 0; return ((days * 86400) + (hh * 3600) + (mm * 60) + ss) * 1000; } function formatWindowDuration(str) { if (!str) return ''; const clean = str.replace(/-/g, ''); const dotIdx = clean.indexOf('.'); let totalHours = 0, timePart = clean; if (dotIdx !== -1) { const days = parseInt(clean.substring(0, dotIdx), 10) || 0; totalHours += days * 24; timePart = clean.substring(dotIdx + 1); } const parts = timePart.split(':'); if (parts.length >= 2) { totalHours += parseInt(parts[0], 10) || 0; } if (totalHours > 0) return `${String(totalHours).padStart(2, '0')}:00:00`; return timePart.substring(0, 8); } function CountdownTimer({ deadline }) { const [remaining, setRemaining] = useState(deadline ? Math.max(0, deadline - Date.now()) : 0); useEffect(() => { if (!deadline) return; const tick = () => setRemaining(Math.max(0, deadline - Date.now())); tick(); const id = setInterval(tick, 1000); return () => clearInterval(id); }, [deadline]); if (remaining <= 0) return انتهت المهلة; const h = Math.floor(remaining / 3600000); const m = Math.floor((remaining % 3600000) / 60000); const s = Math.floor((remaining % 60000) / 1000); const pad = (n) => String(n).padStart(2, '0'); return {pad(h)}:{pad(m)}:{pad(s)}; } function ReservationCard({ r, onViewDetails, onPay, payingId }) { const p = r._prop; const imgs = propImages(p, r); const img = imgs.length > 0 ? `${API_BASE}${imgs[0]}` : null; const addr = propAddr(p, r); const beds = propBeds(p, r); const baths = propBaths(p, r); const isOwnerConfirmed = STATUS_MAP[r.status] === 'ownerConfirmed'; const canRate = STATUS_MAP[r.status] === 'depositPaid' || STATUS_MAP[r.status] === 'completed'; const hasTimeWindow = r.ownerApprovalDate && p?.allowedPaymentPeriod; const deadline = hasTimeWindow ? new Date(r.ownerApprovalDate).getTime() + parseTimeSpan(p.allowedPaymentPeriod) : null; const isExpired = deadline ? Date.now() > deadline : false; const isPaying = payingId === r.id; const [showRating, setShowRating] = useState(false); const [ratingValue, setRatingValue] = useState(0); const [ratingComment, setRatingComment] = useState(''); const [submittingRating, setSubmittingRating] = useState(false); return (
{img &&
}
{addr &&
{addr}
}
{r.totalPrice?.toLocaleString() ?? '—'}
السعر الإجمالي
{(beds||baths) &&
{beds>0&&{beds} غرف}{baths>0&&{baths} حمامات}
}
من
{new Date(r.startDate).toLocaleDateString('ar')}
إلى
{new Date(r.endDate).toLocaleDateString('ar')}
{isOwnerConfirmed && hasTimeWindow &&
متبقي للدفع:
مدة الدفع: {formatWindowDuration(p.allowedPaymentPeriod)}
}
{isOwnerConfirmed && !isExpired && }
{canRate && !showRating && } {canRate && showRating &&
{[1,2,3,4,5].map(n => ( ))}