Update conversations.php

This commit is contained in:
Lizandro Guarnizo
2026-01-23 23:35:02 -05:00
parent 4e56e44ab5
commit 1f1dee883e
+65 -57
View File
@@ -617,8 +617,8 @@
</h6>
<small id="chat-phone">+1234567890</small>
<div style="font-size:12px; margin-top:4px;">
<button id="attend-btn" class="btn btn-sm btn-outline-success" style="display:none; margin-right:6px;">Atender</button>
<button id="finish-attend-btn" class="btn btn-sm btn-outline-warning" style="display:none; margin-right:6px;">Finalizar atención</button>
<button id="attend-toggle-btn" class="btn btn-sm btn-outline-light" style="display:none; margin-right:6px;">Atender</button>
<small id="attend-status" class="text-white me-3" style="font-size:0.9rem;"></small>
<button id="release-hold-btn" class="btn btn-sm btn-outline-primary" style="display:none">Liberar espera</button>
</div>
</div>
@@ -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 = '<i class="fas fa-user-times"></i> 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 = '<i class="fas fa-user-check"></i> 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 {