const NotificationsUI = { notifications: [], unreadCount: 0, pollInterval: null, init() { this.setupEventListeners(); this.startPolling(); }, setupEventListeners() { const bell = document.getElementById('notification-bell'); const panel = document.getElementById('notification-panel'); if (bell) { bell.addEventListener('click', (e) => { e.stopPropagation(); panel.classList.toggle('hidden'); if (!panel.classList.contains('hidden')) this.load(); }); } document.addEventListener('click', (e) => { if (panel && !panel.contains(e.target) && e.target !== bell) { panel.classList.add('hidden'); } }); const markAllBtn = document.getElementById('mark-all-read'); if (markAllBtn) { markAllBtn.addEventListener('click', () => this.markAllRead()); } const clearBtn = document.getElementById('clear-notifications'); if (clearBtn) { clearBtn.addEventListener('click', () => this.clearAll()); } }, startPolling() { this.pollInterval = setInterval(() => this.loadCount(), 15000); this.loadCount(); }, async loadCount() { try { const data = await API.request('GET', '/notifications'); this.unreadCount = data.unreadCount || 0; this.updateBadge(); } catch (e) {} }, async load() { try { const data = await API.request('GET', '/notifications'); this.notifications = data.notifications || []; this.unreadCount = data.unreadCount || 0; this.updateBadge(); this.render(); } catch (e) { console.error('Erro ao carregar notificações:', e); } }, updateBadge() { const badge = document.getElementById('notification-badge'); if (!badge) return; if (this.unreadCount > 0) { badge.textContent = this.unreadCount > 99 ? '99+' : this.unreadCount; badge.classList.remove('hidden'); } else { badge.classList.add('hidden'); } }, render() { const list = document.getElementById('notification-list'); if (!list) return; if (this.notifications.length === 0) { list.innerHTML = '