<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization");

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    http_response_code(200);
    exit;
}

require_once(__DIR__ . '/../config.php'); // Ajusta la ruta a tu config.php

// Recibir JSON
$input = json_decode(file_get_contents("php://input"), true);
$slug = $input['slug_tienda'] ?? null;

// Si quieres filtrar por slug, descomenta esta validación
/*
if (!$slug) {
    echo json_encode(['success' => false, 'message' => 'Falta slug']);
    exit;
}
*/

// Consulta
// Si no necesitas slug, solo filtra por estado
$stmt = $mysqli->prepare("SELECT * FROM parametros_campania WHERE SLUG_TIENDA = ? AND ESTADO = 'ACTIVO'");
$stmt->bind_param("s", $slug);

//$stmt = $mysqli->prepare("SELECT ID_PARAM, NUMERO_CEL, PARAM_1, PARAM_2, PARAM_3, PARAM_4, PARAM_5, PARAM_6, PARAM_7, PARAM_8, PARAM_9, PARAM_10, ESTADO FROM parametros_campania  WHERE ESTADO = 'ACTIVO'");

$stmt->execute();
$result = $stmt->get_result();

$parametros = [];
while ($row = $result->fetch_assoc()) {
    $parametros[] = $row;
}

echo json_encode([
    'success' => true,
    'total' => count($parametros),
    'parametros' => $parametros
], JSON_UNESCAPED_UNICODE);

$stmt->close();
$mysqli->close();
