Update conversations.php
This commit is contained in:
+65
-57
@@ -617,8 +617,8 @@
|
|||||||
</h6>
|
</h6>
|
||||||
<small id="chat-phone">+1234567890</small>
|
<small id="chat-phone">+1234567890</small>
|
||||||
<div style="font-size:12px; margin-top:4px;">
|
<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="attend-toggle-btn" class="btn btn-sm btn-outline-light" 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>
|
<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>
|
<button id="release-hold-btn" class="btn btn-sm btn-outline-primary" style="display:none">Liberar espera</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -1299,75 +1299,83 @@
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Attender (marcar 'en servicio' sin liberar)
|
// Render attend controls (single toggle + status) — compatible con comportamiento de chat_window.php
|
||||||
const attendBtn = document.getElementById('attend-btn');
|
const attendToggleBtn = document.getElementById('attend-toggle-btn');
|
||||||
const finishBtn = document.getElementById('finish-attend-btn');
|
const attendStatus = document.getElementById('attend-status');
|
||||||
if (attendBtn) {
|
|
||||||
attendBtn.style.display = (conv.advisor_requested && !conv.in_service) ? 'inline-block' : 'none';
|
const renderAttendControls = () => {
|
||||||
attendBtn.onclick = async () => {
|
if (!attendToggleBtn || !attendStatus) return;
|
||||||
try {
|
// refresh conv reference
|
||||||
const resp = await fetch('api/attend.php', {
|
const c = this.conversations.find(x => x.user_id === userId) || conv;
|
||||||
method: 'POST',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
if (c.in_service) {
|
||||||
body: JSON.stringify({ user_id: userId })
|
attendToggleBtn.innerHTML = '<i class="fas fa-user-times"></i> Finalizar';
|
||||||
});
|
attendToggleBtn.classList.remove('btn-outline-light');
|
||||||
if (resp.status === 401) {
|
attendToggleBtn.classList.add('btn-light','text-dark');
|
||||||
alert('Sesión expirada. Por favor inicie sesión de nuevo.');
|
attendToggleBtn.style.display = 'inline-block';
|
||||||
return;
|
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();
|
} else {
|
||||||
if (json && json.success) {
|
const resp = await this.apiCall('attend.php', { body: { user_id: userId } });
|
||||||
conv.in_service = true;
|
if (resp && resp.success) {
|
||||||
conv.advisor_requested = 0;
|
showAlert('Atención iniciada', 'success');
|
||||||
|
c.in_service = true;
|
||||||
|
c.advisor_requested = 0;
|
||||||
if (holdIndicator) {
|
if (holdIndicator) {
|
||||||
holdIndicator.textContent = 'EN SERVICIO';
|
holdIndicator.textContent = 'EN SERVICIO';
|
||||||
holdIndicator.style.color = '#28a745';
|
holdIndicator.style.color = '#28a745';
|
||||||
holdIndicator.style.display = 'inline';
|
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
|
// hide bot toggle while in service
|
||||||
if (btn) btn.style.display = 'none';
|
if (btn) btn.style.display = 'none';
|
||||||
await this.loadConversations();
|
await this.loadConversations();
|
||||||
|
await this.loadMessages(userId, true);
|
||||||
} else {
|
} 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
|
// inicializar estado
|
||||||
if (finishBtn) {
|
renderAttendControls();
|
||||||
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
|
|
||||||
if (btn) btn.style.display = 'inline-block';
|
if (btn) btn.style.display = 'inline-block';
|
||||||
await this.loadConversations();
|
await this.loadConversations();
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user