'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 (
جاري التحميل...
{error}
{unreadCount > 0 ? `لديك ${unreadCount} إشعار غير مقروء` : 'جميع الإشعارات مقروءة'}
ستظهر هنا الإشعارات المتعلقة بحجوزاتك ومدفوعاتك
{notification.message}
)}