<?php


//<----------------------------------------------------------->
//<-------------------COPIA AI BRUXÃO------------------------->
//<-------------CONTATO: https://t.me/eonbry------------------>
//<-------------DEVELOPER: EMANUEL NASCIMENTO----------------->
//<----------------------------------------------------------->


ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$results_per_page = 20; // Defina o número desejado de itens por página

$db1 = new SQLite3('a/.eggziedb.db');

// Search functionality
$searchTerm = isset($_GET['search']) ? $_GET['search'] : '';
$searchQuery = '';
if (!empty($searchTerm)) {
    $searchQuery = "WHERE mac_address LIKE '%$searchTerm%' OR title LIKE '%$searchTerm%' OR username LIKE '%$searchTerm%'";
}

$result = $db1->query("SELECT COUNT(id) AS total FROM ibo $searchQuery");
$rownew = $result->fetchArray();
$totaleview = $rownew['total'];

// Não precisamos mais calcular o total de páginas
// Removendo a lógica de paginação

$res1 = $db1->query("SELECT * FROM ibo $searchQuery ORDER BY id ASC");

if (isset($_GET['delete'])) {
    $ids_to_delete = explode(',', $_GET['delete']);
    foreach ($ids_to_delete as $id) {
        $db1->exec('DELETE FROM ibo WHERE id=' . $id);
    }
    header('Location: users.php');
    exit(); // Para garantir que o código pare de ser executado após o redirecionamento
}

include 'includes/header.php';
?>

<style>
/* === ESTILOS ESPECÍFICOS PARA A PÁGINA DE USUÁRIOS === */
.main-content {
    background: transparent !important;
    padding: 20px;
}

.card {
    background: var(--surface-light) !important;
    border: 1px solid var(--border) !important;
    border-radius: 16px !important;
    box-shadow: 0 8px 30px rgba(0,0,0,0.3) !important;
    color: var(--text-primary) !important;
}

.card-header {
    background: linear-gradient(135deg, var(--surface-light), var(--surface-lighter)) !important;
    border-bottom: 1px solid var(--border) !important;
    color: var(--text-primary) !important;
    font-weight: 600 !important;
}

.card-body {
    background: transparent !important;
    color: var(--text-primary) !important;
}

/* Tabela futurista */
.table {
    background: var(--surface-light) !important;
    color: var(--text-primary) !important;
    border: 1px solid var(--border) !important;
    border-radius: 12px !important;
    overflow: hidden;
}

.table thead {
    background: linear-gradient(135deg, var(--surface-lighter), var(--surface-light)) !important;
}

.table th {
    border-bottom: 2px solid var(--primary) !important;
    color: var(--text-primary) !important;
    font-weight: 600 !important;
    padding: 15px 10px !important;
}

.table td {
    border-bottom: 1px solid var(--border) !important;
    color: var(--text-secondary) !important;
    padding: 12px 10px !important;
    vertical-align: middle !important;
}

.table tbody tr:hover {
    background: rgba(0, 212, 255, 0.1) !important;
    transform: translateX(4px);
    transition: all 0.3s ease;
}

/* Formulários futuristas */
.form-control {
    background: var(--surface-lighter) !important;
    border: 1px solid var(--border) !important;
    color: var(--text-primary) !important;
    border-radius: 12px !important;
    padding: 12px 15px !important;
}

.form-control:focus {
    border-color: var(--primary) !important;
    box-shadow: 0 0 0 0.3rem rgba(0, 212, 255, 0.25) !important;
    background: var(--surface-lighter) !important;
    color: var(--text-primary) !important;
}

.form-control::placeholder {
    color: var(--text-muted) !important;
}

/* Botões futuristas */
.btn-primary {
    background: linear-gradient(135deg, var(--primary), var(--primary-dark)) !important;
    border: none !important;
    border-radius: 12px !important;
    box-shadow: var(--glow) !important;
    padding: 10px 20px !important;
    font-weight: 600 !important;
    transition: all 0.3s ease !important;
}

.btn-primary:hover {
    transform: translateY(-2px) !important;
    box-shadow: 0 0 30px rgba(0, 212, 255, 0.5) !important;
}

