<?php
include '../clases/db.php';
include '../clases/db2.php';


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

// Returns a Json
header('Content-Type: application/json');
//header("Access-Control-Allow-Origin: *");
//echo "flag: ".$id;
switch($id){
    case '0':
        $msg = "ok";
        $baseD = "entre";
        $nombreGym;
        $db = new conexion;
        $query = "SELECT * FROM gimnasios WHERE clave_clientes = '$obj->centro'";
        $resultado = $db->selectQuery($query);
        $bandera = 'false';
        //var_dump($resultado);
        if($resultado->num_rows == 1){

            $bandera = 'true';
            while($row = mysqli_fetch_array($resultado)) 
            {
                $baseD = $row["BaseDatos"];
                $nombreGym = $row['NombreGimnasio'];
            }               
            
        }else{
            $msg = "Verifica tus datos!";
        }

        //$array = ["centro"=>$msg, "usuario"=>"direct18_".$baseD,"passwd" => $query];direct18_PG
        $array[] = ["centro"=>$msg, "usuario"=>"direct18_".$baseD ,"passwd" => $query,"nombreGym" => $nombreGym]; 
        echo json_encode($array);
        break;

    case '1':
        $msg;

        $conn = new conexion_sec("directoriofitness.com.mx","direct18","L4LL4v3M@3str@",$obj->centro);
        //$conn = new mysqli("directoriofitness.com.mx","direct18","L4LL4v3M@3str@",$obj->centro);
        
        $query = "SELECT * FROM socios WHERE idSocio = '$obj->usuario'";
        $resultado = $conn->selectQuery($query,"directoriofitness.com.mx","direct18","L4LL4v3M@3str@",$obj->centro);
        $foto;
        $nombre;
        if($resultado->num_rows == 1){
            while($row = mysqli_fetch_array($resultado)) 
            {
               $passwd = $row["Contrasena"];
               $nombre = $row["Nombre"]." ".$row["Apellidos"];
               $foto = $row["Foto"];
            }
            
            if(password_verify($obj->passwd, $passwd)){
                $msg = "login";
            }else{
                $msg = "Verifica Tus Datos!";
            }
        }

        $array = ["centro"=>$msg, "usuario"=>$nombre, "passwd" => $obj->passwd];
        echo json_encode($array);
        //$conn->disconnect("directoriofitness.com.mx","direct18","L4LL4v3M@3str@","direct18_PG");
        break;
    case '2':
        $msg;

        $conn = new conexion_sec("directoriofitness.com.mx","direct18","L4LL4v3M@3str@",$obj->centro);
        //$conn = new mysqli("directoriofitness.com.mx","direct18","L4LL4v3M@3str@",$obj->centro);
        
        $query = "SELECT * FROM socios WHERE idSocio = '$obj->usuario'";
        $resultado = $conn->selectQuery($query,"directoriofitness.com.mx","direct18","L4LL4v3M@3str@",$obj->centro);
        $foto;
        $nombre;
        $activo;
        $sucursal;
        if($resultado->num_rows == 1){
            while($row = mysqli_fetch_array($resultado)) 
            {
               $passwd = $row["Contrasena"];
               $nombre = $row["Nombre"]." ".$row["Apellidos"];
               $foto = $row["Foto"];
               $activo = $row["Activo"];
               $sucursal = $row["sucursal_idSucursal"];
            }
            
            if(password_verify($obj->passwd, $passwd)){
                $msg = "login";
            }else{
                $msg = "Verifica Tus Datos!";
            }
        }
        if($foto == null){
            $foto = 'sin foto';
        }

        $array[] = ["centro"=>$msg, "usuario"=>$nombre, "passwd" => $obj->passwd, "foto" => $foto, "activo" => $activo, "sucursal"=> $sucursal];
        echo json_encode($array);
        //$conn->disconnect("directoriofitness.com.mx","direct18","L4LL4v3M@3str@","direct18_PG");
        break;
}
$db->disconnect();//Close connection



?>
