<?php if (Session::get("tipo_usuario") != "1" and Session::get("tipo_usuario") != "2" and Session::get("tipo_usuario") != "6" or Session::get("usuario_ativo") != "S"){ show_403();} ?>
<?php import_file(DIR_TEMPLATES."header.php", ["title" => "Compra Lojas Físicas", "active_page" => "compras"]); ?>
<?php import_file(DIR_MODELS."Model.class.php"); ?>

<h4 class="mb-3">
	<i class="fa-regular fa-shopping-cart"></i> Compras Lojas Físicas
</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>
					<?php if (Session::get("tipo_usuario") == "1" or Session::get("tipo_usuario") == "6"): ?>
						<th class="align-middle" style="width: 30%;">Usuário</th>
					<?php endif; ?>
					<th class="align-middle">Loja</th>
					<th class="align-middle text-center">Valor</th>
					<?php if (Session::get("tipo_usuario") == "1"): ?>
					    <th class="align-middle text-center">Valor de Rede</th>
					<?php endif; ?>
					<th class="align-middle text-center">Valor de Cashback</th>
					<?php if (Session::get("tipo_usuario") == "1"): ?>
					    <th class="align-middle text-center">Valor de Comissão</th>
					<?php endif; ?>
					<th class="align-middle text-center">Data da Compra</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);
					
					$query = "";
					
					if (Session::get("tipo_usuario") == "2") {
					    $query .= " AND c.id_usuario = ".Session::get("id_usuario");
					}
					else if (Session::get("tipo_usuario") == "6") {
					    $query .= " AND b.id_usuario = ".Session::get("id_usuario");
					}

					$busca = $model->query("
						SELECT * FROM lancamento_compra a
						INNER JOIN loja b ON (a.id_loja = b.id_loja) 
						INNER JOIN usuario c ON (a.id_usuario = c.id_usuario) 
						WHERE 
							(
								b.nome_loja LIKE :pesquisa OR 
								a.valor_compra LIKE :pesquisa OR 
								a.valor_rede LIKE :pesquisa OR 
								a.valor_cashback LIKE :pesquisa OR 
								a.valor_comissao LIKE :pesquisa OR 
								a.data_hora_lancamento_compra LIKE :pesquisa OR
								c.nome_usuario LIKE :pesquisa OR
								c.cpf_usuario LIKE :pesquisa 
							) 
							$query
						ORDER BY 
						    id_lancamento_compra DESC 
						LIMIT 
						    :maximo OFFSET :inicio
					", [
						":pesquisa" => "%".$pesquisa."%", 
						":maximo" => $maximo, 
						":inicio" => $inicio
					]);

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

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

				?>

					<tr>
						<?php if (Session::get("tipo_usuario") == "1" or Session::get("tipo_usuario") == "6"): ?>
    						<td class="align-middle">
								<?php 

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

								?>
							</td>
    					<?php endif; ?>
						<td class="align-middle"><?php echo $dados->nome_loja; ?></td>
						<td class="align-middle text-center"><?php echo money_mask_BR($dados->valor_compra); ?></td>
						<?php if (Session::get("tipo_usuario") == "1"): ?>
						    <td class="align-middle text-center"><?php echo money_mask_BR($dados->valor_rede); ?></td>
						<?php endif; ?>
						<td class="align-middle text-center"><?php echo money_mask_BR($dados->valor_cashback); ?></td>
						<?php if (Session::get("tipo_usuario") == "1"): ?>
						    <td class="align-middle text-center"><?php echo money_mask_BR($dados->valor_comissao); ?></td>
						<?php endif; ?>
						<td class="align-middle text-center"><?php echo convert_date($dados->data_hora_lancamento_compra); ?></td>
					</tr>	
				<?php endforeach; ?>
			</tbody>
		</table>
	</div>

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

			$busca = $model->query("
    			SELECT * FROM lancamento_compra a
    			INNER JOIN loja b ON (a.id_loja = b.id_loja) 
    			INNER JOIN usuario c ON (a.id_usuario = c.id_usuario) 
    			WHERE 
    				(
    					b.nome_loja LIKE :pesquisa OR 
    					a.valor_compra LIKE :pesquisa OR 
    					a.valor_rede LIKE :pesquisa OR 
    					a.valor_cashback LIKE :pesquisa OR 
    					a.valor_comissao LIKE :pesquisa OR 
    					a.data_hora_lancamento_compra LIKE :pesquisa OR
    					c.nome_usuario LIKE :pesquisa OR
						c.cpf_usuario LIKE :pesquisa
    				) 
    				$query
    			ORDER BY 
    			    id_lancamento_compra 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>

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