@@ -331,26 +341,66 @@ setInterval(checkNotifications, 30000); }); + let lastUnreadCount = 0; // Para saber si el número ha subido y sonar + async function checkNotifications() { try { const res = await fetch(`${API_URL}/worker/notifications`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } }); const data = await res.json(); + if (data.ok) { activeNotifications = data.serviceIds; const badge = document.getElementById('topNotificationBadge'); + const panel = document.getElementById('topNotificationBadge').parentElement; // El contenedor para el click + if (data.unreadCount > 0) { badge.classList.remove('hidden'); badge.innerText = data.unreadCount; + + // Si hay más mensajes que antes, ¡SUENA! + if (data.unreadCount > lastUnreadCount) { + document.getElementById('notifSound').play().catch(e => console.warn("Audio bloqueado por el navegador")); + } + + // Hacer que el globo sea clicable + badge.style.cursor = 'pointer'; + badge.onclick = (e) => { e.stopPropagation(); showNotifList(); }; } else { badge.classList.add('hidden'); } - renderServices(); // Refrescamos para pintar los iconos en las tarjetas + + lastUnreadCount = data.unreadCount; + renderServices(); // Refrescamos iconos en tarjetas } } catch (e) {} } + async function showNotifList() { + const panel = document.getElementById('notifPanel'); + const list = document.getElementById('notifList'); + panel.classList.remove('hidden'); + list.innerHTML = '

Cargando...

'; + + // Obtenemos los nombres de los siniestros con mensajes + list.innerHTML = activeNotifications.map(id => { + const s = localServices.find(x => x.id === id); + const name = s ? (s.raw_data["Nombre Cliente"] || "Expediente") : "Nuevo Mensaje"; + const ref = s ? s.service_ref : id; + return ` +
+
+ #${ref} + ${name} +
+ +
+ `; + }).join(''); + safeLoadIcons(); + } + async function loadGuilds() { try { const res = await fetch(`${API_URL}/guilds`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } });