323 lines
19 KiB
HTML
323 lines
19 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>
|
|
|
|
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
|
|
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
|
|
|
|
<style>
|
|
body { background-color: #f4f7f9; background-image: radial-gradient(#e2e8f0 1px, transparent 1px); 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; }
|
|
|
|
/* Ocultar marca de agua de leaflet para que quede más limpio */
|
|
.leaflet-control-attribution { display: none !important; }
|
|
</style>
|
|
</head>
|
|
<body class="text-slate-800 font-sans antialiased min-h-screen flex flex-col items-center relative overflow-x-hidden">
|
|
|
|
<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-screen 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">Parece que este enlace ha caducado o no es correcto. Por favor, solicita un nuevo acceso a tu empresa reparadora.</p>
|
|
</div>
|
|
|
|
<main id="mainContent" class="hidden w-full max-w-md mx-auto min-h-screen flex flex-col relative pb-10 z-10">
|
|
|
|
<div class="pt-10 pb-6 px-6 flex flex-col items-center text-center fade-in" style="animation-delay: 0.1s;">
|
|
<div id="companyLogoContainer" class="hidden relative mb-5">
|
|
<div class="absolute inset-0 bg-blue-500 rounded-3xl blur-xl opacity-20"></div>
|
|
<div class="relative w-28 h-28 bg-white rounded-3xl shadow-lg border border-slate-100 flex items-center justify-center overflow-hidden p-3">
|
|
<img id="companyLogo" src="" class="w-full h-full object-contain">
|
|
</div>
|
|
</div>
|
|
<p class="text-[10px] font-black text-blue-600 uppercase tracking-widest mb-1.5 opacity-80">Portal del Cliente</p>
|
|
<h1 id="companyName" class="text-2xl font-black tracking-tight text-slate-900 uppercase leading-none">Empresa</h1>
|
|
</div>
|
|
|
|
<div class="px-5 flex-1 flex flex-col gap-6 fade-in" style="animation-delay: 0.2s;">
|
|
<h2 class="text-3xl font-black text-slate-800 tracking-tight pl-2">Hola, <span id="clientName" class="text-blue-600">Cliente</span></h2>
|
|
|
|
<div id="activeServicesContainer" class="space-y-6 mt-2"></div>
|
|
|
|
<div id="noActiveServices" class="hidden text-center p-6 bg-white/80 backdrop-blur-xl rounded-[2.5rem] shadow-xl shadow-slate-200/40 border border-white">
|
|
<div class="w-16 h-16 bg-slate-100 text-slate-400 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-800 tracking-tight">Todo al día</h3>
|
|
</div>
|
|
|
|
<div id="historyContainerWrapper" class="hidden mt-4">
|
|
<h3 class="text-xs font-black text-slate-400 uppercase tracking-widest mb-3 ml-2">Historial</h3>
|
|
<div id="historyServicesContainer" class="space-y-4"></div>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
|
|
<script>
|
|
const API_URL = window.location.hostname === 'localhost' || window.location.hostname === '127.0.0.1'
|
|
? 'http://localhost:3000'
|
|
: 'https://integrarepara-api.integrarepara.es';
|
|
|
|
let urlToken = "";
|
|
let mapsToInit = [];
|
|
let liveMaps = {};
|
|
let liveMarkers = {};
|
|
|
|
document.addEventListener("DOMContentLoaded", async () => {
|
|
lucide.createIcons();
|
|
const urlParams = new URLSearchParams(window.location.search);
|
|
urlToken = urlParams.get('token');
|
|
|
|
// SOLUCIÓN AL BLOQUEO SIN TOKEN
|
|
if (!urlToken) {
|
|
showError();
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const res = await fetch(`${API_URL}/public/portal/${urlToken}`);
|
|
const data = await res.json();
|
|
if (!data.ok) throw new Error("Token inválido");
|
|
renderPortal(data.client, data.company, data.services);
|
|
} catch (e) {
|
|
console.error(e);
|
|
showError();
|
|
}
|
|
});
|
|
|
|
function showError() {
|
|
document.getElementById('loader').classList.add('opacity-0', 'pointer-events-none');
|
|
setTimeout(() => {
|
|
document.getElementById('loader').classList.add('hidden');
|
|
document.getElementById('errorScreen').classList.remove('hidden');
|
|
document.getElementById('errorScreen').classList.add('flex');
|
|
}, 300);
|
|
}
|
|
|
|
function summarizeDescription(rawText) {
|
|
if (!rawText) return "Avería reportada.";
|
|
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();
|
|
let words = text.split(/\s+/);
|
|
if (words.length > 10) return words.slice(0, 10).join(" ") + "...";
|
|
if (text.length < 5) return "Reparación general solicitada.";
|
|
return text.endsWith('.') ? text : 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 "";
|
|
try {
|
|
const parts = dateStr.split('-');
|
|
if(parts.length !== 3) return dateStr;
|
|
const d = new Date(parts[0], parts[1]-1, parts[2]);
|
|
return d.toLocaleDateString('es-ES', { weekday: 'long', day: 'numeric', month: 'long' });
|
|
} catch(e) { return dateStr; }
|
|
}
|
|
|
|
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');
|
|
}
|
|
document.getElementById('clientName').innerText = client.name.split(' ')[0].toUpperCase();
|
|
|
|
const activeContainer = document.getElementById('activeServicesContainer');
|
|
const historyContainer = document.getElementById('historyServicesContainer');
|
|
|
|
let hasActive = false;
|
|
|
|
allServices.forEach(srv => {
|
|
const isFinalized = srv.status_name === 'Terminado';
|
|
const isPendingWorker = (!srv.assigned_worker || srv.assigned_worker === 'Pendiente');
|
|
const descLimpia = summarizeDescription(srv.description);
|
|
let statusHtml = '';
|
|
|
|
if (isFinalized) {
|
|
statusHtml = `
|
|
<div class="bg-slate-50 border border-slate-200 p-4 rounded-2xl flex items-center justify-between opacity-70 grayscale">
|
|
<span class="text-[10px] font-black text-slate-500 uppercase flex items-center gap-1.5"><i data-lucide="archive" class="w-3.5 h-3.5"></i> ${srv.status_name}</span>
|
|
</div>`;
|
|
}
|
|
else if (srv.status_name === 'Técnico de Camino') {
|
|
// MODO MAPA EN VIVO
|
|
statusHtml = `
|
|
<div class="bg-indigo-50 border border-indigo-200 p-6 rounded-3xl relative overflow-hidden shadow-inner">
|
|
<div class="flex items-center gap-4 mb-5 relative z-10">
|
|
<div class="w-12 h-12 bg-indigo-500 text-white rounded-full flex items-center justify-center shadow-md animate-bounce shrink-0">
|
|
<i data-lucide="truck" class="w-6 h-6"></i>
|
|
</div>
|
|
<div>
|
|
<h4 class="font-black text-indigo-800 uppercase text-base tracking-tight">¡Técnico en camino!</h4>
|
|
<p class="text-[10px] text-indigo-600 font-bold mt-1">Sigue su ubicación en tiempo real.</p>
|
|
</div>
|
|
</div>
|
|
|
|
<div id="liveMap-${srv.id}" class="w-full h-48 bg-slate-200 rounded-2xl border border-indigo-200 relative overflow-hidden z-10 shadow-inner">
|
|
<div class="absolute inset-0 flex items-center justify-center bg-slate-100 z-[400]" id="map-loader-${srv.id}">
|
|
<div class="text-center">
|
|
<i data-lucide="satellite" class="w-6 h-6 mx-auto mb-2 text-indigo-400 animate-pulse"></i>
|
|
<p class="text-[9px] font-black uppercase tracking-widest text-slate-500">Buscando satélites...</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
`;
|
|
mapsToInit.push(srv.id); // Guardamos para inicializar el mapa
|
|
}
|
|
else if (srv.status_name === 'En Reparación') {
|
|
statusHtml = `
|
|
<div class="bg-orange-50 border border-orange-200 p-6 rounded-3xl flex items-center gap-5 shadow-inner">
|
|
<div class="w-14 h-14 bg-orange-500 text-white rounded-2xl flex items-center justify-center shadow-md shrink-0"><i data-lucide="hammer" class="w-7 h-7"></i></div>
|
|
<div>
|
|
<h4 class="font-black text-orange-800 uppercase text-base tracking-tight">En Reparación</h4>
|
|
<p class="text-[10px] text-orange-600 font-bold mt-1.5 leading-tight">El técnico está trabajando en tu domicilio.</p>
|
|
</div>
|
|
</div>`;
|
|
}
|
|
else if (srv.status_name === 'Visita Agendada' || (srv.scheduled_date && srv.scheduled_time)) {
|
|
const endT = addOneHour(srv.scheduled_time);
|
|
statusHtml = `
|
|
<div class="bg-gradient-to-br from-emerald-400 to-emerald-600 p-6 rounded-3xl flex items-center gap-5 text-white shadow-lg shadow-emerald-500/30 relative overflow-hidden">
|
|
<div class="w-14 h-14 bg-white/20 rounded-2xl flex items-center justify-center backdrop-blur-md border border-white/30 shrink-0 relative z-10"><i data-lucide="calendar-check" class="w-7 h-7 text-white"></i></div>
|
|
<div class="relative z-10">
|
|
<p class="text-[9px] font-black uppercase tracking-widest text-emerald-100 mb-0.5">Visita Confirmada</p>
|
|
<p class="font-black uppercase text-lg leading-none mb-1.5">${formatDate(srv.scheduled_date)}</p>
|
|
<p class="text-sm font-bold text-emerald-50 flex items-center gap-1.5"><i data-lucide="clock" class="w-4 h-4"></i> Llegada aprox: ${srv.scheduled_time} - ${endT}</p>
|
|
</div>
|
|
</div>`;
|
|
}
|
|
else {
|
|
statusHtml = `
|
|
<div class="bg-slate-100 border border-slate-200 p-6 rounded-3xl text-center">
|
|
<div class="w-12 h-12 bg-slate-200 text-slate-500 rounded-full flex items-center justify-center mx-auto mb-3"><i data-lucide="clock" class="w-6 h-6"></i></div>
|
|
<h4 class="font-black text-slate-700 uppercase text-sm mb-1 tracking-tight">En Gestión</h4>
|
|
</div>`;
|
|
}
|
|
|
|
let cardHtml = `
|
|
<div class="bg-white/80 backdrop-blur-xl border border-white shadow-xl shadow-slate-200/40 rounded-[2.5rem] p-6 mb-6 relative overflow-hidden">
|
|
<div class="flex justify-between items-start mb-5">
|
|
<span class="text-[10px] font-black text-slate-400 uppercase tracking-widest">Expediente #${srv.title.replace('Expediente #', '').replace('🚨 URGENTE: ', '')}</span>
|
|
${srv.assigned_worker && srv.assigned_worker !== 'Pendiente' ?
|
|
`<div class="bg-slate-50 border border-slate-100 px-3 py-1.5 rounded-lg text-center shrink-0">
|
|
<p class="text-[8px] uppercase font-black text-slate-400 mb-0.5">Técnico</p>
|
|
<p class="text-[10px] font-bold text-blue-600">${srv.assigned_worker.split(' ')[0]}</p>
|
|
</div>` : ''
|
|
}
|
|
</div>
|
|
<div class="mb-6">${statusHtml}</div>
|
|
<div class="pt-5 border-t border-slate-100">
|
|
<p class="text-[10px] font-black text-slate-400 uppercase tracking-widest mb-1.5">Motivo de la Visita</p>
|
|
<h3 class="font-black text-slate-800 text-sm leading-snug">${descLimpia}</h3>
|
|
</div>
|
|
</div>
|
|
`;
|
|
|
|
if (isFinalized) historyContainer.innerHTML += cardHtml;
|
|
else { activeContainer.innerHTML += cardHtml; hasActive = true; }
|
|
});
|
|
|
|
if (!hasActive) document.getElementById('noActiveServices').classList.remove('hidden');
|
|
|
|
lucide.createIcons();
|
|
|
|
document.getElementById('loader').classList.add('opacity-0', 'pointer-events-none');
|
|
setTimeout(() => {
|
|
document.getElementById('loader').classList.add('hidden');
|
|
document.getElementById('mainContent').classList.remove('hidden');
|
|
|
|
// INICIALIZAMOS LOS MAPAS
|
|
mapsToInit.forEach(id => initLiveMap(id));
|
|
}, 300);
|
|
}
|
|
|
|
// ==========================================
|
|
// LÓGICA DE MAPA EN TIEMPO REAL
|
|
// ==========================================
|
|
function initLiveMap(serviceId) {
|
|
// CORRECCIÓN APLICADA: liveMap-
|
|
if(!document.getElementById(`liveMap-${serviceId}`)) return;
|
|
|
|
// 1. Configuramos el mapa de Leaflet
|
|
liveMaps[serviceId] = L.map(`liveMap-${serviceId}`, { zoomControl: false, attributionControl: false }).setView([40.416775, -3.703790], 6);
|
|
L.tileLayer('https://{s}.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}{r}.png').addTo(liveMaps[serviceId]);
|
|
|
|
// 2. Creamos un icono HTML personalizado (un puntito azul que brilla)
|
|
const carIcon = L.divIcon({
|
|
className: 'custom-div-icon',
|
|
html: `<div style="position:relative;"><div style="background-color:#6366f1; width:20px; height:20px; border-radius:50%; border:3px solid white; box-shadow: 0 4px 6px rgba(0,0,0,0.3); position:relative; z-index:2;"></div><div style="position:absolute; top:-5px; left:-5px; width:30px; height:30px; background-color:#6366f1; border-radius:50%; opacity:0.4; animation: ping 1.5s cubic-bezier(0, 0, 0.2, 1) infinite; z-index:1;"></div></div>`,
|
|
iconSize: [20, 20],
|
|
iconAnchor: [10, 10]
|
|
});
|
|
|
|
liveMarkers[serviceId] = L.marker([40.416775, -3.703790], {icon: carIcon});
|
|
|
|
// 3. Ejecutamos la búsqueda de coordenadas cada 10 segundos
|
|
fetchWorkerLocation(serviceId);
|
|
setInterval(() => fetchWorkerLocation(serviceId), 10000);
|
|
}
|
|
|
|
async function fetchWorkerLocation(serviceId) {
|
|
try {
|
|
const res = await fetch(`${API_URL}/public/portal/${urlToken}/location/${serviceId}`);
|
|
const data = await res.json();
|
|
|
|
if (data.ok && data.location) {
|
|
const loader = document.getElementById(`map-loader-${serviceId}`);
|
|
if(loader) loader.classList.add('hidden'); // Ocultar pantalla de carga
|
|
|
|
const lat = parseFloat(data.location.lat);
|
|
const lng = parseFloat(data.location.lng);
|
|
|
|
// Si el marcador no está en el mapa, lo añadimos
|
|
if (!liveMaps[serviceId].hasLayer(liveMarkers[serviceId])) {
|
|
liveMarkers[serviceId].addTo(liveMaps[serviceId]);
|
|
liveMaps[serviceId].setView([lat, lng], 15);
|
|
} else {
|
|
// Si ya está, lo movemos suavemente
|
|
liveMarkers[serviceId].setLatLng([lat, lng]);
|
|
liveMaps[serviceId].flyTo([lat, lng], 16, { animate: true, duration: 1.5 });
|
|
}
|
|
} else {
|
|
const p = document.querySelector(`#map-loader-${serviceId} p`);
|
|
if(p) p.innerText = "ESPERANDO SEÑAL GPS DEL TÉCNICO...";
|
|
}
|
|
} catch(e) {}
|
|
}
|
|
</script>
|
|
</body>
|
|
</html> |