<?php
// include db connection
include '../db/db.php';
include '../includes/session_control.php';

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    // Verificar que el usuario esté autenticado
    if (!isset($_SESSION['user'])) {
        header('Content-Type: application/json');
        echo json_encode(['error' => 'User not authenticated']);
        exit;
    }

    // Obtener el idIngreso desde la URL
    $idIngreso = $conn->real_escape_string($_GET['idIngreso'] ?? '');

    if (empty($idIngreso)) {
        header('Content-Type: application/json');
        echo json_encode(['error' => 'Invalid data provided']);
        exit;
    }

    // Consultar los detalles de la cotización
    $sql = "SELECT id, idVariacion, cantidad, precioVenta, observaciones 
            FROM tb_cotizacion_detalle 
            WHERE idCotizacion = '$idIngreso'";

    $result = $conn->query($sql);

    if ($result === FALSE) {
        header('Content-Type: application/json');
        echo json_encode(['error' => $conn->error]);
        exit;
    }

    $data = [];
    while ($row = $result->fetch_assoc()) {
        $data[] = $row;
    }

    header('Content-Type: application/json');
    echo json_encode($data);
}
?>
