@@ -1077,6 +1083,9 @@ const grid = document.getElementById('servicesGrid');
const dbStat = raw.status_operativo;
const statusObj = systemStatuses.find(st => String(st.id) === String(dbStat)) || systemStatuses[0];
const stName = (statusObj?.name || "").toLowerCase();
+ // ✅ NUEVO: los botones de cierre directo solo tienen sentido si el servicio sigue abierto
+ const cierreBar = document.getElementById('panelCierreDirecto');
+ if (cierreBar) cierreBar.classList.toggle('hidden', !!(statusObj && statusObj.is_final) || s.status === 'archived');
if (s.automation_status === 'in_progress') {
document.getElementById('panelEnBolsa').classList.remove('hidden');
@@ -1347,6 +1356,41 @@ const grid = document.getElementById('servicesGrid');
}
}
+ // ✅ NUEVO: Finalizar / Anular directamente desde la ficha, sin asignar gremio ni operario
+ async function cerrarServicioDirecto(tipo) {
+ const id = document.getElementById('detId').value;
+ if (!id) return;
+ const clave = tipo === 'finalizar' ? 'finalizado' : 'anulado';
+ const st = systemStatuses.find(x => (x.name || '').toLowerCase().includes(clave));
+ if (!st) return alert('No encuentro el estado "' + clave + '" en la configuración de estados.');
+ const s = localData.find(x => String(x.id) === String(id));
+ const accion = tipo === 'finalizar' ? 'FINALIZAR' : 'ANULAR';
+ if (!confirm('¿Seguro que quieres ' + accion + ' este servicio? No hace falta gremio ni operario.')) return;
+
+ const headers = { "Content-Type": "application/json", "Authorization": `Bearer ${localStorage.getItem("token")}` };
+ try {
+ // Si estaba en la bolsa buscando operario, la paramos para que no siga ofreciéndolo
+ if (s && (s.automation_status === 'in_progress' || s.automation_status === 'failed')) {
+ await fetch(`${API_URL}/providers/scraped/${id}`, { method: 'PUT', headers, body: JSON.stringify({ automation_status: 'completed' }) });
+ }
+ const res = await fetch(`${API_URL}/providers/scraped/${id}`, {
+ method: 'PUT',
+ headers,
+ body: JSON.stringify({ status_operativo: String(st.id) })
+ });
+ if (res.ok) {
+ closeDetailModal();
+ showToast(tipo === 'finalizar' ? 'Servicio finalizado' : 'Servicio anulado');
+ refreshData();
+ } else {
+ alert("Error en el servidor al guardar.");
+ }
+ } catch (e) {
+ alert("Error al cerrar el servicio: " + e.message);
+ console.error(e);
+ }
+ }
+
async function sendToAutomate() {
const id = document.getElementById('detId').value;
const guild_id = document.getElementById('reGremio').value;