<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$results_per_page = 50;

$db1 = new SQLite3('a/.eggziedb.db');
$db1->busyTimeout(5000);
$db1->exec('PRAGMA journal_mode = WAL');

require_once 'includes/DNSDatabase.php';
$dnsDB = DNSDatabase::getInstance();

// Função auxiliar para obter lista de revendas e sub revendas
$revendas = [];
$sub_revendas = [];
$rev_res = $db1->query("SELECT usuario FROM revenda");
while ($r = $rev_res->fetchArray(SQLITE3_ASSOC)) {
    $revendas[] = $r['usuario'];
}
$sub_res = $db1->query("SELECT usuario FROM sub_revenda");
while ($s = $sub_res->fetchArray(SQLITE3_ASSOC)) {
    $sub_revendas[] = $s['usuario'];
}

$searchTerm = isset($_GET['search']) ? $_GET['search'] : '';
$searchCondition = '';
if (!empty($searchTerm)) {
    $searchCondition = "AND (mac_address LIKE '%$searchTerm%' OR title LIKE '%$searchTerm%' OR username LIKE '%$searchTerm%')";
}

// Definir páginas para cada grupo
$page_admin = isset($_GET['page_admin']) ? (int)$_GET['page_admin'] : 1;
$page_revenda = isset($_GET['page_revenda']) ? (int)$_GET['page_revenda'] : 1;
$page_sub = isset($_GET['page_sub']) ? (int)$_GET['page_sub'] : 1;

$start_admin = ($page_admin - 1) * $results_per_page;
$start_revenda = ($page_revenda - 1) * $results_per_page;
$start_sub = ($page_sub - 1) * $results_per_page;

// Consultas para cada grupo
$sql_admin_count = "SELECT COUNT(id) AS total FROM ibo WHERE seller = 'admin' $searchCondition";
$res_admin_count = $db1->query($sql_admin_count);
$total_admin = $res_admin_count->fetchArray(SQLITE3_ASSOC)['total'];
$sql_admin = "SELECT * FROM ibo WHERE seller = 'admin' $searchCondition ORDER BY id ASC LIMIT $start_admin, $results_per_page";
$res_admin = $db1->query($sql_admin);

$revenda_list = implode("','", array_map(function($v){ return SQLite3::escapeString($v); }, $revendas));
$revenda_cond = "seller IN ('$revenda_list')";
if (empty($revendas)) $revenda_cond = "1=0";
$sql_revenda_count = "SELECT COUNT(id) AS total FROM ibo WHERE $revenda_cond $searchCondition";
$res_revenda_count = $db1->query($sql_revenda_count);
$total_revenda = $res_revenda_count->fetchArray(SQLITE3_ASSOC)['total'];
$sql_revenda = "SELECT * FROM ibo WHERE $revenda_cond $searchCondition ORDER BY id ASC LIMIT $start_revenda, $results_per_page";
$res_revenda = $db1->query($sql_revenda);

$sub_list = implode("','", array_map(function($v){ return SQLite3::escapeString($v); }, $sub_revendas));
$sub_cond = "seller IN ('$sub_list')";
if (empty($sub_revendas)) $sub_cond = "1=0";
$sql_sub_count = "SELECT COUNT(id) AS total FROM ibo WHERE $sub_cond $searchCondition";
$res_sub_count = $db1->query($sql_sub_count);
$total_sub = $res_sub_count->fetchArray(SQLITE3_ASSOC)['total'];
$sql_sub = "SELECT * FROM ibo WHERE $sub_cond $searchCondition ORDER BY id ASC LIMIT $start_sub, $results_per_page";
$res_sub = $db1->query($sql_sub);

if (isset($_GET['delete'])) {
    $ids_to_delete = explode(',', $_GET['delete']);
    foreach ($ids_to_delete as $id) {
        $db1->exec('DELETE FROM ibo WHERE id=' . (int)$id);
    }
    header('Location: users.php');
    exit();
}

include 'includes/header.php';
?>

