<?php
    $json = file_get_contents('php://input');   
    $obj = json_decode($json);
    $id = $obj->flag ?? null;
    $msg;

    $conn = new mysqli("directoriofitness.com.mx","direct18","L4LL4v3M@3str@","direct18_ProGymCloud");
    //$conn -> set_charset("utf8");

    // Returns a JSON
    header('Content-Type: application/json');
    
    $gym = $conn->real_escape_string($obj->gym);
    $sucursal = (int)$obj->sucursal; 

    $query = "SELECT inp.* FROM info_plataformaspago inp 
              INNER JOIN gimnasios g ON inp.gimnasio_idGimnasio = g.idGimnasio 
              WHERE g.BaseDatos = '$gym' AND inp.sucursal_idSucursal = $sucursal";

    $result = $conn->query($query);
    if ($result === false) {
        // Debugging: print the SQL error
        echo json_encode(["error" => "SQL Error: " . $conn->error]);
        exit();
    }
    
    $array = [];

    if ($result->num_rows > 0) {
        // output data of each row
        while ($row = $result->fetch_assoc()) {
            $array[] = $row;
        }
    } else {
        $array[] = [
            "id_info_plataformaspago" => "0",
            "gimnasio_idGimnasio" => "0",
            "sucursal_idSucursal" => "0",
            "activar_checkout" => "0",
            "activar_firstdata" => "0",
            "activar_mercadopago" => "0",
            "activar_paypal" => "0",
            "activar_payu" => "0",
            "activar_stripe" => "0"
        ];
    }

    echo json_encode($array);

    $conn->close();
?>