<?php 

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

		$nome = filter_input(INPUT_POST, "nome_banco", FILTER_SANITIZE_STRING);
		$codigo = filter_input(INPUT_POST, "codigo_banco", FILTER_SANITIZE_STRING);
		$id = encrypt_decode(filter_input(INPUT_POST, "id_banco", FILTER_SANITIZE_STRING));

		$model = new Model();

		if ($model->issetElementInTable("banco", ["codigo_banco" => $codigo], ["id_banco" => $id])) {
			echo json_encode(["status" => false, "motivo" => "codigo"]);
			exit();
		}

		$result = $model->update("banco", [
			"nome_banco" => $nome,
			"codigo_banco" => $codigo
		], [
			"id_banco" => $id
		]);

		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 json_encode(["status" => true]);
		}
		else{
			$model->rollback();
			echo json_encode(["status" => false]);
		}
	}
	else{
		show_403();
	}

?>