<?php
// Incluir el archivo de conexión
require 'conexionbd.php';

// Obtener las fechas de inicio y fin desde la solicitud (GET)
$fechaInicio = $_GET['fechaInicio'] ?? '';
$fechaFin = $_GET['fechaFin'] ?? '';

// Validar que las fechas estén presentes
if (empty($fechaInicio) || empty($fechaFin)) {
    die(json_encode(['error' => 'Faltan parámetros (fechaInicio y fechaFin).']));
}

try {
    // Consultar el total de boletos vendidos en el período
    $stmtTotal = $conn->prepare("SELECT COUNT(*) AS total FROM ventas WHERE fecha_venta BETWEEN :fechaInicio AND :fechaFin");
    $stmtTotal->execute([':fechaInicio' => $fechaInicio, ':fechaFin' => $fechaFin]);
    $totalBoletos = $stmtTotal->fetch(PDO::FETCH_ASSOC)['total'];

    // Consultar las ventas en el período con JOIN a la tabla asientos
    $stmtVentas = $conn->prepare("
        SELECT v.fecha_venta, a.asiento, v.precio
        FROM ventas v
        JOIN asientos a ON v.asiento_id = a.id
        WHERE v.fecha_venta BETWEEN :fechaInicio AND :fechaFin
    ");
    $stmtVentas->execute([':fechaInicio' => $fechaInicio, ':fechaFin' => $fechaFin]);
    $ventas = $stmtVentas->fetchAll(PDO::FETCH_ASSOC);

    // Devolver los datos en formato JSON
    echo json_encode([
        'success' => true,
        'totalBoletos' => $totalBoletos,
        'ventas' => $ventas
    ]);
} catch (PDOException $e) {
    // Devolver un mensaje de error en caso de fallo
    die(json_encode(['error' => 'Error al consultar las ventas: ' . $e->getMessage()]));
}
?>