<?php
    header("Access-Control-Allow-Origin: *");
    header("Content-Type: application/json; charset=UTF-8");
    header("Access-Control-Allow-Methods: POST");
    header("Access-Control-Max-Age: 3600");
    header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);


	include_once '../class/auth.php';
    include_once '../config/database.php';
	include_once '../config/funcoes.php';
    include_once '../class/imoveis.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();
	}
	
 
    $item = new Imoveis($db);
    $data = json_decode(file_get_contents("php://input"));
	
	
	///trata dados do post
		//$item->id = $dataRow['id'];
		
		//print_r($data);exit;
		foreach($data as $keys => $values)
		{
			//echo $keys.' - '.$values.'<br>';
			$item->$keys = stringInputCleaner($values);
		}
		$item->imobiliariaID = $gerenciadorImobiliarioUsuarioID;
	///trata dados do post

	$imovelRetorno = $item->cadastraImovel();
    if(!empty($imovelRetorno)){
        //cadastro realizado
		http_response_code(200);
		echo json_encode(
			array("imovelID" => $imovelRetorno)
		);
		die();
    } else{
		http_response_code(500);
		echo json_encode(
			array("status" => "error", "message" => "500 - Houve um erro no cadastro.")
		);
		die();
    }


