<?php 

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

		$id_carrinho_compra = (int) filter_input(INPUT_POST, "id_carrinho_compra", FILTER_SANITIZE_STRING);

		$model = new Model();

		if (!empty($id_carrinho_compra)) {
			$result = $model->delete("carrinho_compra", [
				"id_carrinho_compra" => $id_carrinho_compra
			]);

			if ($result > 0) {
				$last_query = $model->lastQuery();
				$model->insert("historico", [
					"comando_sql" => $last_query,
					"datahora_acao" => date("Y-m-d H:i:s"),
					"id_usuario" => Session::get("id_usuario")
				]);

				$model->commit();
				echo "<script>window.location.href='".show_link("/meu-carrinho-compra/?remover=true")."'</script>";
			}
			else{
				$model->rollback();
				echo "<script>window.location.href='".show_link("/meu-carrinho-compra/?remover=false")."'</script>";
			}
		}
		else{
			echo "<script>window.location.href='".show_link("/meu-carrinho-compra/?remover=false")."'</script>";
		}
	}
	else{
		show_403();
	}

?>