diff --git a/conversations.php b/conversations.php index 0ee1bb0..eac7844 100644 --- a/conversations.php +++ b/conversations.php @@ -1300,6 +1300,31 @@ } } + // Mostrar mensaje de estado (persistente) en la conversación activa + showStatusMessageInChat(userId, text) { + try { + if (!userId || !text) return; + if (String(this.currentUserId) !== String(userId)) return; + const container = document.getElementById('chat-conversations'); + if (!container) return; + + // eliminar status previos para evitar duplicados + Array.from(container.querySelectorAll('.message-template.system.system-status')).forEach(el => el.parentNode && el.parentNode.removeChild(el)); + + const el = document.createElement('div'); + el.className = 'message incoming'; + el.dataset.status = 'service'; + + const bubble = document.createElement('div'); + bubble.className = 'message-bubble message-template system system-status'; + bubble.innerHTML = `
👩‍⚕️
${escapeHtml(String(text))}
`; + + el.appendChild(bubble); + container.appendChild(el); + this.scrollToBottom(); + } catch (e) { console.warn('showStatusMessageInChat failed', e); } + } + setupEventListeners() { // Búsqueda de conversaciones document.getElementById('search-input').addEventListener('input', (e) => { @@ -2036,8 +2061,8 @@ } if (releaseBtn) { - // show button when on_hold or advisor_requested - releaseBtn.style.display = (conv.on_hold || conv.advisor_requested) ? 'inline-block' : 'none'; + // show button only when conversation is explicitly 'on_hold' (do NOT show during advisor request) + releaseBtn.style.display = (conv.on_hold) ? 'inline-block' : 'none'; releaseBtn.onclick = async () => { try { const resp = await fetch('api/release_hold.php', { @@ -2071,6 +2096,22 @@ } }; + // Ensure 'Atender' button appears when there's an advisor request (even if other flows didn't initialize it yet) + (function(){ + try { + const attendToggleBtn = document.getElementById('attend-toggle-btn'); + const attendStatus = document.getElementById('attend-status'); + if (attendToggleBtn && conv && conv.advisor_requested && !conv.in_service) { + attendToggleBtn.innerHTML = ' Atender'; + attendToggleBtn.classList.remove('btn-light','text-dark'); + attendToggleBtn.classList.add('btn-outline-light'); + attendToggleBtn.style.display = 'inline-block'; + if (attendStatus) attendStatus.textContent = ''; + try { this.showStatusMessageInChat(userId, 'Solicitud de asesor pendiente'); } catch(e) { /* ignore */ } + } + } catch (e) { console.warn('ensure attend button visibility failed', e); } + }).call(this); + // Render attend controls (single toggle + status) — compatible con comportamiento de chat_window.php const attendToggleBtn = document.getElementById('attend-toggle-btn'); const attendStatus = document.getElementById('attend-status'); @@ -2090,12 +2131,16 @@ attendToggleBtn.classList.add('btn-light','text-dark'); attendToggleBtn.style.display = 'inline-block'; attendStatus.textContent = `Atendido por ${c.in_service_by_name || 'un asesor'}`; + // show status message in chat + try { this.showStatusMessageInChat(userId, 'Un asesor te está atendiendo'); } catch(e){/* ignore */} } else if (s.advisor_requested) { attendToggleBtn.innerHTML = ' Atender'; attendToggleBtn.classList.remove('btn-light','text-dark'); attendToggleBtn.classList.add('btn-outline-light'); attendToggleBtn.style.display = 'inline-block'; attendStatus.textContent = ''; + // show a subtle status that a request is pending + try { this.showStatusMessageInChat(userId, 'Solicitud de asesor pendiente'); } catch(e){/* ignore */} } else { attendToggleBtn.style.display = 'none'; attendStatus.textContent = ''; @@ -2139,6 +2184,11 @@ // hide hold indicator if (holdIndicator) holdIndicator.style.display = 'none'; + // Try to ensure hold is released on server as finalizing should free the hold + try { + await this.apiCall('release_hold.php', { body: { user_id: userId } }); + } catch (e) { console.warn('release_hold after finish failed', e); } + // actualizar cache local this._userStateCache = this._userStateCache || {}; this._userStateCache[userId] = { state: { in_service: false, advisor_requested: false, on_hold: false }, ts: Date.now() }; @@ -2163,6 +2213,9 @@ // hide bot toggle while in service if (btn) btn.style.display = 'none'; + // Add status message in chat to inform the user + try { this.showStatusMessageInChat(userId, 'Un asesor te está atendiendo'); } catch(e) { console.warn('showStatusMessageInChat failed', e); } + // actualizar cache local this._userStateCache = this._userStateCache || {}; this._userStateCache[userId] = { state: { in_service: true, advisor_requested: false, on_hold: false }, ts: Date.now() };