Actualizar automatizaciones.html

This commit is contained in:
2026-02-14 22:53:52 +00:00
parent 282d1e2a77
commit ec3b3a0461

View File

@@ -64,7 +64,7 @@
</div>
<div class="flex items-center gap-4 text-left">
<div class="flex flex-col items-end mr-4">
<div class="flex flex-col items-end mr-4 text-left">
<span class="text-[9px] font-black text-slate-400 uppercase mb-1">Prioridad</span>
<select id="impUrgent" class="bg-slate-50 border border-slate-200 text-xs font-black px-3 py-1.5 rounded-xl outline-none focus:border-red-400 cursor-pointer">
<option value="false">BAJA (Normal)</option>
@@ -98,7 +98,7 @@
</div>
<div class="grid grid-cols-1 md:grid-cols-5 gap-3 text-left">
<div class="md:col-span-4 space-y-1">
<div class="md:col-span-4 space-y-1 text-left">
<label class="text-[9px] font-black text-blue-600 uppercase tracking-widest ml-2 flex items-center gap-1">
<i data-lucide="map-pin" class="w-3 h-3"></i> Dirección
</label>
@@ -142,7 +142,7 @@
</div>
</div>
<div class="flex items-center gap-3 pt-4 border-t">
<div class="flex items-center gap-3 pt-4 border-t text-left">
<button type="button" onclick="saveDraftChanges(event)" class="bg-slate-100 hover:bg-slate-200 text-slate-600 font-black px-6 py-5 rounded-[1.8rem] transition-all flex items-center justify-center gap-2 active:scale-95 text-xs uppercase tracking-widest">
<i data-lucide="save" class="w-4 h-4"></i> <span>Guardar Borrador</span>
</button>
@@ -221,35 +221,27 @@
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 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 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);
}
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')}">
@@ -284,79 +276,87 @@
} catch (e) { showToast("Error de conexión", true); }
}
function openEditor(id) {
const svc = scrapedData.find(s => s.id === id);
if(!svc) return;
const raw = svc.raw_data;
const companyName = raw['Compañía'] || raw['COMPAÑIA'] || raw['Procedencia'] || "";
document.getElementById('modalCompanyLogo').innerHTML = `<img src="${getLogoUrl(companyName)}" class="max-w-full max-h-full object-contain">`;
document.getElementById('displayRef').innerText = `REF: ${svc.service_ref}`;
document.getElementById('displayCompany').innerText = companyName;
document.getElementById('modalStatusBadge').innerHTML = `<span class="text-[9px] bg-emerald-50 text-emerald-600 px-3 py-1 rounded-full font-black border border-emerald-100">SERVICIO ACTIVO</span>`;
async function openEditor(id) {
const svc = scrapedData.find(s => s.id === id);
if(!svc) return;
const raw = svc.raw_data;
const companyName = raw['Compañía'] || raw['COMPAÑIA'] || raw['Procedencia'] || "";
document.getElementById('modalCompanyLogo').innerHTML = `<img src="${getLogoUrl(companyName)}" class="max-w-full max-h-full object-contain">`;
document.getElementById('displayRef').innerText = `REF: ${svc.service_ref}`;
document.getElementById('displayCompany').innerText = companyName;
document.getElementById('modalStatusBadge').innerHTML = `<span class="text-[9px] bg-emerald-50 text-emerald-600 px-3 py-1 rounded-full font-black border border-emerald-100">SERVICIO ACTIVO</span>`;
document.getElementById('impScrapedId').value = id;
document.getElementById('impName').value = raw['Nombre Cliente'] || raw['CLIENTE'] || "S/N";
const rawPhone = raw['Teléfono'] || raw['TELEFONOS'] || raw['TELEFONO'] || "";
document.getElementById('impPhone').value = rawPhone.match(/[6789]\d{8}/)?.[0] || "";
const addr = raw['Dirección'] || raw['DOMICILIO'] || "";
const pop = raw['Población'] || raw['POBLACION-PROVINCIA'] || "";
document.getElementById('impAddress').value = `${addr} ${pop}`.trim();
document.getElementById('impCP').value = raw['Código Postal'] || raw['C.P.'] || "";
document.getElementById('impDesc').value = raw['Descripción'] || "";
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'] || "";
// Cargar Gremio y disparar carga de operarios si ya existía
const savedGuild = raw['guild_id'] || "";
document.getElementById('impGuild').value = savedGuild;
if(savedGuild) {
loadOpsForGuild(savedGuild).then(() => {
document.getElementById('impOperator').value = raw['assigned_to'] || "";
});
} else {
document.getElementById('impOperator').innerHTML = '<option value="">-- Automática --</option>';
}
document.getElementById('impScrapedId').value = id;
document.getElementById('impName').value = raw['Nombre Cliente'] || raw['CLIENTE'] || "S/N";
const rawPhone = raw['Teléfono'] || raw['TELEFONOS'] || raw['TELEFONO'] || "";
document.getElementById('impPhone').value = rawPhone.match(/[6789]\d{8}/)?.[0] || "";
const addr = raw['Dirección'] || raw['DOMICILIO'] || "";
const pop = raw['Población'] || raw['POBLACION-PROVINCIA'] || "";
document.getElementById('impAddress').value = `${addr} ${pop}`.trim();
document.getElementById('impCP').value = raw['Código Postal'] || raw['C.P.'] || "";
document.getElementById('impDesc').value = raw['Descripción'] || "";
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'] || "";
const savedGuild = raw['guild_id'] || "";
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 {
document.getElementById('impOperator').innerHTML = '<option value="">-- Automática --</option>';
}
document.getElementById('importModal').classList.remove('hidden');
lucide.createIcons();
}
document.getElementById('importModal').classList.remove('hidden');
lucide.createIcons();
}
function closeModal() { document.getElementById('importModal').classList.add('hidden'); }
async function saveDraftChanges(e) {
const id = document.getElementById('impScrapedId').value;
const btn = e.currentTarget;
btn.disabled = true;
const payload = {
name: document.getElementById('impName').value,
phone: document.getElementById('impPhone').value,
address: document.getElementById('impAddress').value,
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 || null,
assigned_to: document.getElementById('impOperator').value,
internal_notes: document.getElementById('impNotesInt').value,
client_notes: document.getElementById('impNotesExt').value,
is_urgent: document.getElementById('impUrgent').value === 'true'
client_notes: document.getElementById('impNotesExt').value
};
try {
const res = await fetch(`${API_URL}/providers/scraped/${id}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${localStorage.getItem("token")}` },
body: JSON.stringify(payload)
});
if(res.ok) { showToast("✅ Borrador guardado"); loadInbox(); }
else { showToast("❌ Error al guardar", true); }
} catch (e) { showToast("❌ Error de red", true); }
finally { btn.disabled = false; }
if(res.ok) {
showToast("✅ Borrador guardado correctamente");
loadInbox();
} else {
showToast("❌ Error al guardar", true);
}
} catch (e) {
showToast("❌ Error de red", true);
} finally {
btn.disabled = false;
}
}
async function handleFinalImport(event) {