<?php
    header("Access-Control-Allow-Origin: *");
    header("Content-Type: application/json; charset=UTF-8");
    
	include_once '../class/auth.php';
    include_once '../config/database.php';
    include_once '../class/corretores.php';
	
	
	
    $database = new Database();
    $db = $database->getConnection();
 
 
	//autenticação
	$token = null;
	$headers = apache_request_headers();
	if(isset($headers['Authorization'])){
		$matches = array();
		$token = $headers['Authorization'];
  
		$item = new userGerencieImobiliario($db);
		$item->token = $token;
		$item->getUserToken();
		if($item->users_id != null){
			//$emp_arr = array("usuarioID" =>  $item->users_id);
			http_response_code(200);
			//setcookie('gerenciadorImobiliarioUsuarioID', $item->users_id, time() + (60 * 2), "/");
			$gerenciadorImobiliarioUsuarioID = $item->users_id;
			//echo json_encode($emp_arr);
		}
		else
		{
			http_response_code(404);
			echo json_encode(
				array("status" => "error", "message" => "404 - N&atilde;o autorizado.")
			);
			die();
		}
	}
	else
	{
		http_response_code(401);
        echo json_encode(
            array("status" => "error", "message" => "401 - N&atilde;o autorizado.")
        );
		die();
	}
	


    $items = new Corretores($db);
	$items->gerenciadorImobiliarioUsuarioID = $gerenciadorImobiliarioUsuarioID;
    $stmt = $items->getCorretor();
	//ECHO $stmt;
    $itemCount = $stmt->rowCount();


    //echo json_encode($itemCount);

    if($itemCount > 0){
        
        $employeeArr = array();
        $employeeArr["itens"] = array();
        $employeeArr["totalItens"] = $itemCount;

        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            extract($row);

            $fotoPerfil = '';
            if(!empty($perfil)){
            	$fotoPerfil = 'https://meusistemas.com.br/gerenciador-imobiliario/assets/img/perfils/'.$perfil;
            }
            $e = array(
				'id' 		=> $id,
				'nome' 		=> $nome,
				'sobrenome' => $sobrenome,
				'email' 	=> $email,
				'telefone'  => $telefone,
				'perfil'  	=> $fotoPerfil,
				'id_imobiliaria' => $id_imobiliaria
            );

            array_push($employeeArr["itens"], $e);
        }
        echo json_encode($employeeArr);
    }

    else{
        http_response_code(404);
        echo json_encode(
            array("status" => "error", "message" => "Nenhum resultado encontrado.")
        );
    }
?>