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

<h4 class="mb-3">
	<i class="fa-regular fa-sack-dollar"></i> Solicitações de Saque
</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">
					<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: 25%;">Usuário</th>
					<th class="align-middle text-left" style="width: 20%;">Conta Bancária</th>
					<th class="align-middle text-center" style="width: 15%;">Valor Solicidado</th>
					<th class="align-middle text-center" style="width: 10%;">Taxa</th>
					<th class="align-middle text-center" style="width: 15%;">Valor do Saque</th>
					<th class="align-middle text-center" style="width: 10%;">Data da Solicitação</th>
					<th class="align-middle text-center" colspan="2" style="width: 5%;">Cofirmar Saque</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 saque 
						INNER JOIN banco ON (banco.id_banco = saque.id_banco) 
						INNER JOIN usuario ON (usuario.id_usuario = saque.id_usuario) 
						WHERE 
							status_saque = 'E' AND 
							(
								valor_saque LIKE :pesquisa OR 
								valor_taxa_saque LIKE :pesquisa OR 
								datahora_solicitacao LIKE :data OR 
								nome_usuario LIKE :pesquisa OR 
								nome_banco LIKE :pesquisa OR 
								codigo_banco LIKE :pesquisa OR 
								numero_agencia_saque LIKE :pesquisa OR 
								numero_conta_saque LIKE :pesquisa OR 
								cpf_usuario LIKE :pesquisa
							) 
						ORDER BY id_saque DESC 
						LIMIT :maximo OFFSET :inicio
					", [
						":pesquisa" => "%".$pesquisa."%", 
						":data" => "%".convert_date($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->nome_usuario}<br>
									<small>{$dados->cpf_usuario}</small><br>
									<small>{$dados->email_usuario} | {$dados->telefone_usuario}</small>
								"; 

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

								echo "
									{$dados->codigo_banco} - {$dados->nome_banco}<br>
									Agência: {$dados->numero_agencia_saque}<br>
									Conta: {$dados->numero_conta_saque}<br>
									PIX: {$dados->chave_pix_saque}
								"; 

							?>	
						</td>
						<td class="align-middle text-center"><?php echo money_mask_BR($dados->valor_saque); ?></td>
						<td class="align-middle text-center"><?php echo money_mask_BR($dados->valor_taxa_saque); ?></td>
						<td class="align-middle text-center"><?php echo money_mask_BR($dados->valor_saque - $dados->valor_taxa_saque); ?></td>
						<td class="align-middle text-center"><?php echo convert_date($dados->datahora_solicitacao); ?></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($dados->id_saque); ?>" 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($dados->id_saque); ?>" 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 

			$busca = $model->query("
				SELECT * FROM saque 
				INNER JOIN banco ON (banco.id_banco = saque.id_banco) 
				INNER JOIN usuario ON (usuario.id_usuario = saque.id_usuario) 
				WHERE 
					status_saque = 'E' AND 
					(
						valor_saque LIKE :pesquisa OR 
						valor_taxa_saque LIKE :pesquisa OR 
						datahora_solicitacao LIKE :data OR 
						nome_usuario LIKE :pesquisa OR 
						nome_banco LIKE :pesquisa OR 
						codigo_banco LIKE :pesquisa OR 
						numero_agencia_saque LIKE :pesquisa OR 
						numero_conta_saque LIKE :pesquisa OR 
						cpf_usuario LIKE :pesquisa
					) 
				ORDER BY id_saque DESC
			", [
				":pesquisa" => "%".$pesquisa."%", 
				":data" => "%".convert_date($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>

<div class="modal fade" id="modal-saque" 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-saque/"); ?>" method="post">
					<input type="hidden" name="id_saque" id="input-saque-id" class="d-none" required>
					<input type="hidden" name="status_saque" id="input-saque-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-saque-id").val(id);
		$("#input-saque-status").val(status);

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

		$("#modal-text").html("Tem certeza que deseja "+info+" esse Saque?");
		$("#modal-saque").modal("show");
	});
</script>

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