<?php if (Session::get("tipo_usuario") != "1" and Session::get("tipo_usuario") != "2" and Session::get("usuario_ativo") != "S"){ show_403();} ?>
<?php import_file(DIR_TEMPLATES."header.php", ["title" => "Minhas Indicações", "active_page" => "meu-time"]); ?>
<?php import_file(DIR_MODELS."Model.class.php"); ?>
<?php import_file(DIR_LIBRARIES."functions.php"); ?>

<h4 class="mb-3">
	<i class="fa-regular fa-users"></i> Minhas Indicações
</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="text-center align-middle" style="width: 5%;">ID</th>
					<th class="text-left align-middle" style="width: 35%;">Nome</th>
					<th class="text-left align-middle" style="width: 30%;">E-mail</th>
					<th class="text-center align-middle" style="width: 20%;">Telefone</th>
					<th class="text-center align-middle" style="width: 10%;">Situaçã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 usuario 
						WHERE 
							id_tipo_usuario = 2 AND 
							id_indicador = :id AND 
							status_usuario = 'S' AND 
							usuario_ativo = 'S' AND 
							(
								nome_usuario LIKE :pesquisa OR 
								email_usuario LIKE :pesquisa OR 
								telefone_usuario LIKE :pesquisa OR 
								email_usuario LIKE :pesquisa
							) 
						ORDER BY id_usuario 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 $dados):

				?>

					<tr>
						<td class="text-center align-middle"><?php echo $dados->id_usuario; ?></td>
						<td class="text-left align-middle"><?php echo $dados->nome_usuario; ?></td>
						<td class="text-left align-middle"><?php echo $dados->email_usuario; ?></td>
						<td class="text-center align-middle"><?php echo $dados->telefone_usuario; ?></td>
						<td class="text-center align-middle">
							<span class="badge <?php echo (qualificado($dados->id_usuario)) ? "badge-success" : "badge-danger"; ?>">
								<?php echo (qualificado($dados->id_usuario)) ? "Qualificado" : "Não Qualificado"; ?>
							</span>
						</td>
					</tr>
				<?php endforeach; ?>
			</tbody>
		</table>
	</div>

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

			$result = $model->query("
				SELECT * FROM usuario 
				WHERE 
					id_tipo_usuario = 2 AND 
					id_indicador = :id AND 
					status_usuario = 'S' AND 
					usuario_ativo = 'S' AND 
					(
						nome_usuario LIKE :pesquisa OR 
						email_usuario LIKE :pesquisa OR 
						telefone_usuario LIKE :pesquisa OR 
						email_usuario LIKE :pesquisa
					) 
				ORDER BY id_usuario 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"); ?>