diff --git a/admin/DashboardController.php b/admin/DashboardController.php index 7c2c1df..012ae26 100644 --- a/admin/DashboardController.php +++ b/admin/DashboardController.php @@ -1920,28 +1920,43 @@ let activePhone = ''; // ─── Load conversations ────────────────────────────────────────────────────── async function loadConvs() { - const r = await fetch('/admin/chat/conversations'); - const data = await r.json(); const list = document.getElementById('convList'); - list.innerHTML = data.conversations.map(c => { - const time = formatTime(c.last_time); - const avatar = (c.contact_name || c.phone).charAt(0).toUpperCase(); - const unread = c.unread_count > 0 ? \`
\${c.unread_count}
\` : ''; - const active = c.phone === activePhone ? ' active' : ''; - const company = c.company_name ? \`\${esc(c.company_name)}\` : ''; - return \`
-
\${avatar}
-
-
\${esc(c.contact_name || 'Desconocido')} \${company}
-
\${c.phone}
-
\${esc(c.last_message || '')}
-
-
-
\${time}
- \${unread} -
-
\`; - }).join(''); + try { + const r = await fetch('/admin/chat/conversations'); + if (!r.ok) { + list.innerHTML = \`
Error \${r.status} al cargar conversaciones
\`; + return; + } + const data = await r.json(); + const convs = data.conversations || []; + if (convs.length === 0) { + list.innerHTML = '
Sin conversaciones aún
'; + return; + } + list.innerHTML = convs.map(c => { + const time = formatTime(c.last_time); + const name = c.contact_name || c.phone || ''; + const avatar = name.charAt(0).toUpperCase() || '?'; + const unread = c.unread_count > 0 ? \`
\${c.unread_count}
\` : ''; + const active = c.phone === activePhone ? ' active' : ''; + const company = c.company_name ? \`\${esc(c.company_name)}\` : ''; + return \`
+
\${avatar}
+
+
\${esc(name || 'Desconocido')} \${company}
+
\${esc(c.phone)}
+
\${esc(c.last_message || '')}
+
+
+
\${time}
+ \${unread} +
+
\`; + }).join(''); + } catch (e) { + console.error('loadConvs error:', e); + list.innerHTML = '
Error al cargar conversaciones. Revisa la consola.
'; + } } // ─── Select conversation ───────────────────────────────────────────────────── @@ -1973,9 +1988,21 @@ function closeMobileChat() { // ─── Load messages ─────────────────────────────────────────────────────────── async function loadMessages(phone) { - const r = await fetch('/admin/chat/messages?phone=' + encodeURIComponent(phone)); - const data = await r.json(); const msgsDiv = document.getElementById('chatMessages'); + msgsDiv.innerHTML = '
Cargando...
'; + let data; + try { + const r = await fetch('/admin/chat/messages?phone=' + encodeURIComponent(phone)); + if (!r.ok) { + msgsDiv.innerHTML = \`
Error \${r.status}
\`; + return; + } + data = await r.json(); + } catch (e) { + console.error('loadMessages error:', e); + msgsDiv.innerHTML = '
Error de conexión
'; + return; + } if (!data.messages || data.messages.length === 0) { msgsDiv.innerHTML = '
No hay mensajes
'; return;