diff --git a/servicios.html b/servicios.html
index c1d5909..36f75ea 100644
--- a/servicios.html
+++ b/servicios.html
@@ -370,46 +370,52 @@
if (html.includes(`value="${currentValue}"`)) opSelect.value = currentValue;
}
- // ==========================================
- // 🚀 LÓGICA INTELIGENTE DE ENRUTAMIENTO (ESTADOS)
+ // ==========================================
+ // 🚀 LÓGICA INTELIGENTE DE ENRUTAMIENTO (ESTADOS) - VERSIÓN OPCIÓN 2
// ==========================================
function getServiceStateInfo(s) {
const raw = s.raw_data || {};
- const dbStat = raw.status_operativo;
+ const dbStat = raw.status_operativo; // AquĂ viene el ID del estado (ej: "42") o texto antiguo
- // 1. Bolsa de Trabajo (Máxima prioridad visual)
+ // 1. Prioridad Máxima: Bolsa de Trabajo / Robot WhatsApp
if (!s.assigned_name && (s.automation_status === 'in_progress' || s.automation_status === 'failed')) {
return { id: 'bolsa', name: s.automation_status === 'in_progress' ? 'Buscando Operario' : 'Fallo en Bolsa', color: s.automation_status === 'in_progress' ? 'amber' : 'red', isBlocked: true, is_final: false };
}
- // 2. Si viene limpio del scraper sin estado -> Pendiente de Asignar
+ // 2. Match Directo por ID (La forma más segura para que los filtros de los botones no fallen)
+ // Si dbStat es un ID que existe en nuestra lista de estados, lo usamos directamente.
+ const foundById = systemStatuses.find(st => String(st.id) === String(dbStat));
+ if (foundById) return { ...foundById, isBlocked: false };
+
+ // 3. LĂłgica de "La pelota en el tejado": Si tiene operario pero NO tiene fecha agendada
+ if (s.assigned_name && (!raw.scheduled_date || raw.scheduled_date === "")) {
+ const asignado = systemStatuses.find(st => st.name.toLowerCase() === 'asignado');
+ const esperando = systemStatuses.find(st => st.name.toLowerCase().includes('esperando'));
+
+ // Si en la base de datos el texto coincide con "esperando" (compatibilidad)
+ if (dbStat === 'esperando_cliente' && esperando) return { ...esperando, isBlocked: false };
+
+ // Por defecto, si tiene tĂ©cnico asignado pero no hay fecha ni estado especĂfico de "esperando",
+ // lo mandamos a la columna "ASIGNADO" (Oficina debe gestionar).
+ if (asignado) return { ...asignado, isBlocked: false };
+ }
+
+ // 4. Si viene nuevo del scraper (Sin operario y sin estado)
if (!s.assigned_name || dbStat === 'sin_asignar') {
const found = systemStatuses.find(st => st.name.toLowerCase().includes('pendiente de asignar')) || systemStatuses[0];
return { ...found, isBlocked: false };
}
- // 3. Match directo por ID Numérico (El nuevo estándar)
- const foundObj = systemStatuses.find(st => String(st.id) === String(dbStat));
- if (foundObj) return { ...foundObj, isBlocked: false };
+ // 5. Fallbacks histĂłricos de texto (Para que los servicios antiguos no se rompan)
+ const stLower = String(dbStat).toLowerCase();
+ if (stLower === 'citado') return { ...systemStatuses.find(st => st.name.toLowerCase().includes('citado')), isBlocked: false };
+ if (stLower === 'de_camino') return { ...systemStatuses.find(st => st.name.toLowerCase().includes('camino')), isBlocked: false };
+ if (stLower === 'trabajando') return { ...systemStatuses.find(st => st.name.toLowerCase().includes('trabajando')), isBlocked: false };
+ if (stLower === 'incidencia') return { ...systemStatuses.find(st => st.name.toLowerCase().includes('incidencia')), isBlocked: false };
+ if (stLower === 'desasignado') return { ...systemStatuses.find(st => st.name.toLowerCase().includes('desasignado')), isBlocked: false };
+ if (stLower === 'terminado' || stLower === 'finalizado') return { ...systemStatuses.find(st => st.is_final), isBlocked: false };
- // 4. Fallbacks histĂłricos de texto (Para que los servicios viejos no se rompan)
- if (dbStat === 'asignado_operario') return { ...systemStatuses.find(st => st.name.toLowerCase() === 'asignado'), isBlocked: false };
- if (dbStat === 'citado') return { ...systemStatuses.find(st => st.name.toLowerCase().includes('citado')), isBlocked: false };
- if (dbStat === 'de_camino') return { ...systemStatuses.find(st => st.name.toLowerCase().includes('camino')), isBlocked: false };
- if (dbStat === 'trabajando') return { ...systemStatuses.find(st => st.name.toLowerCase().includes('trabajando')), isBlocked: false };
- if (dbStat === 'incidencia') return { ...systemStatuses.find(st => st.name.toLowerCase().includes('incidencia')), isBlocked: false };
- if (dbStat === 'terminado') return { ...systemStatuses.find(st => st.name.toLowerCase().includes('terminado') || st.name.toLowerCase().includes('finalizado')), isBlocked: false };
-
- // 5. Fallback final para asignados: Si tiene operario pero NO status_operativo
- if (s.assigned_name && (!raw.scheduled_date || raw.scheduled_date === "")) {
- const f = systemStatuses.find(st => st.name.toLowerCase() === 'asignado') || systemStatuses[1];
- return { ...f, isBlocked: false };
- }
- if (s.assigned_name && raw.scheduled_date) {
- const f = systemStatuses.find(st => st.name.toLowerCase().includes('citado')) || systemStatuses[3];
- return { ...f, isBlocked: false };
- }
-
+ // Fallback de seguridad
return { id: 'unknown', name: 'Desconocido', color: 'gray', isBlocked: false, is_final: false };
}