Files
Portal/index.html
2026-03-15 18:56:40 +00:00

273 lines
17 KiB
HTML

<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Portal del Cliente - IntegraRepara</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://unpkg.com/lucide@latest"></script>
<style>
/* Fondo Marsalva Style */
body {
background-color: #f8fafc;
background-image: radial-gradient(#cbd5e1 1.5px, transparent 1.5px);
background-size: 24px 24px;
}
.fade-in { animation: fadeIn 0.5s cubic-bezier(0.16, 1, 0.3, 1) forwards; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: translateY(0); } }
.blob { position: absolute; filter: blur(60px); z-index: -1; opacity: 0.4; }
.no-scrollbar::-webkit-scrollbar { display: none; }
.no-scrollbar { -ms-overflow-style: none; scrollbar-width: none; }
/* Contenedor principal estilo formulario */
.main-card {
background: white;
border-radius: 2.5rem;
box-shadow: 0 25px 50px -12px rgba(203, 213, 225, 0.6);
border: 1px solid #f1f5f9;
}
</style>
</head>
<body class="text-slate-800 font-sans antialiased min-h-screen flex flex-col items-center relative overflow-x-hidden py-10 px-4">
<div class="blob bg-blue-300 w-64 h-64 rounded-full top-[-50px] left-[-50px]"></div>
<div class="blob bg-emerald-200 w-64 h-64 rounded-full top-[20%] right-[-100px]"></div>
<div id="loader" class="fixed inset-0 bg-slate-50/90 backdrop-blur-md z-50 flex flex-col items-center justify-center transition-opacity duration-300">
<div class="relative w-16 h-16 flex items-center justify-center mb-4">
<div class="absolute inset-0 border-4 border-blue-100 border-t-blue-600 rounded-full animate-spin"></div>
<i data-lucide="wrench" class="w-6 h-6 text-blue-600 animate-pulse"></i>
</div>
<p class="text-sm font-black tracking-widest uppercase text-slate-500 animate-pulse">Conectando...</p>
</div>
<div id="errorScreen" class="hidden w-full max-w-md mx-auto p-6 flex-col items-center justify-center min-h-[60vh] text-center fade-in z-10 relative">
<div class="w-24 h-24 bg-red-50 text-red-500 rounded-full flex items-center justify-center mb-6 shadow-xl shadow-red-500/20 border border-red-100">
<i data-lucide="shield-alert" class="w-12 h-12"></i>
</div>
<h2 class="text-3xl font-black text-slate-800 mb-3 tracking-tight">Enlace no válido</h2>
<p class="text-slate-500 font-medium leading-relaxed">Este enlace ha caducado o es incorrecto.</p>
</div>
<main id="mainContent" class="hidden w-full max-w-md mx-auto flex flex-col relative z-10">
<div class="flex flex-col items-center text-center mb-10 fade-in" style="animation-delay: 0.1s;">
<div id="companyLogoContainer" class="hidden relative mb-6">
<div class="absolute inset-0 bg-blue-500 rounded-[2rem] blur-xl opacity-20"></div>
<div class="relative w-28 h-28 bg-white rounded-[2rem] shadow-2xl shadow-slate-200 flex items-center justify-center overflow-hidden p-4 border border-white">
<img id="companyLogo" src="" class="w-full h-full object-contain">
</div>
</div>
<p class="text-[10px] font-black text-blue-600 uppercase tracking-[0.2em] mb-1 opacity-80">Portal del Cliente</p>
<h1 id="companyName" class="text-2xl font-black tracking-tight text-slate-800 uppercase leading-none">Empresa</h1>
</div>
<div class="px-2 mb-8 fade-in" style="animation-delay: 0.2s;">
<h2 class="text-5xl font-black text-slate-800 tracking-tight leading-none italic">Hola, <br><span id="clientName" class="text-blue-600 not-italic">Cliente</span></h2>
</div>
<div class="main-card p-6 md:p-8 space-y-6 fade-in" style="animation-delay: 0.3s;">
<div id="activeServicesContainer" class="space-y-6"></div>
<div id="noActiveServices" class="hidden text-center py-10">
<div class="w-16 h-16 bg-slate-50 text-slate-300 rounded-full flex items-center justify-center mx-auto mb-4">
<i data-lucide="check-circle" class="w-8 h-8"></i>
</div>
<h3 class="text-lg font-black text-slate-400 tracking-tight uppercase">Todo al día</h3>
</div>
<div id="historyContainerWrapper" class="hidden pt-6 border-t border-slate-50">
<h3 class="text-[10px] font-black text-slate-400 uppercase tracking-widest mb-4 ml-2">Historial reciente</h3>
<div id="historyServicesContainer" class="space-y-4"></div>
</div>
</div>
</main>
<script>
const API_URL = "https://integrarepara-api.integrarepara.es";
let urlToken = "";
let etasToInit = [];
document.addEventListener("DOMContentLoaded", async () => {
const urlParams = new URLSearchParams(window.location.search);
urlToken = urlParams.get('token');
const serviceParam = urlParams.get('service');
if (!urlToken) { showError(); return; }
try {
let fetchUrl = `${API_URL}/public/portal/${urlToken}`;
if (serviceParam) fetchUrl += `?service=${serviceParam}`;
const res = await fetch(fetchUrl);
const data = await res.json();
if (!data.ok) throw new Error("Token inválido");
renderPortal(data.client, data.company, data.services || []);
} catch (e) { showError(); }
lucide.createIcons();
});
function showError() {
document.getElementById('loader').classList.add('hidden');
document.getElementById('errorScreen').classList.remove('hidden');
document.getElementById('errorScreen').classList.add('flex');
}
function summarizeDescription(rawText) {
if (!rawText) return "Asistencia técnica solicitada.";
let text = rawText.replace(/\n/g, ' ');
const regexCorte = /(\(|\bM\.O\b|\bMATERIAL\b|\bASEGURADO\b|\bTELEFONO\b|\bURGENTE\b)/i;
let cutPos = text.search(regexCorte);
if (cutPos > 5) text = text.substring(0, cutPos);
text = text.trim();
text = text.charAt(0).toUpperCase() + text.slice(1).toLowerCase();
return text.length > 100 ? text.substring(0, 100) + "..." : text;
}
function addOneHour(timeStr) {
if(!timeStr) return "";
let [h, m] = timeStr.split(':').map(Number);
let tm = h * 60 + m + 60;
return `${String(Math.floor(tm / 60)).padStart(2,'0')}:${String(tm % 60).padStart(2,'0')}`;
}
function formatDate(dateStr) {
if (!dateStr) return "";
const d = new Date(dateStr);
return d.toLocaleDateString('es-ES', { weekday: 'long', day: 'numeric', month: 'long' });
}
function renderPortal(client, company, allServices) {
document.getElementById('companyName').innerText = company.name || "Empresa";
if (company.logo) {
document.getElementById('companyLogo').src = company.logo;
document.getElementById('companyLogoContainer').classList.remove('hidden');
}
let cName = client?.name?.split(' ')[0] || "Cliente";
document.getElementById('clientName').innerText = cName.toUpperCase();
const activeContainer = document.getElementById('activeServicesContainer');
const historyContainer = document.getElementById('historyServicesContainer');
let hasActive = false; let hasHistory = false;
allServices.forEach(srv => {
let isFinalized = srv.is_final === true;
let raw = srv.raw_data || {};
let descLimpia = summarizeDescription(srv.description);
let stNameLower = (srv.status_name || "").toLowerCase();
let isUrgent = srv.is_urgent === true || (raw['Urgente'] && ["sí","si","true"].includes(raw['Urgente'].toLowerCase()));
let hasWorker = srv.assigned_worker && !["Pendiente", "Sin asignar"].includes(srv.assigned_worker);
let hasDate = srv.scheduled_date && srv.scheduled_time;
let statusHtml = '';
// LÓGICA DE ESTADOS (CAMINO, TRABAJANDO, CITA, URGENCIA...)
if (isFinalized || stNameLower.includes('finalizado') || stNameLower.includes('anulado')) {
statusHtml = `<div class="bg-slate-50 border border-slate-100 p-4 rounded-2xl flex items-center justify-between opacity-60 grayscale">
<span class="text-[10px] font-black text-slate-400 uppercase flex items-center gap-1.5"><i data-lucide="archive" class="w-3 h-3"></i> Finalizado</span>
</div>`;
}
else if (stNameLower.includes('camino')) {
etasToInit.push({ id: srv.id, address: `${raw["Dirección"] || ""}, ${raw["Población"] || ""}` });
statusHtml = `<div class="bg-indigo-50 border border-indigo-100 p-5 rounded-[2rem]">
<div class="flex items-center gap-4">
<div class="w-12 h-12 bg-indigo-600 text-white rounded-2xl flex items-center justify-center shadow-lg animate-pulse shrink-0"><i data-lucide="truck" class="w-6 h-6"></i></div>
<div class="flex-1"><h4 class="font-black text-indigo-900 uppercase text-sm tracking-tight">¡En camino!</h4>
<div id="eta-container-${srv.id}"><p class="text-[10px] font-bold text-indigo-400 animate-pulse">Calculando llegada...</p></div></div>
</div>
</div>`;
}
else if (isUrgent) {
statusHtml = `<div class="bg-red-50 border border-red-100 p-6 rounded-[2rem] relative overflow-hidden shadow-inner">
<div class="flex items-center gap-4 relative z-10">
<div class="w-14 h-14 bg-red-600 text-white rounded-2xl flex items-center justify-center shadow-lg shadow-red-200 shrink-0 animate-pulse"><i data-lucide="flame" class="w-7 h-7"></i></div>
<div class="flex-1">
<p class="text-[9px] font-black uppercase tracking-widest text-red-500 mb-0.5">Atención Inmediata</p>
<h4 class="font-black text-red-900 uppercase text-lg leading-none mb-1 tracking-tight">Aviso Urgente</h4>
<p class="text-[10px] font-bold text-red-700 leading-tight">${hasWorker ? 'Técnico asignado. Acudirá de inmediato.' : 'Localizando técnico de guardia...'}</p>
</div>
</div>
</div>`;
}
else if (hasDate) {
let endT = addOneHour(srv.scheduled_time);
statusHtml = `<div class="bg-emerald-50 border border-emerald-100 p-6 rounded-[2rem] shadow-sm">
<div class="flex items-center gap-4 mb-4">
<div class="w-12 h-12 bg-emerald-500 text-white rounded-2xl flex items-center justify-center shrink-0"><i data-lucide="calendar-check" class="w-6 h-6"></i></div>
<div class="flex-1"><p class="text-[9px] font-black uppercase text-emerald-600 mb-0.5">Visita Agendada</p>
<p class="font-black text-emerald-900 uppercase text-base">${formatDate(srv.scheduled_date)}</p>
<p class="text-xs font-bold text-emerald-700">${srv.scheduled_time} - ${endT}</p></div>
</div>
<a href="cita.html?token=${urlToken}&service=${srv.id}" class="w-full bg-white/60 hover:bg-emerald-100 text-emerald-700 border border-emerald-200 font-black py-3 rounded-xl flex items-center justify-center gap-2 transition-all text-[10px] uppercase tracking-widest">Modificar Cita</a>
</div>`;
}
else if (hasWorker) {
statusHtml = `<div class="bg-blue-50 border border-blue-100 p-6 rounded-[2rem]">
<div class="flex items-center gap-4 mb-4">
<div class="w-12 h-12 bg-blue-600 text-white rounded-2xl flex items-center justify-center shrink-0"><i data-lucide="calendar-plus" class="w-6 h-6"></i></div>
<div class="flex-1"><p class="text-[9px] font-black text-blue-500 uppercase mb-0.5">Acción Requerida</p>
<h4 class="font-black text-blue-900 uppercase text-base leading-tight">Elige tu cita</h4></div>
</div>
<a href="cita.html?token=${urlToken}&service=${srv.id}" class="w-full bg-blue-600 text-white font-black py-4 rounded-2xl flex items-center justify-center gap-2 text-[10px] uppercase tracking-widest shadow-lg shadow-blue-100">Agendar Ahora</a>
</div>`;
}
else {
statusHtml = `<div class="bg-slate-50 border border-slate-100 p-5 rounded-[2rem] flex items-center gap-4">
<div class="w-10 h-10 bg-slate-200 text-slate-500 rounded-xl flex items-center justify-center shrink-0"><i data-lucide="clock" class="w-5 h-5 animate-pulse"></i></div>
<div><h4 class="font-black text-slate-700 uppercase text-xs">Pendiente de Técnico</h4>
<p class="text-[10px] text-slate-400 font-bold">Asignando profesional en tu zona...</p></div>
</div>`;
}
let contactHtml = (hasWorker && !isFinalized) ? `<div class="flex gap-2 mt-4">
<a href="tel:+${srv.worker_phone}" class="flex-1 bg-white border border-slate-100 text-slate-600 font-black py-3 rounded-xl flex items-center justify-center gap-1.5 text-[9px] uppercase tracking-widest shadow-sm"><i data-lucide="phone" class="w-3.5 h-3.5"></i> Llamar</a>
<a href="https://wa.me/${srv.worker_phone}" target="_blank" class="flex-1 bg-emerald-50 text-emerald-600 border border-emerald-100 font-black py-3 rounded-xl flex items-center justify-center gap-1.5 text-[9px] uppercase tracking-widest shadow-sm"><i data-lucide="message-circle" class="w-3.5 h-3.5"></i> WhatsApp</a>
</div>` : '';
let cardHtml = `<div class="fade-in">
<div class="flex justify-between items-center mb-3 px-2">
<span class="text-[9px] font-black text-slate-400 uppercase tracking-widest">${srv.title || 'Aviso'} #${srv.service_ref || '---'}</span>
${hasWorker ? `<span class="text-[9px] font-black text-blue-500 bg-blue-50 px-2 py-1 rounded-lg">TÉCNICO: ${srv.assigned_worker.split(' ')[0].toUpperCase()}</span>` : ''}
</div>
${statusHtml}
${contactHtml}
<div class="mt-4 px-2">
<p class="text-[9px] font-black text-slate-300 uppercase tracking-widest mb-1">Motivo de la visita</p>
<p class="text-sm font-bold text-slate-600 leading-snug">${descLimpia}</p>
</div>
</div>`;
if (isFinalized) { historyContainer.innerHTML += cardHtml; hasHistory = true; }
else { activeContainer.innerHTML += cardHtml; hasActive = true; }
});
if (!hasActive) document.getElementById('noActiveServices').classList.remove('hidden');
if (hasHistory) document.getElementById('historyContainerWrapper').classList.remove('hidden');
document.getElementById('loader').classList.add('opacity-0');
setTimeout(() => {
document.getElementById('loader').classList.add('hidden');
document.getElementById('mainContent').classList.remove('hidden');
etasToInit.forEach(item => calculateClientETA(item.id, item.address));
lucide.createIcons();
}, 300);
}
async function calculateClientETA(serviceId, destAddress) {
const container = document.getElementById(`eta-container-${serviceId}`);
try {
const res = await fetch(`${API_URL}/public/portal/${urlToken}/location/${serviceId}`);
const data = await res.json();
if (data.ok && data.location) {
container.innerHTML = `<p class="text-xs font-black text-indigo-600">El técnico llegará pronto.</p>`;
}
} catch(e) { }
}
</script>
</body>
</html>