'use client'; import { useEffect, useState } from 'react'; import { useRouter } from 'next/navigation'; import { motion, AnimatePresence } from 'framer-motion'; import { Bell, CheckCircle, XCircle, Calendar, MessageCircle, CheckCheck, Loader2 } from 'lucide-react'; import AuthService from '@/app/services/AuthService'; import { getUserNotifications } from '@/app/utils/api'; export default function NotificationsPage() { const router = useRouter(); const [notifications, setNotifications] = useState([]); const [unreadCount, setUnreadCount] = useState(0); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); useEffect(() => { if (!AuthService.isAuthenticated()) { router.push('/login'); return; } fetchNotifications(); }, [router]); const fetchNotifications = async () => { setIsLoading(true); setError(null); try { const data = await getUserNotifications(); const items = Array.isArray(data) ? data : []; setNotifications(items); setUnreadCount(items.length); } catch (err) { console.error('Error fetching notifications:', err); setError(err.message || 'فشل تحميل الإشعارات'); } finally { setIsLoading(false); } }; const markAsRead = (id) => { setNotifications(prev => prev.map(n => (n.id === id ? { ...n, read: true } : n)) ); setUnreadCount(prev => Math.max(0, prev - 1)); }; const markAllAsRead = () => { setNotifications(prev => prev.map(n => ({ ...n, read: true }))); setUnreadCount(0); }; if (isLoading) { return (

جاري التحميل...

); } if (error) { return (

خطأ في التحميل

{error}

إعادة المحاولة
); } return (

الإشعارات

{unreadCount > 0 ? `لديك ${unreadCount} إشعار غير مقروء` : 'جميع الإشعارات مقروءة'}

{unreadCount > 0 && ( تحديد الكل كمقروء )}
{notifications.length === 0 ? (

لا توجد إشعارات

ستظهر هنا الإشعارات المتعلقة بحجوزاتك ومدفوعاتك

) : (
{notifications.map((notification, index) => ( markAsRead(notification.id)} className={`bg-white rounded-2xl shadow-sm border transition-all hover:shadow-md cursor-pointer ${ !notification.read ? 'border-amber-200 bg-amber-50/50' : 'border-gray-200' }`} >
{!notification.read ? ( ) : ( )}

{notification.title}

{notification.message && (

{notification.message}

)}
{notification.date && ( {notification.date} )} {notification.type && ( {notification.type} )}
{!notification.read && ( )}
))}
)}
); }