<?php if (Session::get("tipo_usuario") != "1" and Session::get("tipo_usuario") != "2" and Session::get("tipo_usuario") != "6"){ 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" style="width: 25%;">Valor</th>
					<th class="align-middle text-center" style="width: 30%;">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: 20%;">Situação</th>
					<th class="align-middle text-center" style="width: 5%;">Opção</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 
						WHERE 
							id_usuario = :id AND 
							(
								valor_saldo LIKE :pesquisa OR 
								forma_pagamento LIKE :pesquisa OR 
								codigo_transacao LIKE :pesquisa OR 
								data_hora_solicitacao_saldo LIKE :pesquisa
							) 
						ORDER BY id_solicitacao_saldo DESC 
						LIMIT :maximo OFFSET :inicio
					", [
						":id" => Session::get("id_usuario"), 
						":pesquisa" => "%".$pesquisa."%", 
						":maximo" => $maximo, 
						":inicio" => $inicio
					]);
					

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

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

				?>

					<tr>
						<td class="align-middle"><?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">
							<?php if ($data->status_solicitacao_saldo == "S"): ?>
								<span class="badge badge-success">
									Confirmado
								</span>
							<?php elseif ($data->status_solicitacao_saldo == "N"): ?>
								<span class="badge badge-danger">
									Cancelado
								</span>
							<?php elseif ($data->status_solicitacao_saldo == "E"): ?>
								<span class="badge badge-secondary">
									Aguardando Avaliação
								</span>
							<?php endif; ?>
						</td>
						<td class="align-middle text-center">
							<?php if ($data->status_solicitacao_saldo == "E" and $data->forma_pagamento == "PagSeguro"): ?>
								<a href="<?php echo $data->link_pagamento; ?>" class="btn btn-info btn-square" target="_blank" data-toggle="tooltip" data-placement="bottom" title="Link de Pagamento">
									<i class="fa-regular fa-link"></i>
								</a>
							<?php else: ?>
								<button class="btn btn-info btn-square" disabled>
									<i class="fa-regular fa-link"></i>
								</button>
							<?php endif; ?>
						</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 
				WHERE 
					id_usuario = :id AND 
					(
						valor_saldo LIKE :pesquisa OR 
						forma_pagamento LIKE :pesquisa OR 
						codigo_transacao LIKE :pesquisa OR 
						data_hora_solicitacao_saldo 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>

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