Actualizar automatizaciones.html

This commit is contained in:
2026-02-13 21:56:30 +00:00
parent 2519e2671a
commit 9fb6084cbf

View File

@@ -29,7 +29,7 @@
<h2 class="text-2xl font-bold text-gray-800 flex items-center gap-2">
<i data-lucide="bot" class="text-blue-600"></i> Buzón de Automatizaciones
</h2>
<p class="text-sm text-gray-500 mt-1">Valida e importa servicios de Multiasistencia y HomeServe.</p>
<p class="text-sm text-gray-500 mt-1">Valida e importa servicios detectados por los robots.</p>
</div>
<button onclick="loadInbox()" class="bg-blue-600 hover:bg-blue-700 text-white px-5 py-2.5 rounded-xl shadow-lg flex items-center gap-2 font-medium transition-all transform hover:scale-105">
<i data-lucide="refresh-cw" class="w-5 h-5"></i> Actualizar Buzón
@@ -37,63 +37,63 @@
</div>
<div class="space-y-4" id="inboxContainer">
<div class="p-12 text-center text-gray-400 bg-white rounded-xl border border-gray-100 shadow-sm">
Cargando expedientes de los robots...
</div>
</div>
</div>
</main>
</div>
</div>
<div id="importModal" class="fixed inset-0 bg-black bg-opacity-50 hidden z-50 flex items-center justify-center backdrop-blur-sm p-4">
<div class="bg-white rounded-xl shadow-2xl w-full max-w-2xl p-6 animate-slide-in flex flex-col max-h-[90vh] overflow-y-auto no-scrollbar">
<div class="bg-white rounded-xl shadow-2xl w-full max-w-2xl p-6 flex flex-col max-h-[90vh] overflow-y-auto no-scrollbar">
<div class="flex justify-between items-center mb-6 border-b pb-4">
<h3 class="text-xl font-bold text-gray-800 flex items-center gap-2">
<i data-lucide="file-check" class="text-green-600"></i> Validar Datos para Importar
<i data-lucide="file-check" class="text-green-600"></i> Validar Datos de Importación
</h3>
<button onclick="closeModal()" class="text-gray-400 hover:text-gray-600"><i data-lucide="x" class="w-6 h-6"></i></button>
</div>
<form id="importForm" onsubmit="handleImportSubmit(event)" class="space-y-6">
<form id="importForm" onsubmit="handleImportSubmit(event)" class="space-y-5">
<input type="hidden" id="scrapedId">
<div class="bg-gray-50 p-4 rounded-lg border border-gray-200 grid grid-cols-1 md:grid-cols-2 gap-4">
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>
<label class="block text-xs font-bold text-gray-500 uppercase mb-1 tracking-wider">Nombre del Cliente</label>
<input type="text" id="imp_name" required class="w-full border p-2.5 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 outline-none font-medium">
<label class="block text-[10px] font-bold text-gray-400 uppercase mb-1">Nombre del Cliente</label>
<input type="text" id="imp_name" required class="w-full border p-2.5 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 outline-none">
</div>
<div>
<label class="block text-xs font-bold text-gray-500 uppercase mb-1 tracking-wider">Teléfono</label>
<input type="text" id="imp_phone" required class="w-full border p-2.5 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 outline-none font-medium">
<label class="block text-[10px] font-bold text-gray-400 uppercase mb-1">Teléfono Principal (Limpio)</label>
<input type="text" id="imp_phone" required class="w-full border p-2.5 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 outline-none font-mono">
<div id="imp_phone_extra" class="text-[10px] text-gray-400 mt-1 italic"></div>
</div>
</div>
<div class="grid grid-cols-1 md:grid-cols-4 gap-4">
<div class="md:col-span-3">
<label class="block text-[10px] font-bold text-gray-400 uppercase mb-1">Dirección y Población</label>
<input type="text" id="imp_address" required class="w-full border p-2.5 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 outline-none">
</div>
<div>
<label class="block text-[10px] font-bold text-gray-400 uppercase mb-1">C. Postal</label>
<input type="text" id="imp_cp" required class="w-full border p-2.5 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 outline-none font-mono">
</div>
</div>
<div>
<label class="block text-xs font-bold text-gray-500 uppercase mb-1 tracking-wider">Dirección de la Reparación</label>
<div class="relative">
<i data-lucide="map-pin" class="absolute left-3 top-3 w-4 h-4 text-gray-400"></i>
<input type="text" id="imp_address" required class="w-full pl-10 pr-3 py-2.5 border rounded-lg focus:ring-2 focus:ring-blue-500 outline-none text-sm">
</div>
<label class="block text-[10px] font-bold text-gray-400 uppercase mb-1">Descripción del Trabajo</label>
<textarea id="imp_desc" rows="4" class="w-full border p-3 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 outline-none"></textarea>
</div>
<div class="grid grid-cols-1 md:grid-cols-3 gap-4">
<div>
<label class="block text-xs font-bold text-gray-500 uppercase mb-1 tracking-wider">Descripción de Daños (Detectado)</label>
<textarea id="imp_desc" rows="5" class="w-full border p-3 rounded-lg text-sm focus:ring-2 focus:ring-blue-500 outline-none bg-yellow-50/30 border-yellow-100" placeholder="Escribe aquí los detalles..."></textarea>
</div>
<div class="grid grid-cols-2 md:grid-cols-3 gap-4">
<div>
<label class="block text-xs font-bold text-gray-500 uppercase mb-1 tracking-wider">Referencia</label>
<input type="text" id="imp_ref" class="w-full border p-2.5 rounded-lg text-sm bg-gray-100 font-mono text-gray-500" readonly>
<label class="block text-[10px] font-bold text-gray-400 uppercase mb-1">Referencia</label>
<input type="text" id="imp_ref" class="w-full border p-2.5 rounded-lg text-sm bg-gray-50" readonly>
</div>
<div>
<label class="block text-xs font-bold text-gray-500 uppercase mb-1 tracking-wider">Compañía</label>
<input type="text" id="imp_company" class="w-full border p-2.5 rounded-lg text-sm bg-gray-100 text-gray-500" readonly>
<label class="block text-[10px] font-bold text-gray-400 uppercase mb-1">Compañía / Origen</label>
<input type="text" id="imp_company" class="w-full border p-2.5 rounded-lg text-sm bg-gray-50 font-bold text-blue-600" readonly>
</div>
<div class="col-span-2 md:col-span-1">
<label class="block text-xs font-bold text-gray-500 uppercase mb-1 tracking-wider">Prioridad</label>
<div>
<label class="block text-[10px] font-bold text-gray-400 uppercase mb-1">Prioridad</label>
<select id="imp_urgent" class="w-full border p-2.5 rounded-lg text-sm bg-white outline-none">
<option value="false">Normal</option>
<option value="true">🚨 URGENTE</option>
@@ -101,9 +101,10 @@
</div>
</div>
<div class="pt-4">
<button type="submit" id="btnImport" class="w-full bg-blue-600 hover:bg-blue-700 text-white font-bold py-4 rounded-xl shadow-lg shadow-blue-500/30 text-lg transition-all flex justify-center items-center gap-3 active:scale-95">
<i data-lucide="download-cloud" class="w-6 h-6"></i> IMPORTAR AL CRM
<div class="pt-4 flex gap-3">
<button type="button" onclick="closeModal()" class="flex-1 bg-gray-100 text-gray-600 py-3 rounded-xl font-bold hover:bg-gray-200 transition-all">Cancelar</button>
<button type="submit" id="btnImport" class="flex-[2] bg-blue-600 hover:bg-blue-700 text-white font-bold py-3 rounded-xl shadow-lg transition-all flex justify-center items-center gap-2">
<i data-lucide="download-cloud"></i> IMPORTAR AL CRM
</button>
</div>
</form>
@@ -135,7 +136,7 @@
scrapedData = dataSvc.services || [];
if(scrapedData.length === 0) {
container.innerHTML = '<div class="p-12 text-center text-gray-400 bg-white rounded-xl border border-dashed border-gray-200">No hay servicios pendientes en los robots.</div>';
container.innerHTML = '<div class="p-12 text-center text-gray-400 bg-white rounded-xl border border-dashed border-gray-200 shadow-sm">No hay expedientes pendientes.</div>';
return;
}
@@ -144,30 +145,33 @@
const raw = svc.raw_data || {};
const myMap = mappings.filter(m => m.provider === svc.provider);
// Mapeo dinámico para el resumen de la tarjeta
const name = raw[myMap.find(m => m.target_key === 'clientName')?.original_key] || "Desconocido";
const addr = raw[myMap.find(m => m.target_key === 'address')?.original_key] || "Sin dirección";
const phone = raw[myMap.find(m => m.target_key === 'phone')?.original_key] || raw['Teléfono'] || "S/T";
const addr = raw[myMap.find(m => m.target_key === 'address')?.original_key] || "";
const pop = raw[myMap.find(m => m.target_key === 'poblacion')?.original_key] || "";
const fullAddr = `${addr} ${pop}`.trim() || "Sin dirección";
const rawPhone = raw[myMap.find(m => m.target_key === 'phone')?.original_key] || raw['Teléfono'] || "";
const cleanPhone = rawPhone.match(/[6789]\d{8}/) ? rawPhone.match(/[6789]\d{8}/)[0] : "S/T";
const card = document.createElement('div');
card.className = "bg-white p-5 rounded-xl border border-gray-100 shadow-sm flex items-center justify-between hover:border-blue-200 transition-all fade-in";
card.innerHTML = `
<div class="flex items-center gap-6 min-w-0">
<div class="w-16 h-16 rounded-xl flex flex-col items-center justify-center shrink-0 ${svc.provider === 'homeserve' ? 'bg-red-50 text-red-600' : 'bg-blue-50 text-blue-600'}">
<span class="text-[10px] font-black uppercase tracking-tighter">${svc.provider}</span>
<span class="text-[9px] font-black uppercase tracking-tighter">${svc.provider}</span>
<i data-lucide="file-text" class="w-5 h-5 mt-1"></i>
</div>
<div class="min-w-0">
<h3 class="font-bold text-gray-900 truncate text-lg">${name}</h3>
<p class="text-xs text-gray-500 flex items-center gap-1.5 mt-0.5 truncate"><i data-lucide="map-pin" class="w-3.5 h-3.5"></i> ${addr}</p>
<p class="text-xs text-gray-500 flex items-center gap-1.5 mt-0.5 truncate"><i data-lucide="map-pin" class="w-3.5 h-3.5"></i> ${fullAddr}</p>
<div class="mt-2 flex gap-2">
<span class="text-[10px] bg-gray-100 text-gray-600 px-2 py-0.5 rounded font-bold border border-gray-200">REF: ${svc.service_ref}</span>
<span class="text-[10px] bg-blue-50 text-blue-600 px-2 py-0.5 rounded font-bold border border-blue-100"><i data-lucide="phone" class="w-2.5 h-2.5 inline mr-1"></i>${phone}</span>
<span class="text-[10px] bg-gray-100 text-gray-600 px-2 py-0.5 rounded font-bold border border-gray-200">#${svc.service_ref}</span>
<span class="text-[10px] bg-blue-50 text-blue-600 px-2 py-0.5 rounded font-bold border border-blue-100">${cleanPhone}</span>
</div>
</div>
</div>
<button onclick="openEditor(${svc.id})" class="bg-white border-2 border-blue-600 text-blue-600 hover:bg-blue-600 hover:text-white p-3 rounded-xl transition-all shadow-md active:scale-95 group">
<i data-lucide="arrow-right" class="w-6 h-6"></i>
<button onclick="openEditor(${svc.id})" class="bg-blue-600 text-white p-3 rounded-xl transition-all shadow-lg active:scale-95">
<i data-lucide="edit-3" class="w-5 h-5"></i>
</button>
`;
container.appendChild(card);
@@ -182,14 +186,31 @@
const raw = svc.raw_data;
const myMap = mappings.filter(m => m.provider === svc.provider);
// 1. LIMPIEZA DE TELÉFONO
const phoneVal = raw[myMap.find(m => m.target_key === 'phone')?.original_key] || raw['Teléfono'] || "";
const phoneMatch = phoneVal.match(/[6789]\d{8}/);
document.getElementById('imp_phone').value = phoneMatch ? phoneMatch[0] : "";
document.getElementById('imp_phone_extra').innerText = phoneVal !== (phoneMatch ? phoneMatch[0] : "") ? `Dato original: ${phoneVal}` : "";
// 2. LIMPIEZA DE CP Y DIRECCIÓN
const addr = raw[myMap.find(m => m.target_key === 'address')?.original_key] || "";
const pop = raw[myMap.find(m => m.target_key === 'poblacion')?.original_key] || "";
const cpVal = raw[myMap.find(m => m.target_key === 'cp')?.original_key] || raw['Población_CP'] || "";
const cpMatch = cpVal.match(/\b\d{5}\b/);
document.getElementById('imp_address').value = `${addr} ${pop}`.trim();
document.getElementById('imp_cp').value = cpMatch ? cpMatch[0] : "";
// 3. PROVEEDOR - COMPAÑÍA
const cia = raw[myMap.find(m => m.target_key === 'compania')?.original_key] || raw['Compañía'] || "";
document.getElementById('imp_company').value = `${svc.provider.toUpperCase()} - ${cia}`;
// 4. RESTO DE CAMPOS
document.getElementById('scrapedId').value = scrapedId;
document.getElementById('imp_name').value = raw[myMap.find(m => m.target_key === 'clientName')?.original_key] || "";
document.getElementById('imp_phone').value = raw[myMap.find(m => m.target_key === 'phone')?.original_key] || raw['Teléfono'] || "";
document.getElementById('imp_address').value = raw[myMap.find(m => m.target_key === 'address')?.original_key] || "";
document.getElementById('imp_ref').value = svc.service_ref;
document.getElementById('imp_company').value = raw[myMap.find(m => m.target_key === 'compania')?.original_key] || svc.provider.toUpperCase();
document.getElementById('imp_desc').value = raw[myMap.find(m => m.target_key === 'descripcion')?.original_key] || raw['Descripción'] || "";
document.getElementById('imp_urgent').value = (raw['Urgente'] === 'Sí').toString();
document.getElementById('imp_urgent').value = (raw['Urgente'] === 'Sí' || raw['Urgente'] === 'true').toString();
document.getElementById('importModal').classList.remove('hidden');
lucide.createIcons();
@@ -202,29 +223,28 @@
const btn = document.getElementById('btnImport');
btn.disabled = true; btn.innerText = "IMPORTANDO...";
const id = document.getElementById('scrapedId').value;
const payload = {
name: document.getElementById('imp_name').value,
phone: document.getElementById('imp_phone').value,
address: document.getElementById('imp_address').value,
description: document.getElementById('imp_desc').value,
description: `CP: ${document.getElementById('imp_cp').value}\n${document.getElementById('imp_desc').value}`,
company_ref: document.getElementById('imp_ref').value,
is_urgent: document.getElementById('imp_urgent').value === 'true'
};
try {
const res = await fetch(`${API_URL}/providers/import/${id}`, {
const res = await fetch(`${API_URL}/providers/import/${document.getElementById('scrapedId').value}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${localStorage.getItem('token')}` },
body: JSON.stringify(payload)
});
if((await res.json()).ok) {
showToast("✅ Importado con éxito");
if(res.ok) {
showToast("✅ Servicio importado");
closeModal();
loadInbox();
} else { showToast("Error al importar", true); }
} catch (e) { showToast("Error conexión", true); }
finally { btn.disabled = false; btn.innerText = "IMPORTAR AL CRM"; }
finally { btn.disabled = false; btn.innerHTML = '<i data-lucide="download-cloud"></i> IMPORTAR AL CRM'; lucide.createIcons(); }
}
function showToast(msg, isError = false) {