<?php 
include ('includes/header.php');

// nome da tabela
$table_name = "actreq";
$page_my = "activation_request.php";
$pagemac = "user_manage.php";

// busca da tabela
$res = $db->select($table_name, '*', '', '');

// deletar linha
if(isset($_GET['delete'])){
	$db->delete($table_name, 'id = :id',[':id' => $_GET['delete']]);
	echo "<script>window.location.href='".$page_my."?status=ok'</script>";
}

// deletar todos os registros
if (isset($_GET['confirm_wipe']) && $_GET['confirm_wipe'] == 1) {
    $db->deleteAll($table_name);
    echo "<script>window.location.href='".$page_my."?status=ok'</script>";
    exit;
}
?>

<script>
function confirmarExclusao() {
    if (confirm("⚠️ Todos os dados serão deletados!\nVocê tem certeza?")) {
        window.location.href = "<?= $page_my ?>?confirm_wipe=1";
    } else {
        alert("❌ Exclusão cancelada.");
    }
}
</script>

<div class="container-fluid pt-4 px-4">
    <div class="row g-4">
        <div class="col-12">
            <div class="bg-secondary rounded h-100 p-4">
                <h2 class="mb-4">Solicitações de Ativação Atuais</h2>
                
                <div class="text-end">
                    <a type="button" href="#" onclick="confirmarExclusao()" class="btn btn-square btn-primary m-2">
                        <i class="fa fa-eraser"></i>
                    </a>
                </div>

                <div class="table-responsive">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>#</th>
                                <th>Endereço MAC</th>
                                <th>Data da Solicitação</th>
                                <th>Observação</th>
                                <th>Adicionar Playlist</th>
                                <th>Deletar</th>
                            </tr>
                        </thead>
                        <?php foreach ($res as $row) { ?>
                        <tbody>
                            <tr>
                                <td><?= $row['id']; ?></td>
                                <td><?= $row['mac']; ?></td>
                                <td><?= $row['date']; ?></td>
                                <td><?= $row['remark']; ?></td>
                                <td>
                                    <a class="btn btn-square btn-warning m-2"
                                       href="<?php echo $pagemac; ?>?create&mac=<?php echo $row['mac']; ?>">
                                       <i class="fa fa-users"></i>
                                    </a>
                                </td>
                                <td>
                                    <a class="btn btn-square btn-primary m-2" href="#"
                                       data-href="<?php echo $page_my; ?>?delete=<?php echo $row['id']; ?>"
                                       data-bs-toggle="modal" data-bs-target="#confirm-delete">
                                       <i class="fa fa-trash"></i>
                                    </a>
                                </td>
                            </tr>
                        </tbody>
                        <?php } ?>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<!-- Fim da Tabela -->

<!-- Footer Start -->
<?php include ('includes/footer.php');?>
<!-- Footer End -->
</body>
</html>
