From d07e7838d0ca32818037179a867f75a89524859c Mon Sep 17 00:00:00 2001 From: marsalva Date: Sat, 7 Mar 2026 15:51:53 +0000 Subject: [PATCH] Actualizar servicios.html --- servicios.html | 85 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 1 deletion(-) diff --git a/servicios.html b/servicios.html index 795afc3..f32112d 100644 --- a/servicios.html +++ b/servicios.html @@ -808,7 +808,7 @@ } else { cancelEditing(); } - + loadChat(id); lucide.createIcons(); } @@ -1157,6 +1157,89 @@ } catch (e) { showToast("Error al guardar", "warning"); } } + // --- MOTOR DE CHAT TIPO iTRAMIT --- + async function loadChat(serviceId) { + const chatBox = document.getElementById('chatBox'); + chatBox.innerHTML = '
Cargando historial...
'; + lucide.createIcons(); + + try { + const res = await fetch(`${API_URL}/services/${serviceId}/chat`, { + headers: { 'Authorization': `Bearer ${localStorage.getItem("token")}` } + }); + const data = await res.json(); + + if (data.ok) { + chatBox.innerHTML = ''; + if (data.messages.length === 0) { + chatBox.innerHTML = '
No hay mensajes aún
'; + return; + } + + data.messages.forEach(msg => { + const isMe = msg.sender_role === 'admin' || msg.sender_role === 'superadmin'; + const time = new Date(msg.created_at).toLocaleTimeString('es-ES', { hour: '2-digit', minute: '2-digit' }); + const date = new Date(msg.created_at).toLocaleDateString('es-ES', { day: '2-digit', month: '2-digit' }); + + let bubbleClass = isMe ? 'msg-me' : 'msg-other'; + let headerColor = isMe ? 'text-emerald-700' : 'text-slate-600'; + + if (msg.is_internal) { + bubbleClass = 'msg-internal'; + headerColor = 'text-amber-700'; + } + + const bubbleHtml = ` +
+
+ ${msg.sender_name} ${msg.is_internal ? '(OCULTO)' : ''} + ${date} ${time} +
+

${msg.message}

+
+ `; + chatBox.innerHTML += bubbleHtml; + }); + + // Hacer scroll hasta abajo automáticamente + setTimeout(() => { chatBox.scrollTop = chatBox.scrollHeight; }, 100); + } + } catch (e) { + chatBox.innerHTML = '
Error al cargar el historial
'; + } + } + + async function sendChatMessage() { + const serviceId = document.getElementById('detId').value; + const input = document.getElementById('chatInput'); + const isInternal = document.getElementById('chatInternal').checked; + const message = input.value.trim(); + + if (!message) return; + + input.disabled = true; + try { + const res = await fetch(`${API_URL}/services/${serviceId}/chat`, { + method: 'POST', + headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${localStorage.getItem("token")}` }, + body: JSON.stringify({ message, is_internal: isInternal }) + }); + + if (res.ok) { + input.value = ''; + document.getElementById('chatInternal').checked = false; + loadChat(serviceId); // Recargar el chat al enviar + } else { + showToast("Error al enviar mensaje", "warning"); + } + } catch (e) { + showToast("Error de conexión", "warning"); + } finally { + input.disabled = false; + input.focus(); + } + } + \ No newline at end of file