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

<?php 

	$id_categoria = (int) encrypt_decode(filter_input(INPUT_GET, "categoria", FILTER_SANITIZE_STRING));

	$model = new Model();

	$result = $model->query("
		SELECT * FROM categoria 
		WHERE 
			id_categoria = :id AND 
			status_categoria = 'S'
	", [
		":id" => $id_categoria
	]);

	$dados = $result->fetch();

?>

<h4 class="mb-3">
	<i class="fa-regular fa-th-list"></i> Visualizar Categoria
</h4>

<div class="content-box mb-3">
	<h5 class="mb-3">Categoria</h5>

	<table class="table table-bordered table-striped">
		<tr>
			<th class="align-middle">Nome:</th>
		</tr>
		<tr>
			<td class="align-middle">
				<?php echo $dados->nome_categoria; ?>
			</td>
		</tr>
		<tr>
			<th class="align-middle">Ícone:</th>
		</tr>
		<tr>
			<td class="align-middle">
				<i class="<?php echo $dados->icone_categoria; ?>" style="font-size: 23px;"></i>
			</td>
		</tr>
	</table>
</div>

<div class="content-box">
	<div class="row align-items-center mb-3">
		<div class="col-md-6">
			<h5 class="mb-0">Subcategoria</h5>
		</div>
		<div class="col-md-6 text-right">
			<button type="button" id="adicionar-subcategoria" class="btn btn-primary">
				<i class="fa-regular fa-plus"></i> Adicionar
			</button>
		</div>
	</div>

	<div class="table-responsive">
		<table class="table table-sm table-hover table-striped">
			<thead class="thead-inverse">
				<tr>
					<th class="align-middle text-left" style="width: 95%;">Nome</th>
					<th class="align-middle text-center" style="width: 5%;">Opções</th>
				</tr>
			</thead>
			<tbody>
				<?php 

					$model = new Model();

					$maximo = 15;

					if (isset($_GET["pag"])) {
						$pagina = filter_input(INPUT_GET, "pag", FILTER_SANITIZE_NUMBER_INT);
					}
					else{
						$pagina = 1;
					}

					$inicio = (($maximo*$pagina)-$maximo);

					$busca = $model->query("
						SELECT * FROM subcategoria 
						WHERE 
							status_subcategoria = 'S' AND 
							id_categoria = :id_categoria
						ORDER BY 
							nome_subcategoria DESC 
						LIMIT 
							:maximo OFFSET :inicio
					", [
						":id_categoria" => $id_categoria, 
						":maximo" => $maximo, 
						":inicio" => $inicio
					]);

					$n = ($pagina*$maximo)-$maximo + 1;

					foreach ($busca->fetchAll() as $dados):

				?>

					<tr>
						<td class="align-middle text-left"><?php echo $dados->nome_subcategoria; ?></td>
						
						<td class="align-middle text-center">
							<div class="d-inline-flex">
								<div class="mx-2">
									<button type="submit" class="btn btn-warning btn-square btn-editar" data-value="<?php echo encrypt_encode($dados->id_subcategoria); ?>" data-toggle="tooltip" data-placement="bottom" title="Editar">
										<i class="fa-regular fa-pencil-alt"></i>
									</button>
								</div>
							
								<div class="mx-auto">
									<button type="button" class="btn btn-danger btn-square btn-excluir" data-value="<?php echo encrypt_encode($dados->id_subcategoria); ?>" data-status="N" data-toggle="tooltip" data-placement="bottom" title="Excluir">
										<i class="fa-regular fa-trash-alt"></i>
									</button>
								</div>
							</div>
						</td>
					</tr>	
				<?php endforeach; ?>
			</tbody>
		</table>
	</div>

	<ul class="pagination justify-content-center" style="margin-bottom: -15px;">
		<?php 

			$busca = $model->query("
				SELECT * FROM subcategoria 
				WHERE 
					status_subcategoria = 'S' AND 
					id_categoria = :id_categoria
			", [
				":id_categoria" => $id_categoria
			]);

			$total  = $busca->rowCount();

			$total_paginas = ceil($total/$maximo);
						   
			if ($pagina <= $total_paginas and $pagina > 1) {
 				echo '<li class="page-item"><a href="?'.(!empty($pesquisa) ? "pesquisa=".$pesquisa."&" : "").'pag='.($pagina-1).'" class="page-link"><span aria-hidden="true">&laquo;</span></a> </li>';
			}

			echo ' <li class="page-item active"><p class="page-link">'.$pagina.'</p></li> ';

			if ($pagina < $total_paginas) {	
				echo '<li class="page-item"><a href="?'.(!empty($pesquisa) ? "pesquisa=".$pesquisa."&" : "").'pag='.($pagina+1).'" class="page-link"><span aria-hidden="true">&raquo;</span></a></li> ';
			}
			
		?>
	</ul>

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

<!-- Modal Cadastrar Subcategoria -->
<div class="modal fade" id="modal-cadastrar-subcategoria" tabindex="-1" role="dialog" aria-hidden="true">
	<div class="modal-dialog modal-lg" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<h5 class="modal-title">
					<i class="fa-regular fa-plus"></i> Cadastrar Subcategoria
				</h5>
			</div>
			<form action="<?php echo show_link("/cadastrar-subcategoria/"); ?>" method="post">
				<div class="modal-body">
					<div class="form-group">
						<label for="nome-subcategoria">Nome<span class="text-danger">*</span></label>
						<input type="text" name="nome_subcategoria" id="nome-subcategoria" class="form-control" required>
					</div>
				</div>
				<div class="modal-footer">
					<input type="hidden" name="id_categoria" value="<?php echo $id_categoria; ?>">

					<button type="submit" class="btn btn-primary">
						<i class="fa-regular fa-save"></i> Salvar
					</button>
					<button type="button" class="btn btn-secondary" data-dismiss="modal">
						<i class="fa-regular fa-times"></i> Fechar
					</button>
				</div>
			</form>
		</div>
	</div>
