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

<h4 class="mb-3">
	<i class="fa-regular fa-plus"></i> Cadastrar Categoria da Loja
</h4>

<div class="content-box">
	<form id="form-categoria-loja">
		<div class="form-row">
			<div class="col-md-6">
				<div class="form-group">
					<label for="nome-categoria-loja">Nome<span class="text-danger">*</span></label>
					<input type="text" name="nome_categoria_loja" id="nome-categoria-loja" class="form-control" maxlength="120" required>
				</div>
			</div>
			<div class="col-md-6">
				<div class="form-group">
					<label for="icone-categoria-loja">Ícone<span class="text-danger">*</span></label>
					<input type="text" name="icone_categoria_loja" id="icone-categoria-loja" class="form-control" maxlength="60" placeholder="Exemplo: fa-solid fa-home" required>
					<span class="form-text">Para consultar os icones <a href="https://fontawesome.com/v6/search" target="_blank">Clique aqui</a></span>
				</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("/categorias-lojas/"); ?>" class="btn btn-secondary">
			<i class="fa-regular fa-arrow-left"></i> Voltar
		</a>
	</form>
</div>

<script>
	$("#form-categoria-loja").submit(function(event){
		event.preventDefault();
		var form = $(this);
		$.ajax({
			type: "POST",
			data: form.serialize(),
			url: "<?php echo show_link("/cadastrar-categoria-loja/"); ?>",
			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{
				$.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"); ?>