diff --git a/index.html b/index.html index 638c75b..f8cb439 100644 --- a/index.html +++ b/index.html @@ -430,7 +430,7 @@ }, 300); } - // ========================================== + // ========================================== // CÁLCULO DE ETA (TIEMPO ESTIMADO) INTELIGENTE // ========================================== async function calculateClientETA(serviceId, destAddress) { @@ -470,38 +470,52 @@ const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); const km = R * c; - // Tiempo basado directamente en la distancia (sin restar) + // Tiempo total estimado basado en la distancia const totalMins = Math.round((km/35)*60) + 5; - let remainingMins = totalMins; - // Porcentaje visual para la barra animada - let progressPercent = 5; + // Extraemos la hora exacta en la que el técnico pulsó el botón + let startedAt = new Date().getTime(); 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; - } + const parsed = new Date(data.location.updated_at).getTime(); + if (!isNaN(parsed)) startedAt = parsed; } - - if (progressPercent > 95) progressPercent = 95; - if (progressPercent < 5) progressPercent = 5; - container.innerHTML = ` -

- Llegada en aprox. ${remainingMins} min -

-
-
-
+ // CREAMOS UNA FUNCIÓN INTERNA PARA ACTUALIZAR EN TIEMPO REAL + function renderETA() { + const now = new Date().getTime(); + const diffMins = Math.floor((now - startedAt) / 60000); + + // Restamos el tiempo que ya ha pasado conduciendo + let remainingMins = totalMins - diffMins; + if (remainingMins < 1) remainingMins = 1; // Nunca bajará de 1 minuto + + let progressPercent = (diffMins / totalMins) * 100; + if (progressPercent > 95) progressPercent = 95; + if (progressPercent < 5) progressPercent = 5; + + container.innerHTML = ` +

+ Llegada en aprox. ${remainingMins} min +

+
+
+
+
-
-
-

Saliendo

-

A ${km.toFixed(1)} km

-
- `; - lucide.createIcons(); +
+

Saliendo

+

A ${km.toFixed(1)} km

+
+ `; + lucide.createIcons(); + } + + // 1. Lo pintamos inmediatamente al cargar + renderETA(); + + // 2. Lo actualizamos automáticamente cada 60 segundos (Magia en tiempo real) + setInterval(renderETA, 60000); + } else { container.innerHTML = `

El técnico está en camino hacia tu domicilio.

`; }