<style>
* {
    box-sizing: border-box;
}
html, body {
    width: 100%;
    overflow-x: hidden;
}
.main-container {
    width: 100%;
    padding: 1.5rem;
}
.content-wrapper {
    width: 100%;
    max-width: 100%;
    margin: 0 auto;
}
.card-container {
    margin-bottom: 1.5rem;
    width: 100%;
}
.card {
    background: white;
    border-left: 4px solid #dc3545;
    border-radius: 0.35rem;
    box-shadow: 0 .15rem 1.75rem 0 rgba(58,59,69,.15);
    width: 100%;
}
.card-header {
    padding: 1rem;
    border-bottom: 1px solid #e3e6f0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 10px;
}
.card-header h6 {
    margin: 0;
    font-weight: bold;
    color: #dc3545;
}
.card-body {
    padding: 1rem;
}
.search-group {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin-bottom: 1rem;
}
.input-group {
    display: flex;
    flex: 2;
    min-width: 200px;
}
.input-group input {
    flex: 1;
    padding: 0.375rem 0.75rem;
    border: 1px solid #ced4da;
    border-radius: 0.25rem 0 0 0.25rem;
}
.input-group button {
    padding: 0.375rem 0.75rem;
    background: #007bff;
    color: white;
    border: none;
    border-radius: 0 0.25rem 0.25rem 0;
    cursor: pointer;
}
.btn-secondary {
    background: #6c757d;
    color: white;
    padding: 0.375rem 0.75rem;
    text-decoration: none;
    border-radius: 0.25rem;
    display: inline-block;
}
.btn-primary {
    background: #007bff;
    color: white;
    padding: 0.375rem 0.75rem;
    text-decoration: none;
    border-radius: 0.25rem;
    display: inline-block;
}
.btn-danger {
    background: #dc3545;
    color: white;
    padding: 0.25rem 0.5rem;
    border: none;
    border-radius: 0.2rem;
    cursor: pointer;
    font-size: 0.75rem;
}
.btn-sm {
    padding: 0.25rem 0.5rem;
    font-size: 0.75rem;
    line-height: 1.5;
    border-radius: 0.2rem;
    text-decoration: none;
    display: inline-block;
}
.btn-warning {
    background: #ffc107;
    color: #1f2d3d;
}
.btn-warning:hover {
    background: #e0a800;
}
.btn-danger-sm {
    background: #dc3545;
    color: white;
    padding: 0.25rem 0.5rem;
    border-radius: 0.2rem;
    text-decoration: none;
    display: inline-block;
    font-size: 0.75rem;
}
.table-responsive {
    overflow-x: auto;
}
.table {
    width: 100%;
    border-collapse: collapse;
}
.table th {
    background-color: rgba(255, 255, 255, 0.9);
    color: #007bff;
    font-weight: bold;
    padding: 0.75rem;
    border-bottom: 2px solid #dee2e6;
    text-align: left;
    white-space: nowrap;
}
.table td {
    padding: 0.75rem;
    border-top: 1px solid #dee2e6;
    color: #000000;
    white-space: nowrap;
}
.table-striped tbody tr:nth-child(odd) {
    background-color: rgba(0, 123, 255, 0.1);
}
.table-striped tbody tr:nth-child(even) {
    background-color: rgba(0, 0, 0, 0.05);
}
.table-striped tbody tr:hover {
    background-color: rgba(0, 123, 255, 0.2);
}
.text-center {
    text-align: center;
}
.pagination {
    margin-top: 1rem;
    text-align: center;
}
.pagination-gap {
    margin: 2px;
    background: #28a745;
    color: white;
    padding: 5px 10px;
    display: inline-block;
    border-radius: 4px;
    text-decoration: none;
}
.pagination-red {
    margin: 2px;
    background: #007bff;
    color: white;
    padding: 5px 10px;
    display: inline-block;
    border-radius: 4px;
    text-decoration: none;
}
.badge-test {
    background: #ffc107;
    color: #1f2d3d;
    padding: 3px 8px;
    border-radius: 4px;
    font-size: 11px;
    display: inline-block;
}
.badge-annual {
    background: #28a745;
    color: white;
    padding: 3px 8px;
    border-radius: 4px;
    font-size: 11px;
    display: inline-block;
}
.badge-expired {
    background: #dc3545;
    color: white;
    padding: 3px 8px;
    border-radius: 4px;
    font-size: 11px;
    display: inline-block;
}
/* Destaque em negrito e preto para todas as tabelas dos cards */
#table_admin td,
#table_revenda td,
#table_sub td {
    font-weight: bold;
    color: #000000;
}
/* Melhorias nos ícones de ação */
.btn-action {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: 6px;
    transition: all 0.2s ease;
    text-decoration: none;
    font-size: 1rem;
}
.btn-edit {
    background-color: #ffc107;
    color: #1f2d3d;
}
.btn-edit:hover {
    background-color: #e0a800;
    transform: scale(1.05);
    box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}
