<?php if (Session::get("tipo_usuario") != "4"){ show_403();} ?>
<?php import_file(DIR_TEMPLATES."header.php", ["title" => "Solicitações de Saldo"]); ?>
<?php import_file(DIR_MODELS."Model.class.php"); ?>

<h4 class="mb-3">
	<i class="fa-regular fa-money-check-edit-alt"></i> Solicitações de Saldo
</h4>

<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 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-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: 30%;">Usuário</th>
					<th class="align-middle text-center" style="width: 20%;">Valor</th>
					<th class="align-middle text-center" style="width: 20%;">Forma de Pagamento</th>
					<th class="align-middle text-center" style="width: 20%;">Data da Solicitação</th>
					<th class="align-middle text-center" style="width: 10%;">Confirmar Pagamento</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 solicitacao_saldo a
						INNER JOIN usuario b ON (a.id_usuario = b.id_usuario)
						WHERE 
							a.status_solicitacao_saldo = 'E' AND 
							a.forma_pagamento = 'Transferência Bancária' AND
							(
								a.valor_saldo LIKE :pesquisa OR 
								a.forma_pagamento LIKE :pesquisa OR 
								a.codigo_transacao LIKE :pesquisa OR 
								a.data_hora_solicitacao_saldo LIKE :pesquisa OR
								b.id_usuario LIKE :pesquisa OR
								b.nome_usuario LIKE :pesquisa
							) 
						ORDER BY id_solicitacao_saldo DESC 
						LIMIT :maximo OFFSET :inicio
					", [ 
						":pesquisa" => "%".$pesquisa."%", 
						":maximo" => $maximo, 
						":inicio" => $inicio
					]);
					

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

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

				?>

					<tr>
						<td class="align-middle text-left">
							<?php 

								echo "
									{$data->nome_usuario}<br>
									<small>{$data->cpf_usuario}</small><br>
									<small>{$data->email_usuario} | {$data->telefone_usuario}</small>
								"; 

							?>
						</td>
						<td class="align-middle text-center"><?php echo money_mask_BR($data->valor_saldo); ?></td>
						<td class="align-middle text-center"><?php echo $data->forma_pagamento; ?></td>
						<td class="align-middle text-center"><?php echo convert_date($data->data_hora_solicitacao_saldo); ?></td>
						<td class="align-middle text-center">
							<div class="d-inline-flex">
								<div class="mx-2">
									<button type="button" class="btn btn-success btn-square btn-avaliar" data-id="<?php echo encrypt_encode($data->id_solicitacao_saldo); ?>" data-status="S" data-toggle="tooltip" data-placement="bottom" title="Sim">
										<i class="fa-regular fa-check"></i>
									</button>
								</div>
								<div class="mx-auto">
									<button type="button" class="btn btn-danger btn-square btn-avaliar" data-id="<?php echo encrypt_encode($data->id_solicitacao_saldo); ?>" data-status="N" data-toggle="tooltip" data-placement="bottom" title="Não">
										<i class="fa-regular fa-times"></i>
									</button>
								</div>
							</div>
						</td>
					</tr>	
				<?php endforeach; ?>
			</tbody>
		</table>
	</div>

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


			$result = $model->query("
				SELECT * FROM solicitacao_saldo a
				INNER JOIN usuario b ON (a.id_usuario = b.id_usuario)
				WHERE 
					a.status_solicitacao_saldo = 'E' AND 
					a.forma_pagamento = 'Transferência Bancária' AND
					(
						a.valor_saldo LIKE :pesquisa OR 
						a.forma_pagamento LIKE :pesquisa OR 
						a.codigo_transacao LIKE :pesquisa OR 
						a.data_hora_solicitacao_saldo LIKE :pesquisa OR
						b.id_usuario LIKE :pesquisa OR
						b.nome_usuario LIKE :pesquisa
					) 
				ORDER BY id_solicitacao_saldo DESC
			", [
				":id" => Session::get("id_usuario"), 
				":pesquisa" => "%".$pesquisa."%"
			]);
			
			$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."&" : "").'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 Confirmar/Não Saldo -->
<div class="modal fade" id="modal-solicitacao-saldo" 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"></p>
			</div>
			<div class="modal-footer">
				<form action="<?php echo show_link("/avaliar-solicitacao-saldo/"); ?>" method="post">
					<input type="hidden" name="id_solicitacao_saldo" id="input-solicitacao-saldo-id" class="d-none" required>
					<input type="hidden" name="status_solicitacao_saldo" id="input-solicitacao-saldo-status" 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-avaliar").click(function(){
		var id = $(this).attr("data-id");
		var status = $(this).attr("data-status");

		$("#input-solicitacao-saldo-id").val(id);
		$("#input-solicitacao-saldo-status").val(status);

		if (status == "S") {
			var info = "<strong class='text-success'>CONFIRMAR</strong>";
		}
		else{
			var info = "<strong class='text-danger'>CANCELAR</strong>";
		}

		$("#modal-text").html("Tem certeza que deseja "+info+" essa Solicitação de Saldo?");
		$("#modal-solicitacao-saldo").modal("show");
	});
</script>


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