<?php
session_start();
header('Content-Type: application/json');

require_once 'includes/db.php';

try {
    if (!isset($_POST['grupo_id'])) {
        throw new Exception('Grupo ID no proporcionado');
    }

    $db = new Database();
    $stmt = $db->prepare("
        SELECT id 
        FROM venta_items 
        WHERE grupo_id = ? 
        AND estado = 'EN_CURSO' 
        ORDER BY id DESC 
        LIMIT 1
    ");
    $stmt->execute([$_POST['grupo_id']]);
    $venta_id = $stmt->fetchColumn();

    echo json_encode([
        'success' => true,
        'venta_id' => $venta_id
    ]);

} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'message' => $e->getMessage()
    ]);
}