.btn-delete {
    background-color: #dc3545;
    color: white;
}
.btn-delete:hover {
    background-color: #c82333;
    transform: scale(1.05);
    box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}
</style>

<script>
    document.addEventListener("DOMContentLoaded", function() {
        window.toggleSelectAll = function(tableId, sourceCheckbox) {
            let checkboxes = document.querySelectorAll('#' + tableId + ' input[name="delete_check[]"]');
            checkboxes.forEach(cb => cb.checked = sourceCheckbox.checked);
        };

        document.querySelectorAll('.btn-excluir-selecionados').forEach(btn => {
            btn.addEventListener('click', function() {
                let tableId = this.getAttribute('data-table');
                let checkboxes = document.querySelectorAll('#' + tableId + ' input[name="delete_check[]"]:checked');
                let ids = Array.from(checkboxes).map(cb => cb.value);
                if(ids.length) {
                    if(confirm("Tem certeza de que deseja excluir os itens selecionados?"))
                        window.location.href = 'users.php?delete=' + ids.join(',');
                } else alert("Selecione pelo menos um item.");
            });
        });

        window.SetModalFun = function(id) {
            document.getElementById('deletar_modal').href = 'users.php?delete=' + id;
        }
    });
</script>

<div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h2>Confirmar</h2>
            </div>
            <div class="modal-body">Você realmente deseja excluir este usuário?</div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                <a class="btn btn-danger btn-ok" id="deletar_modal">Deletar</a>
            </div>
        </div>
    </div>
</div>

