async function recuperarCantidadGenero(tipoConsulta, filtros = {}) { try { const response = await fetch("../controllers/graficos.php", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ tipoConsulta, ...filtros }), }); const data = await response.json(); return data.cantidad || 0; } catch (error) { console.error("Error al recuperar datos:", error); return 0; } } async function recuperarCantidadEdad(tipoConsulta, filtros = {}) { try { const response = await fetch("../controllers/graficos.php", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ tipoConsulta, ...filtros }), }); const data = await response.json(); return data.cantidad || 0; } catch (error) { console.error("Error al recuperar datos:", error); return 0; } } async function recuperarCantidadEstado(tipoConsulta, filtros = {}) { try { const response = await fetch("../controllers/graficos.php", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ tipoConsulta, ...filtros }), }); const data = await response.json(); if (!Array.isArray(data)) throw new Error("La respuesta del backend no es un array."); return data; } catch (error) { console.error("Error al recuperar datos de estados:", error); return []; } } async function recuperarCantidadExamen(tipoConsulta, filtros = {}) { try { const response = await fetch("../controllers/graficos.php", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ tipoConsulta, ...filtros }), }); const data = await response.json(); if (!Array.isArray(data)) throw new Error("La respuesta del backend no es un array"); return data; } catch (error) { console.error("Error al recuperar datos de examenes:", error); return []; } } async function recuperarCantidadFecha(tipoConsulta, filtros = {}) { try { const response = await fetch("../controllers/graficos.php", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ tipoConsulta, ...filtros }), }); const data = await response.json(); if (!Array.isArray(data)) throw new Error("La respuesta del backend no es un array."); return data; } catch (error) { console.error("Error al recuperar datos de fechas:", error); return []; } } let filtroActivo = null; // Puede ser 'id_rango_edad', 'id_genero', 'id_examen', 'fechas' o null function getFiltrosSeleccionados() { const id_rango_edad = document.getElementById('id_rango_edad')?.value || "NULL"; const id_genero = document.getElementById('id_genero')?.value || "NULL"; const id_examen = document.getElementById('id_examen')?.value || "NULL"; const fechaInicio = document.getElementById('date1')?.value || ""; const fechaFin = document.getElementById('date2')?.value || ""; const filtros = {}; if (id_rango_edad !== "NULL") { filtros.id_rango_edad = id_rango_edad; } if (id_genero !== "NULL") { filtros.id_genero = id_genero; } if (id_examen !== "NULL") { filtros.id_examen = id_examen; } if (fechaInicio && fechaFin) { filtros.fechaInicio = fechaInicio; filtros.fechaFin = fechaFin; } return filtros; } function setFiltroActivo(tipo) { filtroActivo = tipo; // Limpiar otros filtros cuando se activa uno nuevo if (tipo === 'id_rango_edad') { document.getElementById('id_genero').value = "NULL"; document.getElementById('id_examen').value = "NULL"; document.getElementById('date1').value = ""; document.getElementById('date2').value = ""; } else if (tipo === 'id_genero') { document.getElementById('id_rango_edad').value = "NULL"; document.getElementById('id_examen').value = "NULL"; document.getElementById('date1').value = ""; document.getElementById('date2').value = ""; } else if (tipo === 'id_examen') { document.getElementById('id_rango_edad').value = "NULL"; document.getElementById('id_genero').value = "NULL"; document.getElementById('date1').value = ""; document.getElementById('date2').value = ""; } else if (tipo === 'fechas') { document.getElementById('id_rango_edad').value = "NULL"; document.getElementById('id_genero').value = "NULL"; document.getElementById('id_examen').value = "NULL"; } } function getFiltroActivo() { return filtroActivo; } async function obtenerDatosGeneros(filtros = {}) { const response = await fetch("../controllers/graficos.php", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ tipoConsulta: "Generos", ...filtros }), }); return await response.json(); } async function obtenerDatosEdades(filtros = {}) { const response = await fetch("../controllers/graficos.php", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ tipoConsulta: "Edades", ...filtros }), }); return await response.json(); } async function obtenerDatosEstados(filtros = {}) { const response = await fetch("../controllers/graficos.php", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ tipoConsulta: "Estados", ...filtros }), }); return await response.json(); } async function obtenerDatosExamenes(filtros = {}) { try { const response = await fetch("../controllers/graficos.php", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ tipoConsulta: "Examenes", ...filtros }), }); const data = await response.json(); if (!Array.isArray(data)) { console.error("La respuesta del backend no es un array:", data); return []; } return data; } catch (error) { console.error("Error al obtener datos de exámenes:", error); return []; } } async function obtenerDatosFechas(filtros = {}) { const response = await fetch("../controllers/graficos.php", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ tipoConsulta: "Fechas", ...filtros }), }); return await response.json(); } async function obtenerResumenGenero(filtros = {}) { const response = await fetch("../controllers/graficos.php", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ tipoConsulta: "ResumenGenero", ...filtros }), }); return await response.json(); } export { obtenerDatosGeneros, obtenerDatosEdades, obtenerDatosEstados, obtenerDatosExamenes, obtenerDatosFechas, getFiltrosSeleccionados, setFiltroActivo, getFiltroActivo, obtenerResumenGenero // <-- Exporta la nueva función };