<?php if (Session::get("tipo_usuario") != "1"){ show_403();} ?>
<?php import_file(DIR_TEMPLATES."header.php", ["title" => "Cadastrar Banco", "active_page" => "bancos"]); ?>

<h4 class="mb-3">
	<i class="fa-regular fa-plus"></i> Cadastrar Banco
</h4>

<div class="content-box">
	<form id="form-banco">
		<div class="form-row">
			<div class="col-xl-6 col-lg-6 col-md-6 col-sm-12">
				<div class="form-group">
					<label for="codigo-banco">Código<span class="text-danger">*</span></label>
					<input type="text" name="codigo_banco" id="codigo-banco" class="form-control" maxlength="20" required>
				</div>
			</div>
			<div class="col-xl-6 col-lg-6 col-md-6 col-sm-12">
				<div class="form-group">
					<label for="nome-banco">Nome<span class="text-danger">*</span></label>
					<input type="text" name="nome_banco" id="nome-banco" class="form-control" maxlength="120" required>
				</div>
			</div>
		</div>
		
		<button type="submit" class="btn btn-primary">
			<i class="fa-regular fa-save"></i> Salvar
		</button>

		<a href="<?php echo show_link("/bancos/"); ?>" class="btn btn-secondary">
			<i class="fa-regular fa-arrow-left"></i> Voltar
		</a>
	</form>
</div>

<script>
	$("#form-banco").submit(function(event){
		event.preventDefault();
		var form = $(this);
		$.ajax({
			type: "POST",
			data: form.serialize(),
			url: "<?php echo show_link("/cadastrar-banco/"); ?>",
			async: true
		}).then(function(response){
			var result = $.parseJSON(response);
			if (result.status == true) {
				$.toast({
					heading: "Aviso",
					text: "Cadastro Realizado com Sucesso!",
					icon: "success",
					loader: true
				});

				form.each(function(){
					this.reset();
				});
			}
			else if (result.status == false && result.motivo == "codigo") {
				$("#codigo-banco").addClass("is-warning");
				$.toast({
					heading: "Aviso",
					text: "Um banco com esse código já foi cadastrado no sistema!",
					icon: "warning",
					loader: true
				});
				$("#codigo-banco").keyup(function(){
					$(this).removeClass("is-warning");
					$(this).off("keyup");
				});
			}
			else{
				$.toast({
					heading: "Aviso",
					text: "Erro ao Realizar o Cadastro!",
					icon: "error",
					loader: true
				});
			}
		}, function(error){
			console.log("Erro: "+error);
		});
	});
</script>

<?php import_file(DIR_TEMPLATES."footer.php"); ?>