<?php
$json = file_get_contents('php://input');   
$obj = json_decode($json);
$id = $obj->flag;
$msg;
    
// Returns a Json
header('Content-Type: application/json');
$array;
switch($id){
 
    case '0': //get gimnasios
        
        $conn = new mysqli("directoriofitness.com.mx","direct18","L4LL4v3M@3str@",'direct18_ProGymCloud');
        $conn -> set_charset("utf8");
        $query = "SELECT * FROM gimnasios WHERE clave_clientes != '' AND BaseDatos != '' ORDER BY NombreGimnasio ASC";
        $resultado = $conn->query($query);
        
        //$i = 0;
        //echo '[';
        while($row = $resultado->fetch_assoc()) {
            $array[] = $row;
            
            //echo json_encode($row);
            //echo ',';
        }
        
        //echo ']';
        echo json_encode($array);
        $conn->close();
        break;
         
    case '1': //get sucursales
        
        /*$conn = new mysqli("directoriofitness.com.mx","direct18","L4LL4v3M@3str@",$obj->bd);
        $conn -> set_charset("utf8");
        $query = "SELECT * FROM sucursal";
        $resultado = $conn->query($query);
         
        //echo '[';
        while($row = $resultado->fetch_assoc()) {
            $array[] = [idSucursal => $row['idSucursal'],'NombreSucursal'=>$row['NombreSucursal']];

        }
        
        //echo ']';
        //echo json_encode($array);
        echo json_encode($array);
        $conn->close();
        break;*/
        
        $conn = new mysqli("directoriofitness.com.mx","direct18","L4LL4v3M@3str@",$obj->bd);
        $conn -> set_charset("utf8");
        $query = "SELECT * FROM sucursal";
        $resultado = $conn->query($query);
         
        if ($resultado) {
            while ($row = $resultado->fetch_assoc()) {
                $array[] = [
                    'idSucursal' => $row['idSucursal'],
                    'NombreSucursal' => $row['NombreSucursal']
                ];
            }
        } else {
            echo json_encode(["error" => $conn->error]);
            exit();
        }
        echo json_encode($array);
        $conn->close();
        break;

    case '2': //get paquetes
    $conn = new mysqli("directoriofitness.com.mx","direct18","L4LL4v3M@3str@",$obj->bd);
    $conn -> set_charset("utf8");
        $query = "SELECT * FROM servicios WHERE Borrado = 0 AND Sucursal = $obj->sucursal AND PVisita != '' GROUP BY NombreServicio ORDER BY NombreServicio ASC";
        
        $resultado = $conn->query($query);
            
        echo '[';
        while($row = $resultado->fetch_assoc()) {
            $array[] = $row;
            
            echo json_encode($row);
            echo ',';
        }
        
        echo ']';
        
        $conn->close();
        
        break;
    
    
}
?>