"use client"; import { useEffect, useState, useCallback } from 'react'; import Link from 'next/link'; import { motion } from 'framer-motion'; import { useTranslation } from 'react-i18next'; import { Home, MapPin, Phone, ShieldCheck, CreditCard, Banknote, Building, ArrowLeftRight, Loader2, Check, X, Calendar, Clock, LogIn, Lock, Info, Landmark, Wallet, } from 'lucide-react'; import toast, { Toaster } from 'react-hot-toast'; import AuthService from '@/app/services/AuthService'; import { payDeposit, getMyTransaction } 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', icon: Clock }, ownerConfirmed: { label: 'مؤكد من المالك', color: 'bg-blue-100 text-blue-800 border-blue-300', icon: ShieldCheck }, depositPaid: { label: 'تم دفع السلفة', color: 'bg-orange-100 text-orange-800 border-orange-300', icon: Wallet }, depositConfirmed: { label: 'تم تأكيد الدفع', color: 'bg-green-100 text-green-800 border-green-300', icon: Check }, completed: { label: 'منتهي', color: 'bg-teal-100 text-teal-800 border-teal-300', icon: Check }, cancelled: { label: 'ملغي', color: 'bg-red-100 text-red-800 border-red-300', icon: X }, }; const PAYMENT_METHODS = [ { id: 'cash', label: 'الدفع النقدي', desc: 'ادفع الآن عبر شام كاش مع تأكيد تلقائي للحجز', icon: Banknote, active: true, }, { id: 'office', label: 'نقداً', desc: 'ادفع في مكتب المنصة. يتم التأكيد لاحقاً', icon: Building, active: true, }, { id: 'transfer', label: 'تحويل داخلي', desc: 'حول من حساب داخلي ثم أرسل المرجع', icon: ArrowLeftRight, active: false, }, { id: 'electronic', label: 'دفع إلكتروني', desc: 'سيتم تفعيل الدفع الإلكتروني والتحويل لاحقاً', icon: CreditCard, active: false, } ]; function formatCurrency(v, sign = 'ل.س') { return `${sign} ${Number(v ?? 0).toLocaleString()}`; } function formatDate(date) { if (!date) return ''; const d = new Date(date); if (Number.isNaN(d.getTime())) return ''; return d.toLocaleDateString('en-GB'); } export default function PaymentsPage() { const { t } = useTranslation(); const [reservations, setReservations] = useState([]); const [loading, setLoading] = useState(true); const [payingId, setPayingId] = useState(null); const [isGuest, setIsGuest] = useState(null); const [selectedPayment, setSelectedPayment] = useState('cash'); const loadReservations = useCallback(async () => { try { const json = await getMyTransaction(); const items = Array.isArray(json) ? json : []; const mapped = items.map((item) => { const deposit = item?.diposit || item?.deposit || {}; const reservation = deposit?.reservation || {}; const transaction = deposit?.transaction || {}; const currency = item?.currency || {}; const propertyInfo = reservation?.propertyInformation || {}; return { id: deposit.id ?? reservation.id ?? item?.id, reservationId: reservation.id ?? deposit.reservationId ?? item?.reservationId, status: reservation.status ?? 0, startDate: reservation.startDate, endDate: reservation.endDate, totalPrice: reservation.totalPrice ?? transaction.amount ?? 0, depositAmount: transaction.amount ?? reservation.totalPrice ?? 0, currencySign: currency.sign || 'ل.س', currencyName: currency.name || '', currencyRate: currency.rate, propertyName: propertyInfo.name || propertyInfo.address || reservation.propertyName || `عقار #${reservation.id || ''}`, propertyAddress: propertyInfo.address || reservation.propertyAddress || '', propertyCity: propertyInfo.city || reservation.city || '', _deposit: deposit, _reservation: reservation, }; }); setReservations(mapped); } catch (err) { console.error(err); toast.error(t('payments.loadingTransactions')); } finally { setLoading(false); } }, []); useEffect(() => { if (AuthService.isGuest()) { setIsGuest(true); setLoading(false); return; } setIsGuest(false); loadReservations(); }, [loadReservations]); const handlePayDeposit = async (reservation) => { setPayingId(reservation.id); try { await payDeposit({ reservationId: reservation.id }); toast.success(t('payments.depositPaidSuccess')); loadReservations(); } catch (err) { toast.error(err?.message || t('payments.paymentFailed')); } finally { setPayingId(null); } }; const canPay = (status) => STATUS_MAP[status] === 'ownerConfirmed'; if (loading) { return (
{t('payments.loginRequiredDesc')}
{t('payments.description')}
{t('payments.noTransactionsDesc')}
{t('payments.depositAmount')}
{formatCurrency(amount, r.currencySign)}
{t('payments.depositPaidToPlatform')}
{t('payments.cashOnlyNotice')}
{t('payments.paymentMethodLabel')}
{t('payments.cashPendingDialogDesc1')}
{t('payments.cashPendingDialogDesc2')}
{t('payments.platformOfficeLabel')}
{t('payments.platformOfficeFullAddress')}
{t('payments.cashPaymentLocationLabel')}
{t('payments.platformOfficeFullAddress')}