.btn-danger {
    background: linear-gradient(135deg, #FF6B9D, #FF416C) !important;
    border: none !important;
    border-radius: 12px !important;
    box-shadow: 0 0 20px rgba(255, 107, 157, 0.3) !important;
    padding: 10px 20px !important;
    font-weight: 600 !important;
    transition: all 0.3s ease !important;
}

.btn-danger:hover {
    transform: translateY(-2px) !important;
    box-shadow: 0 0 30px rgba(255, 107, 157, 0.5) !important;
}

/* Checkboxes futuristas */
input[type="checkbox"] {
    width: 18px !important;
    height: 18px !important;
    accent-color: var(--primary) !important;
    cursor: pointer !important;
}

/* Ícones */
.fa-edit {
    color: var(--primary) !important;
    transition: all 0.3s ease !important;
}

.fa-ban {
    color: #FF6B9D !important;
    transition: all 0.3s ease !important;
}

.fa-edit:hover, .fa-ban:hover {
    transform: scale(1.2) !important;
    filter: drop-shadow(0 0 8px currentColor) !important;
}

/* Input group */
.input-group {
    border-radius: 12px !important;
    overflow: hidden !important;
}

.input-group .form-control {
    border-radius: 12px 0 0 12px !important;
}

.input-group-append .btn {
    border-radius: 0 12px 12px 0 !important;
}

/* Badges e labels */
.text-primary2 {
    color: var(--primary) !important;
    background: linear-gradient(135deg, var(--primary), var(--accent));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

/* Paginação */
.pagination .page-link {
    background: var(--surface-light) !important;
    border: 1px solid var(--border) !important;
    color: var(--text-primary) !important;
}

.pagination .page-item.active .page-link {
    background: linear-gradient(135deg, var(--primary), var(--accent)) !important;
    border-color: var(--primary) !important;
}

/* Alertas */
.alert {
    background: var(--surface-light) !important;
    border: 1px solid var(--border) !important;
    color: var(--text-primary) !important;
    border-radius: 12px !important;
}

/* Responsividade */
@media (max-width: 768px) {
    .main-content {
        padding: 10px !important;
    }
    
    .table-responsive {
        border: 1px solid var(--border) !important;
        border-radius: 12px !important;
    }
    
    .table th, .table td {
        padding: 8px 6px !important;
        font-size: 12px !important;
    }
    
    .btn {
        padding: 8px 12px !important;
        font-size: 12px !important;
    }
}
</style>

<script>
// Partículas animadas
document.addEventListener('DOMContentLoaded', function() {
    const numParticles = 40;
    const particlesContainer = document.createElement('div');
    particlesContainer.style.position = 'fixed';
    particlesContainer.style.top = '0';
    particlesContainer.style.left = '0';
    particlesContainer.style.width = '100%';
    particlesContainer.style.height = '100%';
    particlesContainer.style.pointerEvents = 'none';
    particlesContainer.style.zIndex = '-1';
    
    for (let i = 0; i < numParticles; i++) {
        const particle = document.createElement('div');
        particle.classList.add('particle');
        particle.style.left = Math.random() * 100 + 'vw';
        particle.style.animationDuration = (3 + Math.random() * 7) + 's';
        particle.style.animationDelay = Math.random() * 5 + 's';
        particle.style.width = particle.style.height = (Math.random() * 3) + 'px';
        particlesContainer.appendChild(particle);
    }
    
    document.body.appendChild(particlesContainer);
    
    // Script para selecionar/deselecionar todos os checkboxes
    window.selecionarTodos = function() {
        const checkboxes = document.querySelectorAll('input[name="delete_check[]"]');
        const selecionarTodosCheckbox = document.getElementById('selecionarTodos');
        
        checkboxes.forEach(checkbox => {
            checkbox.checked = selecionarTodosCheckbox.checked;
        });
    };
    
    // Script para excluir selecionados
    document.getElementById('excluirSelecionados').addEventListener('click', function() {
        const checkboxes = document.querySelectorAll('input[name="delete_check[]"]:checked');
        const ids = Array.from(checkboxes).map(checkbox => checkbox.value);
        
        if (ids.length === 0) {
            alert('Selecione pelo menos um item para excluir.');
            return;
        }
        
        if (confirm(`Tem certeza que deseja excluir ${ids.length} item(ns)?`)) {
            window.location.href = `./users.php?delete=${ids.join(',')}`;
        }
    });
});
</script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<main role="main" class="col-12 pt-4 px-3 main-content">
    <div class="">
        <div class="chartjs-size-monitor" style="position:absolute; left:0; top:0; right:0; bottom:0; overflow:hidden; pointer-events:none; visibility:hidden; z-index:-1;">
            <div class="chartjs-size-monitor-expand" style="position:absolute;left:0;top:0;right:0;bottom:0;overflow:hidden;pointer-events:none;visibility:hidden;z-index:-1;">
                <div style="position:absolute;width:1000000px;height:1000000px;left:0;top:0"></div>
            </div>
            <div class="chartjs-size-monitor-shrink" style="position:absolute;left:0;top:0;right:0;bottom:0;overflow:hidden;pointer-events:none;visibility:hidden;z-index:-1;">
                <div style="position:absolute;width:200%;height:200%;left:0; top:0"></div>
            </div>
        </div>
        <div id="main">
            <!-- Content Row -->
            <div class="row">
                <!-- First Column -->
                <div class="col-lg-12">
                    <!-- Custom codes -->
                    <div class="card border-left-primary shadow h-100 card shadow mb-4">
                        <div class="card-header py-3">
                            <h6 class="m-0 font-weight-bold text-primary2">
                                <i class="fas fa-fw fa-user-plus"></i> Usuários
                            </h6>
                        </div>
                        <div class="card-body">
                            <form method="get" class="mb-4">
                                <div class="form-group">
                                    <div class="input-group">
                                        <input type="text" name="search" class="form-control" placeholder="Pesquise por MAC, Titulo, Username...." value="<?php echo htmlspecialchars($searchTerm); ?>">
                                        <div class="input-group-append">
                                            <button type="submit" class="btn btn-primary">
                                                <i class="fas fa-search"></i> Pesquisar
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </form>
                            
                            <div class="form-group">
                                <div class="input-group">
                                    <a href="./users_create.php" class="btn btn-primary btn-icon-split">
                                        <span class="icon text-white-50">
                                            <i class="fas fa-plus"></i>
                                        </span>
                                        <span class="text">Criar Usuário</span>
                                    </a>
                                    <button type="button" class="btn btn-danger ml-2" id="excluirSelecionados">
                                        <i class="fas fa-trash"></i> Excluir Selecionados
                                    </button>
                                </div>
                            </div>
                            
                            <br>
                            
                            <div class="table-responsive">
                                <table class="table table-striped table-sm">
                                    <thead class="text-primary">
                                        <tr>
                                            <th><input type="checkbox" id="selecionarTodos" onclick="selecionarTodos()"></th>
                                            <th>Título</th>
                                            <th>Mac Address</th>
                                            <th>Username</th>
                                            <th>Vendedor</th>
                                            <th>Validade</th>
                                            <th>DNS</th>
                                            <th>Extra</th>
                                            <th>Editar</th>
                                            <th>Deletar</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php while ($row1 = $res1->fetchArray()) { ?>
                                            <tr>
                                                <?php
                                                $users_id = $row1['id'];
                                                $user_mac = $row1['mac_address'];
                                                $user_username = $row1['username'];
                                                $user_seller = $row1['seller'];
                                                $user_expire_date = $row1['expire_date'];
                                                $user_url = $row1['url'];
                                                $user_title = $row1['title'];
                                                ?>
                                                <td><input type="checkbox" name="delete_check[]" value="<?php echo $users_id; ?>"></td>
                                                <td><?php echo htmlspecialchars($user_title); ?></td>
                                                <td><code><?php echo htmlspecialchars($user_mac); ?></code></td>
                                                <td><?php echo htmlspecialchars($user_username); ?></td>
                                                <td><?php echo htmlspecialchars($user_seller); ?></td>
                                                <td><?php echo htmlspecialchars($user_expire_date); ?></td>
                                                <td><small><?php echo htmlspecialchars($user_url); ?></small></td>
                                                <td><?php echo htmlspecialchars($row1['extra']); ?></td>
                                                <td>
                                                    <a class="btn btn-icon" href="./users_update.php?update=<?php echo $users_id; ?>">
                                                       <span class="icon text-white-50">
                                                           <i class="fa fa-edit" style="font-size:20px;"></i>
                                                       </span>
                                                    </a>
                                                </td>
                                                <td>
                                                    <a class="btn btn-icon delete-btn" href="./users.php?delete=<?php echo $users_id; ?>" onclick="return confirm('Tem certeza de que deseja excluir este item?');">
                                                        <span class="icon text-white-50">
                                                            <i class="fa fa-ban" style="font-size:20px;"></i>
                                                        </span>
                                                    </a>
                                                </td>
                                            </tr>
                                        <?php } ?>
                                    </tbody>
                                </table>
                            </div>
                            
                            <?php if ($totaleview == 0): ?>
                                <div class="text-center py-4">
                                    <i class="fas fa-users fa-3x text-muted mb-3"></i>
                                    <h5 class="text-muted">Nenhum usuário encontrado</h5>
                                    <p class="text-muted">Use o botão "Criar Usuário" para adicionar novos usuários.</p>
                                </div>
                            <?php endif; ?>
                            
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</main>

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