<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Content-Type");
header("Access-Control-Allow-Methods: POST, OPTIONS");
header("Content-Type: application/json");

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

require_once(__DIR__ . '/../config.php');

$input = json_decode(file_get_contents('php://input'), true);
$slug_tienda = trim($input['slug_tienda'] ?? '');

if ($slug_tienda === '') {
    echo json_encode(['success' => false, 'message' => 'Falta slug_tienda']);
    exit;
}

$stmt = $mysqli->prepare("SELECT id, identificador, etiqueta, activo FROM pos_medios_pago WHERE slug_tienda=? ORDER BY id ASC");
$stmt->bind_param("s", $slug_tienda);
$stmt->execute();
$res = $stmt->get_result();

$rows = [];
while ($r = $res->fetch_assoc()) $rows[] = $r;

echo json_encode(['success' => true, 'medios' => $rows]);
$stmt->close();
$mysqli->close();
