Actualizar automatizaciones.html

This commit is contained in:
2026-02-15 14:38:37 +00:00
parent ab94a02a7e
commit 379512c867

View File

@@ -26,19 +26,36 @@
<main class="flex-1 overflow-x-hidden overflow-y-auto bg-gray-50 p-6 relative">
<div id="automationView" class="fade-in max-w-6xl mx-auto">
<div class="flex justify-between items-center mb-8">
<div class="flex justify-between items-center mb-6">
<div>
<h2 class="text-2xl font-black text-slate-800 flex items-center gap-3">
<span class="bg-blue-600 p-2.5 rounded-xl text-white shadow-lg"><i data-lucide="bot"></i></span>
Buzón de Automatizaciones
</h2>
<p class="text-sm text-slate-500 mt-1 font-medium">Pulsa sobre cualquier servicio activo para validar los datos.</p>
<p class="text-sm text-slate-500 mt-1 font-medium">Gestiona y filtra tus expedientes capturados.</p>
</div>
<button onclick="loadInbox()" class="bg-white border-2 border-slate-200 hover:border-blue-600 hover:text-blue-600 text-slate-600 px-6 py-2.5 rounded-xl font-bold flex items-center gap-2 transition-all active:scale-95 shadow-sm">
<i data-lucide="refresh-cw" class="w-4 h-4"></i> Actualizar
</button>
</div>
<div class="bg-white p-4 rounded-2xl border border-slate-200 shadow-sm mb-6 flex flex-wrap gap-4 items-center">
<div class="flex-1 min-w-[200px] relative">
<i data-lucide="search" class="w-4 h-4 absolute left-4 top-1/2 -translate-y-1/2 text-slate-400"></i>
<input type="text" id="searchBox" oninput="renderFilteredInbox()" placeholder="Buscar por nombre, ref, dirección..." class="w-full pl-10 pr-4 py-2.5 bg-slate-50 border border-slate-200 rounded-xl text-sm focus:ring-2 focus:ring-blue-500 outline-none transition-all">
</div>
<div class="flex gap-2">
<select id="filterProvider" onchange="renderFilteredInbox()" class="bg-slate-50 border border-slate-200 text-xs font-bold px-4 py-2.5 rounded-xl outline-none focus:ring-2 focus:ring-blue-500">
<option value="ALL">TODOS LOS PROVEEDORES</option>
<option value="homeserve">HOME (HOMESERVE)</option>
<option value="multiasistencia">MULTI (MULTIASISTENCIA)</option>
</select>
<select id="filterCompany" onchange="renderFilteredInbox()" class="bg-slate-50 border border-slate-200 text-xs font-bold px-4 py-2.5 rounded-xl outline-none focus:ring-2 focus:ring-blue-500">
<option value="ALL">TODAS LAS COMPAÑÍAS</option>
</select>
</div>
</div>
<div id="inboxContainer" class="space-y-4 text-left"></div>
</div>
</main>
@@ -220,67 +237,105 @@
const resSvc = await fetch(`${API_URL}/providers/scraped`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } });
const dataSvc = await resSvc.json();
scrapedData = dataSvc.services || [];
container.innerHTML = "";
if(scrapedData.length === 0) {
container.innerHTML = '<div class="p-12 text-center text-slate-400 bg-white rounded-3xl border-2 border-dashed">No hay expedientes pendientes.</div>';
return;
}
scrapedData.forEach(svc => {
const raw = svc.raw_data || {};
const isArchived = svc.status === 'archived';
const statusLabel = isArchived ? 'ARCHIVADO' : 'SERVICIO ACTIVO';
const statusClass = isArchived ? 'bg-gray-100 text-gray-500 border-gray-200' : 'bg-emerald-50 text-emerald-600 border-emerald-100';
const name = raw['Nombre Cliente'] || raw['CLIENTE'] || "S/N";
const addr = raw['Dirección'] || raw['DOMICILIO'] || "";
const pop = raw['Población'] || raw['POBLACION-PROVINCIA'] || "";
const fullAddr = `${addr} ${pop}`.trim();
const phone = (raw['Teléfono'] || raw['TELEFONOS'] || raw['TELEFONO'] || "").match(/[6789]\d{8}/)?.[0] || "";
// --- OBTENER NOMBRES DE GREMIO Y OPERARIO (SI EXISTEN EN EL BORRADOR) ---
const guildName = allGuilds.find(g => g.id == raw['guild_id'])?.name || null;
const opName = raw['assigned_to_name'] || null;
// Actualizar Filtro de Compañías
const companyFilter = document.getElementById('filterCompany');
const companies = [...new Set(scrapedData.map(s => {
const raw = s.raw_data || {};
return (raw['Compañía'] || raw['COMPAÑIA'] || raw['Procedencia'] || "DESCONOCIDA").toString().toUpperCase().trim();
}))].sort();
const card = document.createElement('div');
card.className = `service-card bg-white p-5 rounded-2xl border ${isArchived ? 'archived' : 'shadow-sm'} flex items-center justify-between transition-all group fade-in text-left`;
card.onclick = (e) => {
if (e.target.closest('a') || e.target.closest('button')) return;
if (isArchived) showToast("⚠️ Este servicio está ARCHIVADO y no se puede editar.", true);
else openEditor(svc.id);
};
card.innerHTML = `
<div class="flex items-center gap-4 min-w-0 text-left">
<div class="w-16 h-16 rounded-2xl flex flex-col items-center justify-center shrink-0 shadow-sm border border-slate-100 ${isArchived ? 'bg-gray-200 text-gray-400' : (svc.provider === 'homeserve' ? 'bg-red-50 text-red-600' : 'bg-blue-50 text-blue-600')}">
<span class="text-[9px] font-black uppercase tracking-tighter">${svc.provider === 'multiasistencia' ? 'MULTI' : 'HOME'}</span>
<i data-lucide="${isArchived ? 'lock' : 'file-text'}" class="w-5 h-5 mt-0.5"></i>
companyFilter.innerHTML = '<option value="ALL">TODAS LAS COMPAÑÍAS</option>';
companies.forEach(c => {
companyFilter.innerHTML += `<option value="${c}">${c}</option>`;
});
renderFilteredInbox(); // Render inicial con filtros aplicados
} catch (e) { showToast("Error de conexión", true); }
}
// FUNCIÓN PARA RENDERIZAR SEGÚN FILTROS
function renderFilteredInbox() {
const container = document.getElementById('inboxContainer');
const search = document.getElementById('searchBox').value.toUpperCase();
const provider = document.getElementById('filterProvider').value;
const company = document.getElementById('filterCompany').value;
const filtered = scrapedData.filter(svc => {
const raw = svc.raw_data || {};
const name = (raw['Nombre Cliente'] || raw['CLIENTE'] || "").toUpperCase();
const ref = (svc.service_ref || "").toUpperCase();
const addr = (raw['Dirección'] || raw['DOMICILIO'] || "").toUpperCase();
const compName = (raw['Compañía'] || raw['COMPAÑIA'] || raw['Procedencia'] || "").toString().toUpperCase().trim();
const matchesSearch = name.includes(search) || ref.includes(search) || addr.includes(search);
const matchesProvider = provider === "ALL" || svc.provider === provider;
const matchesCompany = company === "ALL" || compName === company;
return matchesSearch && matchesProvider && matchesCompany;
});
container.innerHTML = "";
if(filtered.length === 0) {
container.innerHTML = '<div class="p-12 text-center text-slate-400 bg-white rounded-3xl border-2 border-dashed">No se encontraron expedientes con los filtros actuales.</div>';
return;
}
filtered.forEach(svc => {
const raw = svc.raw_data || {};
const isArchived = svc.status === 'archived';
const statusLabel = isArchived ? 'ARCHIVADO' : 'SERVICIO ACTIVO';
const statusClass = isArchived ? 'bg-gray-100 text-gray-500 border-gray-200' : 'bg-emerald-50 text-emerald-600 border-emerald-100';
const name = raw['Nombre Cliente'] || raw['CLIENTE'] || "S/N";
const addr = raw['Dirección'] || raw['DOMICILIO'] || "";
const pop = raw['Población'] || raw['POBLACION-PROVINCIA'] || "";
const fullAddr = `${addr} ${pop}`.trim();
const phone = (raw['Teléfono'] || raw['TELEFONOS'] || raw['TELEFONO'] || "").match(/[6789]\d{8}/)?.[0] || "";
const guildName = allGuilds.find(g => g.id == raw['guild_id'])?.name || null;
const opName = raw['assigned_to_name'] || null;
const card = document.createElement('div');
card.className = `service-card bg-white p-5 rounded-2xl border ${isArchived ? 'archived' : 'shadow-sm'} flex items-center justify-between transition-all group fade-in text-left`;
card.onclick = (e) => {
if (e.target.closest('a') || e.target.closest('button')) return;
if (isArchived) showToast("⚠️ Este servicio está ARCHIVADO y no se puede editar.", true);
else openEditor(svc.id);
};
card.innerHTML = `
<div class="flex items-center gap-4 min-w-0 text-left">
<div class="w-16 h-16 rounded-2xl flex flex-col items-center justify-center shrink-0 shadow-sm border border-slate-100 ${isArchived ? 'bg-gray-200 text-gray-400' : (svc.provider === 'homeserve' ? 'bg-red-50 text-red-600' : 'bg-blue-50 text-blue-600')}">
<span class="text-[9px] font-black uppercase tracking-tighter">${svc.provider === 'multiasistencia' ? 'MULTI' : 'HOME'}</span>
<i data-lucide="${isArchived ? 'lock' : 'file-text'}" class="w-5 h-5 mt-0.5"></i>
</div>
<div class="w-12 h-12 rounded-xl bg-white border border-slate-100 p-2 flex items-center justify-center shrink-0 shadow-inner">
<img src="${getLogoUrl(raw['Compañía'] || raw['COMPAÑIA'])}" onerror="this.src='${companyLogos['DEFAULT']}'" class="max-w-full max-h-full object-contain">
</div>
<div class="min-w-0 text-left">
<div class="flex items-center gap-2 text-left">
<h3 class="font-black text-slate-800 truncate uppercase text-lg leading-tight">${name}</h3>
<span class="text-[8px] font-black px-2 py-0.5 rounded-full border ${statusClass}">${statusLabel}</span>
</div>
<div class="w-12 h-12 rounded-xl bg-white border border-slate-100 p-2 flex items-center justify-center shrink-0 shadow-inner">
<img src="${getLogoUrl(raw['Compañía'] || raw['COMPAÑIA'])}" onerror="this.src='${companyLogos['DEFAULT']}'" class="max-w-full max-h-full object-contain">
</div>
<div class="min-w-0 text-left">
<div class="flex items-center gap-2 text-left">
<h3 class="font-black text-slate-800 truncate uppercase text-lg leading-tight">${name}</h3>
<span class="text-[8px] font-black px-2 py-0.5 rounded-full border ${statusClass}">${statusLabel}</span>
</div>
<p class="text-xs text-slate-400 truncate italic mt-0.5">${fullAddr}</p>
<p class="text-xs text-slate-400 truncate italic mt-0.5">${fullAddr}</p>
<div class="flex flex-wrap gap-2 mt-2">
<span class="text-[10px] bg-slate-100 text-slate-500 px-2 py-0.5 rounded-lg font-bold border">#${svc.service_ref}</span>
${guildName ? `<span class="text-[10px] bg-blue-50 text-blue-600 px-2 py-0.5 rounded-lg font-bold border border-blue-100"><i data-lucide="hammer" class="w-2.5 h-2.5 inline mr-1"></i>${guildName}</span>` : ''}
${opName ? `<span class="text-[10px] bg-purple-50 text-purple-600 px-2 py-0.5 rounded-lg font-bold border border-purple-100"><i data-lucide="user" class="w-2.5 h-2.5 inline mr-1"></i>${opName}</span>` : ''}
</div>
<div class="flex flex-wrap gap-2 mt-2">
<span class="text-[10px] bg-slate-100 text-slate-500 px-2 py-0.5 rounded-lg font-bold border">#${svc.service_ref}</span>
${guildName ? `<span class="text-[10px] bg-blue-50 text-blue-600 px-2 py-0.5 rounded-lg font-bold border border-blue-100"><i data-lucide="hammer" class="w-2.5 h-2.5 inline mr-1"></i>${guildName}</span>` : ''}
${opName ? `<span class="text-[10px] bg-purple-50 text-purple-600 px-2 py-0.5 rounded-lg font-bold border border-purple-100"><i data-lucide="user" class="w-2.5 h-2.5 inline mr-1"></i>${opName}</span>` : ''}
</div>
</div>
<div class="flex items-center gap-2 text-left">
${!isArchived ? `
<div class="hidden group-hover:flex items-center gap-2 pr-4 transition-all">
<a href="https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(fullAddr)}" target="_blank" class="action-btn p-2.5 rounded-xl bg-slate-50 text-slate-400 hover:text-blue-600 shadow-sm"><i data-lucide="map"></i></a>
${phone ? `<a href="https://wa.me/34${phone}" target="_blank" class="action-btn p-2.5 rounded-xl bg-slate-50 text-slate-400 hover:text-emerald-600 shadow-sm"><i data-lucide="message-square"></i></a>` : ''}
</div>` : ''}
</div>`;
container.appendChild(card);
});
lucide.createIcons();
} catch (e) { showToast("Error de conexión", true); }
</div>
<div class="flex items-center gap-2 text-left">
${!isArchived ? `
<div class="hidden group-hover:flex items-center gap-2 pr-4 transition-all">
<a href="https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(fullAddr)}" target="_blank" class="action-btn p-2.5 rounded-xl bg-slate-50 text-slate-400 hover:text-blue-600 shadow-sm"><i data-lucide="map" class="w-5 h-5"></i></a>
${phone ? `<a href="https://wa.me/34${phone}" target="_blank" class="action-btn p-2.5 rounded-xl bg-slate-50 text-slate-400 hover:text-emerald-600 shadow-sm"><i data-lucide="message-square" class="w-5 h-5"></i></a>` : ''}
</div>
<div class="bg-blue-600 text-white p-4 rounded-2xl shadow-lg group-hover:scale-105 transition-all"><i data-lucide="chevron-right" class="w-6 h-6"></i></div>` : ''}
</div>`;
container.appendChild(card);
});
lucide.createIcons();
}
async function openEditor(id) {
@@ -307,7 +362,6 @@
const isUrgent = raw['Urgente'] === 'Sí' || raw['Urgente'] === 'true' || raw['URGENTE'] === 'SI';
document.getElementById('impUrgent').value = isUrgent.toString();
// --- RECUPERACIÓN DE DATOS GUARDADOS PREVIAMENTE ---
document.getElementById('impNotesInt').value = raw['internal_notes'] || "";
document.getElementById('impNotesExt').value = raw['client_notes'] || "";
@@ -315,7 +369,6 @@
document.getElementById('impGuild').value = savedGuild;
if(savedGuild) {
// Cargamos operarios y luego seleccionamos el guardado
await loadOpsForGuild(savedGuild);
document.getElementById('impOperator').value = raw['assigned_to'] || "";
} else {
@@ -333,7 +386,6 @@
const btn = e.currentTarget;
btn.disabled = true;
// --- OBTENER NOMBRE DEL OPERARIO SELECCIONADO PARA MOSTRAR EN LA TARJETA ---
const opSelect = document.getElementById('impOperator');
const opName = opSelect.options[opSelect.selectedIndex]?.text.includes('--') ? null : opSelect.options[opSelect.selectedIndex]?.text;
@@ -344,7 +396,6 @@
cp: document.getElementById('impCP').value,
description: document.getElementById('impDesc').value,
is_urgent: document.getElementById('impUrgent').value === 'true',
// --- CAMPOS CORREGIDOS PARA EL GUARDADO ---
guild_id: document.getElementById('impGuild').value,
assigned_to: document.getElementById('impOperator').value,
assigned_to_name: opName,