<section class="controle">
	<div class="container-fluid">
		<div class="row">
			<div class="col-md-3">
<?php include('pages/visual/common/menu.php'); ?>
			</div>
			<div class="col-md-8">
				<h2 class="text-center titleSection">Controle da Galeria</h2>
				<br>
				<?php
				$link = URL_BASE.'data/usuariosLeitura.php';
				$ch = curl_init($link);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
				curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
				curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
				$response = curl_exec($ch);
				$error = curl_error($ch);
				$info = curl_getinfo($ch);
				curl_close($ch);
				$usuarios = json_decode($response, true);
				?>
				<br><br>
				
				<div class="row">
					<div class="col-12">
					
					<table class="table table-striped">
					  <thead>
						<tr>
						  <th scope="col"> </th>
						  <th scope="col">Nome Completo</th>
						  <th scope="col">E-mail</th>
						  <th scope="col">Tipo de Usuário</th>
						  <th scope="col"> </th>
						</tr>
					  </thead>
					  <tbody id="table-body">

				<?php
				$i = 1;
				foreach($usuarios as $usuario){
				switch($usuario['aprovado']){
				case 0: $nwStatus = 1;
						$color = 'text-danger';
				break;
				case 1: $nwStatus = 0;
						$color = 'text-success';
				break;}
				$apv = '<a href="'.URL_BASE.'controle/command/aprovar.php?id='.$usuario['id'].'&aprovado='.$nwStatus.'"><i class="fa-solid fa-circle-user '.$color.'"></i></a>';
				?>
				
						<tr>
						  <th scope="row"><?php echo $i; ?></th>
						  <td><?php echo $usuario['nome']; ?></td>
						  <td style="font-size: 10pt;"><?php echo $usuario['email']; ?></td>
						  <td style="font-size: 10pt;">
							<?php echo $usuario['tipo']; ?><br>
							<?php
							if($usuario['tipo'] == 'Médico Formado'){
							echo '<strong>CRM - UF: </strong>'.$usuario['crmUf'];}
							if($usuario['tipo'] == 'Estudante de Medicina'){
							echo '<strong>Instituição de Ensino: </strong>'.$usuario['instituicaoEnsino'];}
							?>
						  </td>
						  <td><?php echo $apv; ?></td>
						</tr>				
				
				<?php
				$i++;
				}
				?>

					  </tbody>
					</table>

					<nav>
						<ul class="pagination justify-content-center" id="pagination">
							<!-- Botões serão adicionados dinamicamente -->
						</ul>
					</nav>
					
					</div>
				</div>
				
			</div>
			<div class="col-md-1">
				&nbsp;
			</div>
		</div>
	</div>
</section>

<script>
    $(document).ready(function() {
        let rowsPerPage = 10;
        let rows = $('#table-body tr');
        let totalPages = Math.ceil(rows.length / rowsPerPage);

        function showPage(page) {
            rows.hide();
            rows.slice((page - 1) * rowsPerPage, page * rowsPerPage).show();
        }

        function createPagination() {
            let pagination = $('#pagination');
            pagination.empty();

            for (let i = 1; i <= totalPages; i++) {
                let btn = $('<li class="page-item"><a class="page-link" href="#">' + i + '</a></li>');
                btn.click(function() {
                    $('.page-item').removeClass('active');
                    $(this).addClass('active');
                    showPage(i);
                });
                pagination.append(btn);
            }
            pagination.find('li:first-child').addClass('active');
        }

        showPage(1);
        createPagination();
    });
</script>