<?php 

	if (Session::get("tipo_usuario") != "1"){ show_403();}
	if ($_SERVER["REQUEST_METHOD"] == "POST") {
		import_file(DIR_MODELS."Model.class.php");

		$id_compra = (int) encrypt_decode(filter_input(INPUT_POST, "id_compra", FILTER_SANITIZE_STRING));

		$model = new Model();

		$result = $model->query("
			SELECT * FROM compra a
			INNER JOIN produto b ON (a.id_produto = b.id_produto) 
			INNER JOIN usuario c ON (a.id_usuario = c.id_usuario) 
			INNER JOIN distribuicao d ON (a.id_compra = d.id_compra) 
			WHERE 
				a.id_compra = :id
		", [
			":id" => $id_compra
		]);

		if ($result->rowCount() > 0) {
			$data = ["status" => true];

			foreach ($result->fetchAll() as $key => $value) {
				foreach ($value as $k => $v) {
					$data["data"][$key][$k] = $v;
				}
			}

			echo json_encode($data);
		}
		else{
			echo json_encode(["status" => false]);
		}
	}
	else{
		show_403();
	}

?>