diff --git a/proveedores.html b/proveedores.html
index 1fd1c19..415b092 100644
--- a/proveedores.html
+++ b/proveedores.html
@@ -48,9 +48,14 @@
Filtra por proveedor, compañía o cualquier dato del asegurado.
-
+
+
+
+
@@ -209,6 +214,7 @@
let scrapedData = [];
let systemStatuses = []; // Guardará los estados reales de la DB
let pollingInterval = null;
+ let isSmartSortActive = false; // Variable para el botón Orden Inteligente
const companyLogos = {
'REPSOL': 'https://cdn.sanity.io/images/rn4tswnp/production/1bc5be0207b732bd18dd0fc38e063d5701267068-1000x832.png?rect=6,0,994,832&h=320&auto=format&dpr=2',
@@ -253,7 +259,55 @@
if (modal.classList.contains('hidden')) {
await loadInbox();
}
- }, 20000);
+ }, 30000);
+ }
+
+ function toggleSmartSort() {
+ isSmartSortActive = !isSmartSortActive;
+ const btn = document.getElementById('btnSmartSort');
+ if (isSmartSortActive) {
+ btn.classList.remove('bg-indigo-50', 'text-indigo-700', 'border-indigo-100', 'hover:border-indigo-400');
+ btn.classList.add('bg-indigo-600', 'text-white', 'border-indigo-600', 'shadow-indigo-200', 'hover:bg-indigo-700');
+ } else {
+ btn.classList.remove('bg-indigo-600', 'text-white', 'border-indigo-600', 'shadow-indigo-200', 'hover:bg-indigo-700');
+ btn.classList.add('bg-indigo-50', 'text-indigo-700', 'border-indigo-100', 'hover:border-indigo-400');
+ }
+ renderFilteredInbox();
+ }
+
+ // Calcular la prioridad del servicio
+ function getServicePriority(svc) {
+ const raw = svc.raw_data || {};
+ const autoStatus = (svc.automation_status || '').toLowerCase();
+ const sysStatus = (svc.status || '').toLowerCase();
+ const dbStatusId = raw.status_operativo;
+ const dbStatusObj = systemStatuses.find(st => String(st.id) === String(dbStatusId));
+ const opName = raw['assigned_to_name'] || null;
+
+ // Fallo bolsa: 2
+ if (autoStatus === 'failed') return 2;
+
+ // Bolsa activa: 5 (Resto)
+ if (autoStatus.includes('bolsa') || autoStatus === 'in_progress') return 5;
+
+ if (dbStatusObj) {
+ const stName = dbStatusObj.name.toLowerCase();
+ // Sin asignar de DB: 1
+ if (stName.includes('pendiente') && !stName.includes('cita')) return 1;
+ // Esperando al cliente: 3
+ if (stName.includes('esperando')) return 3;
+ // Citado: 4
+ if (stName.includes('citado')) return 4;
+ // Resto de estados: 5
+ return 5;
+ }
+
+ // Asignado o Importado: 5 (Resto)
+ if (raw['assigned_to'] || (sysStatus === 'imported' && opName)) return 5;
+ if (sysStatus === 'imported') return 5;
+
+ // Por defecto (Sin asignar): 1
+ return 1;
}
async function loadStatuses() {
@@ -526,9 +580,22 @@
}
// SEPARAR ACTIVOS Y ARCHIVADOS
- const activeServices = filtered.filter(svc => svc.status !== 'archived');
+ let activeServices = filtered.filter(svc => svc.status !== 'archived');
const archivedServices = filtered.filter(svc => svc.status === 'archived');
+ // APLICAR ORDEN INTELIGENTE SI ESTÁ ACTIVO
+ if (isSmartSortActive) {
+ activeServices.sort((a, b) => {
+ const priorityA = getServicePriority(a);
+ const priorityB = getServicePriority(b);
+ if (priorityA !== priorityB) {
+ return priorityA - priorityB;
+ }
+ // Mantener el orden original si tienen la misma prioridad
+ return 0;
+ });
+ }
+
// 1. RENDERIZAR ACTIVOS
activeServices.forEach(svc => container.appendChild(buildServiceCard(svc)));