import React, { useState, useEffect } from 'react'; import { Bell, Search, Filter, CheckCircle2, Trash2, Calendar, DollarSign, UserCheck, ShieldAlert, MessageSquare, Inbox, ArrowRight, MoreHorizontal } from 'lucide-react'; import { HybridBackend } from '../services/backend.ts'; import { Notificacao } from '../types.ts'; import { useToast } from '../contexts/ToastContext.tsx'; import { PageHeader } from '../components/PageHeader.tsx'; export const NotificationsView: React.FC = () => { const [notificacoes, setNotificacoes] = useState([]); const [loading, setLoading] = useState(true); const [searchTerm, setSearchTerm] = useState(''); const [activeTab, setActiveTab] = useState<'all' | 'unread' | 'important'>('all'); const [filterType, setFilterType] = useState('all'); const toast = useToast(); const fetchNotificacoes = async () => { setLoading(true); try { const data = await HybridBackend.getNotifications(); // Data de mais nova para mais antiga setNotificacoes(data.sort((a, b) => new Date(b.data).getTime() - new Date(a.data).getTime())); } catch (err) { toast.error("Erro ao carregar notificações"); } finally { setLoading(false); } }; useEffect(() => { fetchNotificacoes(); }, []); const markAsRead = async (id: string) => { try { await HybridBackend.markNotificationRead(id); setNotificacoes(prev => prev.map(n => n.id === id ? { ...n, lida: true } : n)); toast.success("Notificação marcada como lida"); } catch (err) { toast.error("Erro ao atualizar notificação"); } }; const markAllAsRead = async () => { const unread = notificacoes.filter(n => !n.lida); if (unread.length === 0) return; try { await HybridBackend.markAllNotificationsRead(); setNotificacoes(prev => prev.map(n => ({ ...n, lida: true }))); toast.success("Todas as notificações foram marcadas como lidas"); } catch (err) { toast.error("Erro ao atualizar notificações"); } }; const deleteNotification = async (id: string) => { try { await HybridBackend.deleteNotification(id); setNotificacoes(prev => prev.filter(n => n.id !== id)); toast.success("Notificação removida"); } catch (err) { toast.error("Erro ao excluir notificação"); } }; const getIcon = (tipo: string) => { switch (tipo) { case 'agenda': return ; case 'financeiro': return ; case 'lead': return ; case 'sistema': return ; default: return ; } }; const filteredNotificacoes = notificacoes.filter(n => { const matchesSearch = n.titulo.toLowerCase().includes(searchTerm.toLowerCase()) || n.mensagem.toLowerCase().includes(searchTerm.toLowerCase()); const matchesTab = activeTab === 'all' || (activeTab === 'unread' && !n.lida) || (activeTab === 'important' && n.tipo === 'sistema'); const matchesType = filterType === 'all' || n.tipo === filterType; return matchesSearch && matchesTab && matchesType; }); const unreadCount = notificacoes.filter(n => !n.lida).length; return (
{/* Stats Dashboard */}
{[ { label: 'Total', count: notificacoes.length, icon: Inbox, color: 'text-slate-600', bg: 'bg-slate-100' }, { label: 'Não Lidas', count: unreadCount, icon: Bell, color: 'text-blue-600', bg: 'bg-blue-50' }, { label: 'Sistema', count: notificacoes.filter(n => n.tipo === 'sistema').length, icon: ShieldAlert, color: 'text-red-600', bg: 'bg-red-50' }, { label: 'Financeiro', count: notificacoes.filter(n => n.tipo === 'financeiro').length, icon: DollarSign, color: 'text-emerald-600', bg: 'bg-emerald-50' }, ].map((stat, i) => (

{stat.label}

{stat.count}

))}
{/* Filters Bar */}
{[ { id: 'all', label: 'Todas' }, { id: 'unread', label: 'Não Lidas' }, { id: 'important', label: 'Importantes' }, ].map(tab => ( ))}
setSearchTerm(e.target.value)} className="w-full pl-12 pr-4 py-3.5 bg-white border border-slate-200 rounded-2xl text-sm font-bold uppercase tracking-tight focus:ring-4 focus:ring-blue-50 focus:border-blue-500 outline-none transition-all placeholder:text-slate-300" />
{/* List */}
{loading ? (

Carregando Histórico...

) : filteredNotificacoes.length === 0 ? (

Nenhuma notificação encontrada

Não existem registros que correspondam aos seus filtros ou pesquisa no momento.

) : (
{filteredNotificacoes.map((notif, idx) => (
{!notif.lida && (
)}
{getIcon(notif.tipo)}
{notif.tipo} {new Date(notif.data).toLocaleDateString('pt-BR', { day: '2-digit', month: 'short', year: 'numeric' })} {new Date(notif.data).toLocaleTimeString('pt-BR', { hour: '2-digit', minute: '2-digit' })}

{notif.titulo}

{notif.mensagem}

{!notif.lida && ( )}
))}
)}
{/* Footer Info */}

Mostrando {filteredNotificacoes.length} de {notificacoes.length} registros

); }; const Clock = ({ size, className }: { size: number, className?: string }) => ( );