Files
web/automatizaciones.html

238 lines
14 KiB
HTML

<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Automatizaciones - IntegraRepara</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://unpkg.com/lucide@latest"></script>
<style>
.fade-in { animation: fadeIn 0.3s ease-in-out; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.no-scrollbar::-webkit-scrollbar { display: none; }
.no-scrollbar { -ms-overflow-style: none; scrollbar-width: none; }
</style>
</head>
<body class="bg-gray-50 text-gray-800 font-sans antialiased overflow-hidden">
<div class="flex h-screen overflow-hidden">
<div id="sidebar-container" class="h-full shrink-0"></div>
<div class="flex-1 flex flex-col overflow-hidden relative">
<div id="header-container"></div>
<main class="flex-1 overflow-x-hidden overflow-y-auto bg-gray-50 p-6 relative">
<div id="automationView" class="fade-in">
<div class="flex justify-between items-center mb-6">
<div>
<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>
</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
</button>
</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="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
</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">
<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>
<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">
</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">
</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>
</div>
<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>
</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>
</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>
<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>
</select>
</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
</button>
</div>
</form>
</div>
</div>
<div id="toast" class="fixed bottom-5 right-5 bg-slate-800 text-white px-6 py-3 rounded-lg shadow-2xl hidden z-[60]"><span id="toastMsg"></span></div>
<script src="js/layout.js"></script>
<script>
let mappings = [];
let scrapedData = [];
document.addEventListener("DOMContentLoaded", () => {
if (!localStorage.getItem("token")) window.location.href = "index.html";
loadInbox();
});
async function loadInbox() {
const container = document.getElementById('inboxContainer');
try {
const [resSvc, resMap] = await Promise.all([
fetch(`${API_URL}/providers/scraped`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } }),
fetch(`${API_URL}/discovery/mappings`, { headers: { "Authorization": `Bearer ${localStorage.getItem("token")}` } })
]);
const dataSvc = await resSvc.json();
mappings = await resMap.json();
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>';
return;
}
container.innerHTML = "";
scrapedData.forEach(svc => {
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 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>
<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>
<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>
</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>
`;
container.appendChild(card);
});
lucide.createIcons();
} catch (e) { showToast("Error de conexión", true); }
}
function openEditor(scrapedId) {
const svc = scrapedData.find(s => s.id === scrapedId);
if(!svc) return;
const raw = svc.raw_data;
const myMap = mappings.filter(m => m.provider === svc.provider);
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('importModal').classList.remove('hidden');
lucide.createIcons();
}
function closeModal() { document.getElementById('importModal').classList.add('hidden'); }
async function handleImportSubmit(e) {
e.preventDefault();
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,
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}`, {
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");
closeModal();
loadInbox();
} else { showToast("Error al importar", true); }
} catch (e) { showToast("Error conexión", true); }
finally { btn.disabled = false; btn.innerText = "IMPORTAR AL CRM"; }
}
function showToast(msg, isError = false) {
const t = document.getElementById('toast'), m = document.getElementById('toastMsg');
t.className = `fixed bottom-5 right-5 px-6 py-3 rounded-lg shadow-2xl z-[60] flex items-center gap-3 ${isError ? 'bg-red-600' : 'bg-slate-800'} text-white font-medium`;
m.innerText = msg; t.classList.remove('hidden');
setTimeout(() => t.classList.add('hidden'), 3000);
}
</script>
</body>
</html>