function buscarDatos() {
const fechaSeleccionada = document.getElementById("fecha").value;
if (!fechaSeleccionada) {
alert("⚠️ Selecciona una fecha");
return;
}
fetch(`http://localhost:3000/datos?fecha=${fechaSeleccionada}`)
.then(response => response.json())
.then(data => {
const tabla = document.getElementById("tablaDatos");
const imagenPrincipal = document.getElementById("imagenPrincipal");
tabla.innerHTML = "";
if (data.length === 0) {
tabla.innerHTML = "<tr><td colspan='7'>❌ No hay datos para esta
fecha.</td></tr>";
imagenPrincipal.src = "default.png";
return;
}
imagenPrincipal.src = data[0].imagen; // Primera imagen
data.forEach(dato => {
const fila = document.createElement("tr");
fila.innerHTML = `
<td>${dato.tempMax}</td>
<td>${dato.tempMin}</td>
<td>${dato.ph}</td>
<td>${dato.humedad}</td>
<td>${dato.fecha}</td>
<td>${dato.hora}</td>
<td><img src="${dato.imagen}" alt="Miniatura" /></td>
`;
// Al hacer clic en la fila o miniatura, cambia la imagen principal
fila.addEventListener("click", () => {
imagenPrincipal.src = dato.imagen;
});
tabla.appendChild(fila);
});
})
.catch(error => console.error("Error al obtener datos:", error));
}