<div class="main-container">
    <div class="content-wrapper">

        <!-- Card: Usuários Administradores -->
        <div class="card-container">
            <div class="card">
                <div class="card-header">
                    <h6><i class="fas fa-user-shield"></i> Usuários Administradores (<?php echo $total_admin; ?>)</h6>
                    <button type="button" class="btn-danger btn-excluir-selecionados" data-table="table_admin">
                        <i class="fas fa-trash"></i> Excluir Selecionados
                    </button>
                </div>
                <div class="card-body">
                    <div class="search-group">
                        <form method="get" style="flex:2; margin:0;">
                            <div class="input-group">
                                <input type="text" name="search" placeholder="Pesquise por MAC, Título, Username..." value="<?php echo htmlspecialchars($searchTerm); ?>">
                                <button type="submit"><i class="fas fa-search"></i> Pesquisar</button>
                            </div>
                        </form>
                        <?php if(!empty($searchTerm)): ?>
                            <a href="users.php" class="btn-secondary"><i class="fas fa-times"></i> Limpar</a>
                        <?php endif; ?>
                    </div>
                    <div style="margin-bottom:1rem;">
                        <a href="./users_create.php" class="btn-primary"><i class="fas fa-plus"></i> Criar</a>
                    </div>
                    <div class="table-responsive">
                        <table class="table table-striped" id="table_admin">
                            <thead>
                                 <tr>
                                    <th style="width:30px;"><input type="checkbox" onclick="toggleSelectAll('table_admin', this)"></th>
                                    <th>SERVIDOR</th>
                                    <th>CLIENTE</th>
                                    <th>DNS</th>
                                    <th>USUÁRIO</th>
                                    <th>TIPO</th>
                                    <th>VALIDADE</th>
                                    <th>MAC ADDRESS</th>
                                    <th style="width:60px;">EDITAR</th>
                                    <th style="width:60px;">EXCLUIR</th>
                                 </tr>
                            </thead>
                            <tbody>
                                <?php $total_admin_reg = 0;
                                while($row = $res_admin->fetchArray()) {
                                    $total_admin_reg++;
                                    $id = $row['id'];
                                    $title = htmlspecialchars($row['title']);
                                    $extra = htmlspecialchars($row['extra']);
                                    $username = htmlspecialchars($row['username']);
                                    $mac = htmlspecialchars($row['mac_address']);
                                    $expire = $row['expire_date'];
                                    $url = $row['url'];
                                    $is_test = isset($row['is_test']) ? $row['is_test'] : 0;
                                    $expired = strtotime($expire) < time();
                                    $dns_info = $dnsDB->getByDns($url, 'admin', $row['seller']);
                                    $dns_display = $dns_info ? htmlspecialchars($dns_info['nome']) : htmlspecialchars($url);
                                ?>
                                 <tr>
                                    <td class="text-center"><input type="checkbox" name="delete_check[]" value="<?php echo $id; ?>"><?php echo $id; ?></td>
                                    <td><?php echo $title; ?></td>
                                    <td><?php echo $extra; ?></td>
                                    <td><?php echo $dns_display; ?></td>
                                    <td><?php echo $username; ?></td>
                                    <td>
                                        <?php if($is_test) echo '<span class="badge-test"><i class="fas fa-flask"></i> TESTE</span>';
                                        elseif($expired) echo '<span class="badge-expired"><i class="fas fa-hourglass-end"></i> EXPIRADO</span>';
                                        else echo '<span class="badge-annual"><i class="fas fa-calendar-check"></i> ANUAL</span>'; ?>
                                    </td>
                                    <td><?php echo htmlspecialchars($expire); if(!$expired && !$is_test) echo ' <small class="text-success">(Ativo)</small>'; elseif($expired) echo ' <small class="text-danger">(Expirado)</small>'; ?></td>
                                    <td><?php echo $mac; ?></td>
                                    <td class="text-center"><a class="btn-action btn-edit" href="./users_update.php?update=<?php echo $id; ?>" title="Editar"><i class="fas fa-edit"></i></a></td>
                                    <td class="text-center"><a class="btn-action btn-delete delete-btn" href="#" onclick="SetModalFun('<?php echo $id;?>');" data-toggle="modal" data-target="#confirm-delete" title="Excluir"><i class="fas fa-trash-alt"></i></a></td>
                                 </tr>
                                <?php } ?>
                                <?php if($total_admin_reg==0): ?>
                                 <tr>
                                    <td colspan="10" class="text-center">Nenhum usuário administrador encontrado</td>
                                 </tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                    <?php if($results_per_page < $total_admin): ?>
                    <div class="pagination">
                        <?php if($page_admin>1): ?><a class="pagination-gap" href='users.php?page_admin=<?php echo ($page_admin-1); ?>&search=<?php echo urlencode($searchTerm); ?>'>&lt; Previous</a><?php endif; ?>
                        <?php for($i=1;$i<=ceil($total_admin/$results_per_page);$i++): ?>
                            <a class="<?php echo $i==$page_admin?'pagination-red':'pagination-gap'; ?>" href='users.php?page_admin=<?php echo $i; ?>&search=<?php echo urlencode($searchTerm); ?>'><?php echo $i; ?></a>
                        <?php endfor; ?>
                        <?php if($page_admin<ceil($total_admin/$results_per_page)): ?><a class="pagination-gap" href='users.php?page_admin=<?php echo ($page_admin+1); ?>&search=<?php echo urlencode($searchTerm); ?>'>Next &gt;</a><?php endif; ?>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <!-- Card: Usuários de Revendas -->
        <div class="card-container">
            <div class="card">
                <div class="card-header">
                    <h6><i class="fas fa-store"></i> Usuários de Revendas (<?php echo $total_revenda; ?>)</h6>
                    <button type="button" class="btn-danger btn-excluir-selecionados" data-table="table_revenda">
                        <i class="fas fa-trash"></i> Excluir Selecionados
                    </button>
                </div>
                <div class="card-body">
                    <div class="search-group">
                        <form method="get" style="flex:2; margin:0;">
                            <div class="input-group">
                                <input type="text" name="search" placeholder="Pesquise por MAC, Título, Username..." value="<?php echo htmlspecialchars($searchTerm); ?>">
                                <button type="submit"><i class="fas fa-search"></i> Pesquisar</button>
                            </div>
                        </form>
                        <?php if(!empty($searchTerm)): ?>
                            <a href="users.php" class="btn-secondary"><i class="fas fa-times"></i> Limpar</a>
                        <?php endif; ?>
                    </div>
                    <div class="table-responsive">
                        <table class="table table-striped" id="table_revenda">
                            <thead>
                                 <tr>
                                    <th style="width:30px;"><input type="checkbox" onclick="toggleSelectAll('table_revenda', this)"></th>
                                    <th>SERVIDOR</th>
                                    <th>CLIENTE</th>
                                    <th>DNS</th>
                                    <th>USUÁRIO</th>
                                    <th>TIPO</th>
                                    <th>VALIDADE</th>
                                    <th>MAC ADDRESS</th>
                                    <th style="width:60px;">EDITAR</th>
                                    <th style="width:60px;">EXCLUIR</th>
                                 </tr>
                            </thead>
                            <tbody>
                                <?php $total_revenda_reg = 0;
                                while($row = $res_revenda->fetchArray()) {
                                    $total_revenda_reg++;
                                    $id = $row['id'];
                                    $title = htmlspecialchars($row['title']);
                                    $extra = htmlspecialchars($row['extra']);
                                    $username = htmlspecialchars($row['username']);
                                    $mac = htmlspecialchars($row['mac_address']);
                                    $expire = $row['expire_date'];
                                    $url = $row['url'];
                                    $is_test = isset($row['is_test']) ? $row['is_test'] : 0;
                                    $expired = strtotime($expire) < time();
                                    $dns_info = $dnsDB->getByDns($url, 'revenda', $row['seller']);
                                    $dns_display = $dns_info ? htmlspecialchars($dns_info['nome']) : htmlspecialchars($url);
                                ?>
                                 <tr>
                                    <td class="text-center"><input type="checkbox" name="delete_check[]" value="<?php echo $id; ?>"></td>
                                    <td><?php echo $title; ?></td>
                                    <td><?php echo $extra; ?></td>
                                    <td><?php echo $dns_display; ?></td>
                                    <td><?php echo $username; ?></td>
                                    <td>
                                        <?php if($is_test) echo '<span class="badge-test"><i class="fas fa-flask"></i> TESTE</span>';
                                        elseif($expired) echo '<span class="badge-expired"><i class="fas fa-hourglass-end"></i> EXPIRADO</span>';
                                        else echo '<span class="badge-annual"><i class="fas fa-calendar-check"></i> ANUAL</span>'; ?>
                                    </td>
                                    <td><?php echo htmlspecialchars($expire); if(!$expired && !$is_test) echo ' <small class="text-success">(Ativo)</small>'; elseif($expired) echo ' <small class="text-danger">(Expirado)</small>'; ?></td>
                                    <td><?php echo $mac; ?></td>
                                    <td class="text-center"><a class="btn-action btn-edit" href="./users_update.php?update=<?php echo $id; ?>" title="Editar"><i class="fas fa-edit"></i></a></td>
                                    <td class="text-center"><a class="btn-action btn-delete delete-btn" href="#" onclick="SetModalFun('<?php echo $id;?>');" data-toggle="modal" data-target="#confirm-delete" title="Excluir"><i class="fas fa-trash-alt"></i></a></td>
                                 </tr>
                                <?php } ?>
                                <?php if($total_revenda_reg==0): ?>
                                 <tr>
                                    <td colspan="10" class="text-center">Nenhum usuário de revenda encontrado</td>
                                 </tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                    <?php if($results_per_page < $total_revenda): ?>
                    <div class="pagination">
                        <?php if($page_revenda>1): ?><a class="pagination-gap" href='users.php?page_revenda=<?php echo ($page_revenda-1); ?>&search=<?php echo urlencode($searchTerm); ?>'>&lt; Previous</a><?php endif; ?>
                        <?php for($i=1;$i<=ceil($total_revenda/$results_per_page);$i++): ?>
                            <a class="<?php echo $i==$page_revenda?'pagination-red':'pagination-gap'; ?>" href='users.php?page_revenda=<?php echo $i; ?>&search=<?php echo urlencode($searchTerm); ?>'><?php echo $i; ?></a>
                        <?php endfor; ?>
                        <?php if($page_revenda<ceil($total_revenda/$results_per_page)): ?><a class="pagination-gap" href='users.php?page_revenda=<?php echo ($page_revenda+1); ?>&search=<?php echo urlencode($searchTerm); ?>'>Next &gt;</a><?php endif; ?>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <!-- Card: Usuários de Sub Revendas -->
        <div class="card-container">
            <div class="card">
                <div class="card-header">
                    <h6><i class="fas fa-users"></i> Usuários de Sub Revendas (<?php echo $total_sub; ?>)</h6>
                    <button type="button" class="btn-danger btn-excluir-selecionados" data-table="table_sub">
                        <i class="fas fa-trash"></i> Excluir Selecionados
                    </button>
                </div>
                <div class="card-body">
                    <div class="search-group">
                        <form method="get" style="flex:2; margin:0;">
                            <div class="input-group">
                                <input type="text" name="search" placeholder="Pesquise por MAC, Título, Username..." value="<?php echo htmlspecialchars($searchTerm); ?>">
                                <button type="submit"><i class="fas fa-search"></i> Pesquisar</button>
                            </div>
                        </form>
                        <?php if(!empty($searchTerm)): ?>
                            <a href="users.php" class="btn-secondary"><i class="fas fa-times"></i> Limpar</a>
                        <?php endif; ?>
                    </div>
                    <div class="table-responsive">
                        <table class="table table-striped" id="table_sub">
                            <thead>
                                 <tr>
                                    <th style="width:30px;"><input type="checkbox" onclick="toggleSelectAll('table_sub', this)"></th>
                                    <th>SERVIDOR</th>
                                    <th>CLIENTE</th>
                                    <th>DNS</th>
                                    <th>USUÁRIO</th>
                                    <th>TIPO</th>
                                    <th>VALIDADE</th>
                                    <th>MAC ADDRESS</th>
                                    <th style="width:60px;">EDITAR</th>
                                    <th style="width:60px;">EXCLUIR</th>
                                 </tr>
                            </thead>
                            <tbody>
                                <?php $total_sub_reg = 0;
                                while($row = $res_sub->fetchArray()) {
                                    $total_sub_reg++;
                                    $id = $row['id'];
                                    $title = htmlspecialchars($row['title']);
                                    $extra = htmlspecialchars($row['extra']);
                                    $username = htmlspecialchars($row['username']);
                                    $mac = htmlspecialchars($row['mac_address']);
                                    $expire = $row['expire_date'];
                                    $url = $row['url'];
                                    $is_test = isset($row['is_test']) ? $row['is_test'] : 0;
                                    $expired = strtotime($expire) < time();
                                    $dns_info = $dnsDB->getByDns($url, 'sub_revenda', $row['seller']);
                                    $dns_display = $dns_info ? htmlspecialchars($dns_info['nome']) : htmlspecialchars($url);
                                ?>
                                 <tr>
                                    <td class="text-center"><input type="checkbox" name="delete_check[]" value="<?php echo $id; ?>"></td>
                                    <td><?php echo $title; ?></td>
                                    <td><?php echo $extra; ?></td>
                                    <td><?php echo $dns_display; ?></td>
                                    <td><?php echo $username; ?></td>
                                    <td>
                                        <?php if($is_test) echo '<span class="badge-test"><i class="fas fa-flask"></i> TESTE</span>';
                                        elseif($expired) echo '<span class="badge-expired"><i class="fas fa-hourglass-end"></i> EXPIRADO</span>';
                                        else echo '<span class="badge-annual"><i class="fas fa-calendar-check"></i> ANUAL</span>'; ?>
                                    </td>
                                    <td><?php echo htmlspecialchars($expire); if(!$expired && !$is_test) echo ' <small class="text-success">(Ativo)</small>'; elseif($expired) echo ' <small class="text-danger">(Expirado)</small>'; ?></td>
                                    <td><?php echo $mac; ?></td>
                                    <td class="text-center"><a class="btn-action btn-edit" href="./users_update.php?update=<?php echo $id; ?>" title="Editar"><i class="fas fa-edit"></i></a></td>
                                    <td class="text-center"><a class="btn-action btn-delete delete-btn" href="#" onclick="SetModalFun('<?php echo $id;?>');" data-toggle="modal" data-target="#confirm-delete" title="Excluir"><i class="fas fa-trash-alt"></i></a></td>
                                 </tr>
                                <?php } ?>
                                <?php if($total_sub_reg==0): ?>
                                 <tr>
                                    <td colspan="10" class="text-center">Nenhum usuário de sub revenda encontrado</td>
                                 </tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                    <?php if($results_per_page < $total_sub): ?>
                    <div class="pagination">
                        <?php if($page_sub>1): ?><a class="pagination-gap" href='users.php?page_sub=<?php echo ($page_sub-1); ?>&search=<?php echo urlencode($searchTerm); ?>'>&lt; Previous</a><?php endif; ?>
                        <?php for($i=1;$i<=ceil($total_sub/$results_per_page);$i++): ?>
                            <a class="<?php echo $i==$page_sub?'pagination-red':'pagination-gap'; ?>" href='users.php?page_sub=<?php echo $i; ?>&search=<?php echo urlencode($searchTerm); ?>'><?php echo $i; ?></a>
                        <?php endfor; ?>
                        <?php if($page_sub<ceil($total_sub/$results_per_page)): ?><a class="pagination-gap" href='users.php?page_sub=<?php echo ($page_sub+1); ?>&search=<?php echo urlencode($searchTerm); ?>'>Next &gt;</a><?php endif; ?>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <?php include 'includes/footer.php'; ?>
    </div>
</div>