'use client'; import { useEffect, useState, useCallback } from 'react'; import { useRouter } from 'next/navigation'; import { motion } from 'framer-motion'; import { CreditCard, Loader2, Home, Calendar, Check, X, Clock } from 'lucide-react'; import toast, { Toaster } from 'react-hot-toast'; import AuthService from '@/app/services/AuthService'; import { getUserReservations, payDeposit } from '@/app/utils/api'; const STATUS_MAP = ['pending', 'ownerConfirmed', 'depositPaid', 'depositConfirmed', 'completed', 'cancelled']; const STATUS_CONFIG = { pending: { label: 'قيد الانتظار', color: 'bg-yellow-100 text-yellow-800 border-yellow-300', depositPaid: false }, ownerConfirmed: { label: 'مؤكد من المالك', color: 'bg-blue-100 text-blue-800 border-blue-300', depositPaid: false }, depositPaid: { label: 'تم دفع السلفة', color: 'bg-orange-100 text-orange-800 border-orange-300', depositPaid: true }, depositConfirmed: { label: 'تم تأكيد الدفع', color: 'bg-green-100 text-green-800 border-green-300', depositPaid: true }, completed: { label: 'منتهي', color: 'bg-teal-100 text-teal-800 border-teal-300', depositPaid: true }, cancelled: { label: 'ملغي', color: 'bg-red-100 text-red-800 border-red-300', depositPaid: false }, }; export default function PaymentsPage() { const router = useRouter(); const [reservations, setReservations] = useState([]); const [loading, setLoading] = useState(true); const [payingId, setPayingId] = useState(null); useEffect(() => { if (AuthService.isAdmin()) { router.push('/'); return; } loadReservations(); }, [router]); const loadReservations = useCallback(async () => { try { const data = await getUserReservations(); setReservations(Array.isArray(data) ? data : []); } catch (err) { console.error(err); toast.error('فشل تحميل المدفوعات'); } finally { setLoading(false); } }, []); const handlePayDeposit = async (reservation) => { setPayingId(reservation.id); try { await payDeposit({ reservationId: reservation.id }); toast.success('تم دفع السلفة بنجاح!'); loadReservations(); } catch (err) { toast.error(err?.message || 'فشل عملية الدفع'); } finally { setPayingId(null); } }; const formatCurrency = (v) => (v ?? 0).toLocaleString() + ' ل.س'; if (loading) { return (
); } const canPay = (status) => STATUS_MAP[status] === 'pending' || STATUS_MAP[status] === 'ownerConfirmed'; return (

المدفوعات

إدارة مدفوعات الحجوزات والدفعات المقدمة

{reservations.length === 0 ? (

لا توجد معاملات مالية

ستظهر هنا مدفوعاتك للحجوزات

) : (
{reservations.map((r, i) => { const statusKey = STATUS_MAP[r.status] || 'pending'; const cfg = STATUS_CONFIG[statusKey]; const amount = r.depositAmount || r.totalPrice || 0; return (

{r.propertyAddress || r._prop?.address || `عقار #${r.propertyId || r.id}`}

حجز #{r.id}

{new Date(r.startDate).toLocaleDateString('ar')} {new Date(r.endDate).toLocaleDateString('ar')}
{formatCurrency(amount)}
{cfg.depositPaid ? : } {cfg.label}
{canPay(r.status) && (
)}
); })}
)}
); }