</div>

<!-- Modal Editar Subcategoria -->
<div class="modal fade" id="modal-editar-subcategoria" tabindex="-1" role="dialog" aria-hidden="true">
	<div class="modal-dialog modal-lg" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<h5 class="modal-title">
					<i class="fa-regular fa-edit"></i> Editar Subcategoria
				</h5>
			</div>
			<form action="<?php echo show_link("/editar-subcategoria/"); ?>" method="post">
				<div class="modal-body">
					<div class="form-group">
						<label for="nome-subcategoria-2">Nome<span class="text-danger">*</span></label>
						<input type="text" name="nome_subcategoria" id="nome-subcategoria-2" class="form-control" required>
					</div>
				</div>
				<div class="modal-footer">
					<input type="hidden" name="id_subcategoria" id="id-subcategoria">
					<input type="hidden" name="id_categoria" value="<?php echo $id_categoria; ?>">

					<button type="submit" class="btn btn-primary">
						<i class="fa-regular fa-save"></i> Salvar
					</button>
					<button type="button" class="btn btn-secondary" data-dismiss="modal">
						<i class="fa-regular fa-times"></i> Fechar
					</button>
				</div>
			</form>
		</div>
	</div>
</div>

<!-- Modal Excluir Subcategoria -->
<div class="modal fade" id="modal-excluir-subcategoria" tabindex="-1" role="dialog" aria-hidden="true">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<h5 class="modal-title"><i class="fa-regular fa-exclamation-triangle"></i> Aviso</h5>
			</div>
			<div class="modal-body">
				<p id="modal-text">
					Tem certeza que deseja <strong class="text-danger">Excluir</strong> essa Subcategoria?
				</p>
			</div>
			<div class="modal-footer">
				<form action="<?php echo show_link("/excluir-subcategoria/"); ?>" method="post">
					<input type="hidden" name="id_subcategoria" id="input-subcategoria-id" class="d-none" required>
					<input type="hidden" name="id_categoria" value="<?php echo $id_categoria; ?>">

					<button type="submit" class="btn btn-white">
						<i class="fa-regular fa-check"></i> Sim
					</button>
					<button type="button" class="btn btn-white" data-dismiss="modal">
						<i class="fa-regular fa-times"></i> Não
					</button>
				</form>
			</div>
		</div>
	</div>
</div>

<script>
	$("#adicionar-subcategoria").click(function(){
		$("#modal-cadastrar-subcategoria").modal("show");
	});

	$(".btn-editar").click(function(){
		var id_subcategoria = $(this).data("value");

		$.ajax({
			type: "POST",
			data: {
				id_subcategoria: id_subcategoria
			},
			url: "<?php echo show_link("/obter-subcategoria/"); ?>",
			async: true
		}).then(function(response){
			var result = $.parseJSON(response);

			if (result.status == true) {
				var data = result.dados[0];

				$("#nome-subcategoria-2").val(data.nome_subcategoria);

				$("#id-subcategoria").val(id_subcategoria);
				$("#modal-editar-subcategoria").modal("show");
			}
			else{

			}
		}, function(error){
			console.log("Erro: "+error);
		});
	});

	$(".btn-excluir").click(function(){
		var id_subcategoria = $(this).data("value");

		$("#input-subcategoria-id").val(id_subcategoria);
		$("#modal-excluir-subcategoria").modal("show");
	});

	if ($_GET("cadastro_sub") == "true") {
		$.toast({
			heading: "Aviso",
			text: "Dados Cadastrados com Sucesso!",
			icon: "success",
			loader: true
		});
		window.history.pushState("", "", "<?php echo show_link("/visualizar-categoria/?categoria=".urlencode(encrypt_encode($id_categoria))); ?>");
	}
	else if ($_GET("cadastro_sub") == "false") {
		$.toast({
			heading: "Aviso",
			text: "Erro ao Cadastrar os Dados!",
			icon: "error",
			loader: true
		});
		window.history.pushState("", "", "<?php echo show_link("/visualizar-categoria/?categoria=".urlencode(encrypt_encode($id_categoria))); ?>");
	}

	if ($_GET("editar_sub") == "true") {
		$.toast({
			heading: "Aviso",
			text: "Dados Editados com Sucesso!",
			icon: "success",
			loader: true
		});
		window.history.pushState("", "", "<?php echo show_link("/visualizar-categoria/?categoria=".urlencode(encrypt_encode($id_categoria))); ?>");
	}
	else if ($_GET("editar_sub") == "false") {
		$.toast({
			heading: "Aviso",
			text: "Erro ao Editar os Dados!",
			icon: "error",
			loader: true
		});
		window.history.pushState("", "", "<?php echo show_link("/visualizar-categoria/?categoria=".urlencode(encrypt_encode($id_categoria))); ?>");
	}

	if ($_GET("excluir_sub") == "true") {
		$.toast({
			heading: "Aviso",
			text: "Dados Excluido com Sucesso!",
			icon: "success",
			loader: true
		});
		window.history.pushState("", "", "<?php echo show_link("/visualizar-categoria/?categoria=".urlencode(encrypt_encode($id_categoria))); ?>");
	}
	else if ($_GET("excluir_sub") == "false") {
		$.toast({
			heading: "Aviso",
			text: "Erro ao Excluir os Dados!",
			icon: "error",
			loader: true
		});
		window.history.pushState("", "", "<?php echo show_link("/visualizar-categoria/?categoria=".urlencode(encrypt_encode($id_categoria))); ?>");
	}
</script>

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