diff --git a/index.html b/index.html index 96f9231..638c75b 100644 --- a/index.html +++ b/index.html @@ -435,13 +435,14 @@ // ========================================== async function calculateClientETA(serviceId, destAddress) { const container = document.getElementById(`eta-container-${serviceId}`); + if (!container) return; try { const res = await fetch(`${API_URL}/public/portal/${urlToken}/location/${serviceId}`); const data = await res.json(); if (!data.ok || !data.location) { - container.innerHTML = `
El técnico está conduciendo hacia tu domicilio.
`; + container.innerHTML = `El técnico está en camino hacia tu domicilio.
`; return; } @@ -469,19 +470,20 @@ const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); const km = R * c; - const totalMins = Math.round((km/35)*60) + 5; - -// Simplemente asignamos el tiempo basado en la distancia real -let remainingMins = totalMins; - -// Calculamos el tiempo transcurrido solo para animar la barra de progreso -const startedAt = new Date(data.location.updated_at).getTime(); -const diffMins = Math.floor((new Date().getTime() - startedAt) / 60000); - -let progressPercent = (diffMins / totalMins) * 100; - if (remainingMins < 1) remainingMins = 1; + // Tiempo basado directamente en la distancia (sin restar) + const totalMins = Math.round((km/35)*60) + 5; + let remainingMins = totalMins; + + // Porcentaje visual para la barra animada + let progressPercent = 5; + if (data.location.updated_at) { + const startedAt = new Date(data.location.updated_at).getTime(); + if (!isNaN(startedAt)) { + const diffMins = Math.floor((new Date().getTime() - startedAt) / 60000); + progressPercent = (diffMins / totalMins) * 100; + } + } - let progressPercent = (diffMins / totalMins) * 100; if (progressPercent > 95) progressPercent = 95; if (progressPercent < 5) progressPercent = 5; @@ -501,10 +503,11 @@ let progressPercent = (diffMins / totalMins) * 100; `; lucide.createIcons(); } else { - container.innerHTML = `El técnico está conduciendo hacia tu domicilio.
`; + container.innerHTML = `El técnico está en camino hacia tu domicilio.
`; } } catch(e) { - container.innerHTML = `El técnico está conduciendo hacia tu domicilio.
`; + console.error("Error de GPS: ", e); + if (container) container.innerHTML = `El técnico está de camino.
`; } }