diff --git a/conversations.php b/conversations.php index de6d0a4..066e32a 100644 --- a/conversations.php +++ b/conversations.php @@ -617,8 +617,8 @@ +1234567890
- - + +
@@ -1299,75 +1299,83 @@ } }; - // Attender (marcar 'en servicio' sin liberar) - const attendBtn = document.getElementById('attend-btn'); - const finishBtn = document.getElementById('finish-attend-btn'); - if (attendBtn) { - attendBtn.style.display = (conv.advisor_requested && !conv.in_service) ? 'inline-block' : 'none'; - attendBtn.onclick = async () => { - try { - const resp = await fetch('api/attend.php', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ user_id: userId }) - }); - if (resp.status === 401) { - alert('Sesión expirada. Por favor inicie sesión de nuevo.'); - return; + // 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'); + + const renderAttendControls = () => { + if (!attendToggleBtn || !attendStatus) return; + // refresh conv reference + const c = this.conversations.find(x => x.user_id === userId) || conv; + + if (c.in_service) { + attendToggleBtn.innerHTML = ' Finalizar'; + attendToggleBtn.classList.remove('btn-outline-light'); + attendToggleBtn.classList.add('btn-light','text-dark'); + attendToggleBtn.style.display = 'inline-block'; + attendStatus.textContent = `Atendido por ${c.in_service_by_name || 'un asesor'}`; + } else if (c.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 = ''; + } else { + attendToggleBtn.style.display = 'none'; + attendStatus.textContent = ''; + } + }; + + const toggleAttend = async () => { + try { + const c = this.conversations.find(x => x.user_id === userId) || conv; + if (c.in_service) { + const resp = await this.apiCall('finish_attend.php', { body: { user_id: userId } }); + if (resp && resp.success) { + showAlert('Finalizada la atención', 'success'); + c.in_service = false; + c.advisor_requested = 0; + // hide hold indicator + if (holdIndicator) holdIndicator.style.display = 'none'; + await this.loadConversations(); + await this.loadMessages(userId, true); + } else { + throw new Error(resp && resp.error ? resp.error : 'Error finalizando'); } - const json = await resp.json(); - if (json && json.success) { - conv.in_service = true; - conv.advisor_requested = 0; + } else { + const resp = await this.apiCall('attend.php', { body: { user_id: userId } }); + if (resp && resp.success) { + showAlert('Atención iniciada', 'success'); + c.in_service = true; + c.advisor_requested = 0; if (holdIndicator) { holdIndicator.textContent = 'EN SERVICIO'; holdIndicator.style.color = '#28a745'; holdIndicator.style.display = 'inline'; - } else { - console.warn('holdIndicator missing when marking in_service'); } - attendBtn.style.display = 'none'; - // keep release available - releaseBtn.style.display = 'inline-block'; - // show finish button - if (finishBtn) finishBtn.style.display = 'inline-block'; // hide bot toggle while in service if (btn) btn.style.display = 'none'; await this.loadConversations(); + await this.loadMessages(userId, true); } else { - alert('Error al marcar como atendida'); + throw new Error(resp && resp.error ? resp.error : 'Error iniciando atención'); } - } catch (e) { - console.error('Error attending', e); - alert('Error al atender'); } - }; + } catch (err) { + console.error('Error toggling attend', err); + showAlert('Error al cambiar estado de atención: ' + (err.message || err), 'danger'); + } finally { + // re-render controls after any operation + renderAttendControls(); + } + }; + + if (attendToggleBtn) { + attendToggleBtn.onclick = toggleAttend; } - // Finalizar atención: enviar encuesta y limpiar estado - if (finishBtn) { - finishBtn.style.display = conv.in_service ? 'inline-block' : 'none'; - finishBtn.onclick = async () => { - if (!confirm('¿Finalizar atención y enviar encuesta al usuario?')) return; - try { - const resp = await fetch('api/finish_attend.php', { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ user_id: userId }) - }); - if (resp.status === 401) { - alert('Sesión expirada. Por favor inicie sesión de nuevo.'); - return; - } - const json = await resp.json(); - if (json && json.success) { - conv.in_service = false; - conv.advisor_requested = 0; - holdIndicator.style.display = 'none'; - finishBtn.style.display = 'none'; - releaseBtn.style.display = 'none'; - attendBtn.style.display = 'none'; - // show bot toggle again + // inicializar estado + renderAttendControls(); if (btn) btn.style.display = 'inline-block'; await this.loadConversations(); } else {