// '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(() => {
// // Admin check removed
// // 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) && (
//
//
//
// )}
//
// );
// })}
//
// )}
//
//
// );
// }
'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 { 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);
const getAuthToken = () => {
if (typeof window === 'undefined') return '';
return (
AuthService?.getToken?.() ||
AuthService?.getAccessToken?.() ||
localStorage.getItem('token') ||
localStorage.getItem('accessToken') ||
localStorage.getItem('authToken') ||
''
);
};
const loadReservations = useCallback(async () => {
try {
const token = getAuthToken();
const res = await fetch('http://45.93.137.91/api/Customer/GetMyTransaction', {
method: 'GET',
headers: {
...(token ? { Authorization: `Bearer ${token}` } : {}),
},
});
if (!res.ok) {
throw new Error('فشل تحميل المدفوعات');
}
const json = await res.json();
const items = Array.isArray(json?.data) ? json.data : 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 || {};
return {
id: reservation.id ?? deposit.reservationId ?? deposit?.reservation?.id ?? item?.reservationId ?? deposit?.id,
reservationId: reservation.id ?? deposit.reservationId ?? item?.reservationId ?? deposit?.id,
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,
_deposit: deposit,
};
});
setReservations(mapped);
} catch (err) {
console.error(err);
toast.error('فشل تحميل المدفوعات');
} finally {
setLoading(false);
}
}, []);
useEffect(() => {
// Admin check removed
// if (AuthService.isAdmin()) {
// router.push('/');
// return;
// }
loadReservations();
}, [router, loadReservations]);
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, sign = 'ل.س') => `${sign} ${Number(v ?? 0).toLocaleString()}`;
const formatDate = (date) => {
if (!date) return '';
const d = new Date(date);
if (Number.isNaN(d.getTime())) return '';
return d.toLocaleDateString('en-GB');
};
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.reservationId || r.id}
{cfg.depositPaid ? : }
{cfg.label}
Reservation
#{r.reservationId || r.id}
{r.currencyName}
{formatCurrency(amount, r.currencySign)}
الفترة
{formatDate(r.startDate)} - {formatDate(r.endDate)}
Reservation
#{r.reservationId || r.id}
{canPay(r.status) && (
)}
);
})}
)}
);
}