<?php if (Session::get("tipo_usuario") != "4"){ show_403();} ?>
<?php import_file(DIR_TEMPLATES."header.php", ["title" => "Usuários Inativos"]); ?>
<?php import_file(DIR_MODELS."Model.class.php"); ?>

<h4><i class="fa-solid fa-users"></i> Usuários Inativos</h4>

<div class="card my-3">
	<div class="card-header">
		<div class="form-row align-items-center">
			<div class="col-xl-8 col-lg-8 col-md-6 col-sm-12">
				<div class="card-title h5 mb-0">
					Todos os Usuários Inativos
				</div>
			</div>
			<div class="col-xl-4 col-lg-4 col-md-6 col-sm-12">
				<form action="" method="get">
					<div class="input-group my-1">
						<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 fa-search"></i></button>
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>
	<div class="card-body">
		<div class="table-responsive">
			<table class="table table-sm table-hover table-striped">
				<thead class="thead-inverse">
					<tr>
						<th class="align-middle text-center">Nº</th>
						<th class="align-middle text-center">Nome</th>
						<th class="align-middle text-center">E-Mail</th>
						<th class="align-middle text-center">CPF</th>
						<th class="align-middle text-center">Data do Cadastro</th>
						<th class="align-middle text-center">Link de Pagamento</th>
						<th class="align-middle text-center">Cód. Transação</th>
						<th class="align-middle text-center">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);

						$result = $model->query("SELECT * FROM usuario WHERE status_usuario = 'S' AND usuario_ativo = 'N' AND (nome_usuario LIKE :pesquisa OR email_usuario LIKE :pesquisa OR cpf_usuario LIKE :pesquisa OR datahora_cadastro_usuario LIKE :pesquisa) ORDER BY id_usuario DESC LIMIT :maximo OFFSET :inicio", [":pesquisa" => "%".$pesquisa."%", ":maximo" => $maximo, ":inicio" => $inicio]);

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

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

					?>

						<tr>
							<td class="align-middle text-center"><?php echo $n++; ?></td>
							<td class="align-middle text-center"><?php echo $dados->nome_usuario; ?></td>
							<td class="align-middle text-center"><?php echo $dados->email_usuario; ?></td>
							<td class="align-middle text-center"><?php echo $dados->cpf_usuario; ?></td>
							<td class="align-middle text-center"><?php echo convert_date($dados->datahora_cadastro_usuario); ?></td>
							<td class="align-middle text-center"><?php echo $dados->link_pagamento_adesao; ?></td>
							<td class="align-middle text-center"><?php echo $dados->codigo_transacao_adesao; ?></td>
							<td class="align-middle text-center">
								<div class="d-inline-flex">
									<div class="mx-2">
										<button type="button" class="btn btn-success btn-block btn-ativar" data-value="<?php echo encrypt_encode($dados->id_usuario); ?>" data-toggle="tooltip" data-placement="bottom" title="Ativar">
											<i class="fa-solid fa-check"></i>
										</button>
									</div>
								
									<div class="mx-auto">
										<button type="button" class="btn btn-danger btn-block btn-excluir" data-value="<?php echo encrypt_encode($dados->id_usuario); ?>" data-toggle="tooltip" data-placement="bottom" title="Excluir">
											<i class="fa-solid fa-trash-alt"></i>
										</button>
									</div>
								</div>
							</td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>

			<?php 

				$result = $model->query("SELECT * FROM usuario WHERE status_usuario = 'S' AND usuario_ativo = 'N' AND (nome_usuario LIKE :pesquisa OR email_usuario LIKE :pesquisa OR cpf_usuario LIKE :pesquisa OR datahora_cadastro_usuario LIKE :pesquisa) ORDER BY id_usuario DESC", [":pesquisa" => "%".$pesquisa."%"]);

				$total_usuarios  = $result->rowCount();

			?>

			<p class="mb-0 text-center">Total de Usuários encontrados: <?php echo $total_usuarios; ?></p>
		</div>
	</div>
	<div class="card-footer mb-0">
		<ul class="pagination justify-content-center" style="margin-bottom: -15px;">
			<?php

				$total = $result->rowCount();

				$total_paginas = ceil($total/$maximo);
							   
				if ($pagina <= $total_paginas and $pagina > 1) {
	 				echo '<li class="page-item"><a href="?'.(!empty($pesquisa) ? "pesquisa=".$pesquisa."&" : "").(!empty($filtro) ? "filtro=".$filtro."&" : "").'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."&" : "").(!empty($filtro) ? "filtro=".$filtro."&" : "").'pag='.($pagina+1).'" class="page-link"><span aria-hidden="true">&raquo;</span></a></li> ';
				}
				
			?>
		</ul>
	</div>
</div>

<!-- Modal Ativar Usuário -->
<div class="modal fade" id="modal-ativar-usuario" 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-solid fa-exclamation-triangle"></i> Aviso</h5>
			</div>
			<div class="modal-body">
				<p id="modal-text">Tem certeza que deseja <strong class="text-success">ATIVAR</strong> esse Usuário?</p>
			</div>
			<div class="modal-footer">
				<form action="<?php echo show_link("/ativar-usuario/"); ?>" method="post">
					<input type="hidden" name="id_usuario" id="input-usuario-id" class="d-none" required>
					<button type="submit" class="btn btn-white"><i class="fa-solid fa-check"></i> Sim</button>
					<button type="button" class="btn btn-white" data-dismiss="modal"><i class="fa-solid fa-times"></i> Não</button>
				</form>
			</div>
		</div>
	</div>
</div>


<!-- Modal Excluir Usuário -->
<div class="modal fade" id="modal-excluir-usuario" 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-solid 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> esse Usuário?</p>
			</div>
			<div class="modal-footer">
				<form action="<?php echo show_link("/excluir-definitivamente-usuario/"); ?>" method="post">
					<input type="hidden" name="id_usuario" id="input-usuario-id" class="d-none" required>
					<button type="submit" class="btn btn-white"><i class="fa-solid fa-check"></i> Sim</button>
					<button type="button" class="btn btn-white" data-dismiss="modal"><i class="fa-solid fa-times"></i> Não</button>
				</form>
			</div>
		</div>
	</div>
</div>

<script>
	$(".btn-ativar").click(function(){
		var id = $(this).attr("data-value");

		$("#input-usuario-id").val(id);
		$("#modal-ativar-usuario").modal("show");
	});

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

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

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