document.getElementById('formReporte').addEventListener('submit', function (event) {
    event.preventDefault(); 

    const fechaInicio = document.getElementById('fechaInicio').value;
    const fechaFin = document.getElementById('fechaFin').value;

    cargarReporte(fechaInicio, fechaFin);
});

function cargarReporte(fechaInicio, fechaFin) {
    const url = `control/reporte_ventas.php?fechaInicio=${encodeURIComponent(fechaInicio)}&fechaFin=${encodeURIComponent(fechaFin)}`;

    fetch(url)
        .then(response => response.json())
        .then(data => {
            if (data.error) {
                console.error("Error al cargar el reporte:", data.error);
                return;
            }

            document.getElementById('totalBoletos').textContent = data.totalBoletos;

            const tbody = document.querySelector('#tablaVentas tbody');
            tbody.innerHTML = '';

            data.ventas.forEach(venta => {
                const fila = document.createElement('tr');
                fila.innerHTML = `
                    <td>${venta.fecha_venta}</td>
                    <td>${venta.asiento}</td>
                    <td>$${venta.precio}</td>
                `;
                tbody.appendChild(fila);
            });
        })
        .catch(error => console.error('Error al cargar el reporte:', error));
}

setInterval(() => {
    const fechaInicio = document.getElementById('fechaInicio').value;
    const fechaFin = document.getElementById('fechaFin').value;

    if (fechaInicio && fechaFin) {
        cargarReporte(fechaInicio, fechaFin);
    }
}, 10000);