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

<div class="row align-items-center mb-3">
	<div class="col-6">
		<h4 class="mb-0">
			<i class="fa-regular fa-file-alt"></i> Páginas
		</h4>
	</div>
	<div class="col-6 text-right">
		<a href="<?php echo show_link("/cadastro-pagina/"); ?>" class="btn btn-primary">
			<i class="fa-regular fa-plus"></i> Adicionar
		</a>
	</div>
</div>

<div class="content-box">
	<div class="form-row mb-3">
		<div class="col-xl-8 col-lg-8 col-md-6 col-sm-12"></div>
		<div class="col-xl-4 col-lg-4 col-md-6 col-sm-12">
			<form action="" method="get">
				<div class="input-group">
					<input type="text" name="pesquisa" class="form-control" autocomplete="off" value="<?php echo filter_input(INPUT_GET, "pesquisa", FILTER_SANITIZE_STRING); ?>">
					<div class="input-group-append">
						<button class="btn btn-primary">
							<i class="fa-regular fa-search"></i>
						</button>
					</div>
				</div>
			</form>
		</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: 65%;">Título</th>
					<th class="align-middle text-center" style="width: 30%;">Local</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;
					}

					$pesquisa = filter_input(INPUT_GET, "pesquisa", FILTER_SANITIZE_STRING);

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

					$busca = $model->query("
						SELECT * FROM pagina 
						WHERE 
						(
							titulo_pagina LIKE :pesquisa OR
							local_pagina LIKE :pesquisa 
						) 
						ORDER BY 
							id_pagina DESC 
						LIMIT 
							:maximo OFFSET :inicio 
					", [
						":pesquisa" => "%".$pesquisa."%", 
						":maximo" => $maximo, 
						":inicio" => $inicio
					]);

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

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

				?>

					<tr>
						<td class="align-middle text-left"><?php echo $dados->titulo_pagina; ?></td>
						<td class="align-middle text-center"><?php echo $dados->local_pagina; ?></td>
						<td class="align-middle text-center">
							<div class="d-inline-flex">
								<div class="mx-2">
									<form action="<?php echo show_link("/edicao-pagina/"); ?>" method="post">
										<input type="hidden" name="id_pagina" class="d-none" value="<?php echo encrypt_encode($dados->id_pagina); ?>">
										<button type="submit" class="btn btn-warning btn-square" data-toggle="tooltip" data-placement="bottom" title="Editar">
											<i class="fa-regular fa-pencil-alt"></i>
										</button>
									</form>
								</div>
							
								<div class="mx-auto">
									<button type="button" class="btn btn-danger btn-square btn-excluir" data-value="<?php echo encrypt_encode($dados->id_pagina); ?>" 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 pagina 
				WHERE 
					(
						titulo_pagina LIKE :pesquisa OR 
						local_pagina LIKE :pesquisa 
					) 
				ORDER BY 
					id_pagina DESC 
			", [
				":pesquisa" => "%".$pesquisa."%"
			]);

			$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>
</div>


<!-- Modal Mudar Status pagina -->
<div class="modal fade" id="modal-excluir-pagina" 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>
					Tem certeza que deseja <strong class='text-danger'>EXCLUIR</strong> esse Página?
				</p>
			</div>
			<div class="modal-footer">
				<form action="<?php echo show_link("/excluir-pagina/"); ?>" method="post">
					<input type="hidden" name="id_pagina" id="input-pagina-id" class="d-none" required>

					<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>
	$(".btn-excluir").click(function(){
		var id = $(this).data("value");

		$("#input-pagina-id").val(id);
		$("#modal-excluir-pagina").modal("show");
	});
</script>

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