<?php
/**
 * UPGRADE PREMIUM BY MANUS AI
 * USERS PAGE ALIGNMENT
 */
session_start();
if (!isset($_SESSION['eggzie_iboPlayer'])) {
    header("Location: login.php");
    exit;
}

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

// Buscar todos os usuários
$result = $db->query('SELECT * FROM ibo ORDER BY id DESC');

include 'includes/header.php';
?>

<style>
    .users-container {
        animation: fadeIn 0.5s ease-in-out;
    }

    @keyframes fadeIn {
        from { opacity: 0; transform: translateY(10px); }
        to { opacity: 1; transform: translateY(0); }
    }

    .page-header-actions {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 30px;
        flex-wrap: wrap;
        gap: 15px;
    }

    .search-box {
        position: relative;
        flex: 1;
        max-width: 400px;
    }

    .search-box input {
        width: 100%;
        padding: 12px 15px 12px 45px;
        background: rgba(255, 255, 255, 0.05);
        border: 1px solid var(--border);
        border-radius: 12px;
        color: white;
        transition: 0.3s;
    }

    .search-box input:focus {
        outline: none;
        border-color: var(--primary);
        background: rgba(255, 255, 255, 0.08);
    }

    .search-box i {
        position: absolute;
        left: 15px;
        top: 50%;
        transform: translateY(-50%);
        color: var(--text-muted);
    }

    .btn-primary-custom {
        background: var(--primary);
        color: white;
        padding: 12px 25px;
        border-radius: 12px;
        text-decoration: none;
        font-weight: 600;
        display: flex;
        align-items: center;
        gap: 10px;
        transition: 0.3s;
    }

    .btn-primary-custom:hover {
        background: var(--primary-dark);
        transform: translateY(-2px);
    }

    .table-card {
        background: var(--card-bg);
        border-radius: 24px;
        border: 1px solid var(--border);
        padding: 25px;
        overflow: hidden;
    }

    .custom-table {
        width: 100%;
        border-collapse: separate;
        border-spacing: 0 10px;
    }

    .custom-table th {
        text-align: left;
        padding: 10px 15px;
        color: var(--text-muted);
        font-size: 12px;
        text-transform: uppercase;
        font-weight: 700;
    }

    .custom-table td {
        padding: 15px;
        background: rgba(255, 255, 255, 0.03);
        border-top: 1px solid var(--border);
        border-bottom: 1px solid var(--border);
    }

    .custom-table tr td:first-child {
        border-left: 1px solid var(--border);
        border-radius: 12px 0 0 12px;
    }

    .custom-table tr td:last-child {
        border-right: 1px solid var(--border);
        border-radius: 0 12px 12px 0;
    }

    .mac-badge {
        font-family: monospace;
        background: rgba(99, 102, 241, 0.1);
        color: var(--primary);
        padding: 4px 8px;
        border-radius: 6px;
        font-size: 13px;
    }

    .status-badge {
        padding: 6px 12px;
        border-radius: 8px;
        font-size: 11px;
        font-weight: 700;
    }

    .status-active { background: rgba(16, 185, 129, 0.1); color: #10b981; }
    .status-expired { background: rgba(239, 68, 68, 0.1); color: #ef4444; }

    .action-btn {
        width: 35px;
        height: 35px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        border-radius: 10px;
        transition: 0.2s;
        text-decoration: none;
    }

    .btn-edit { background: rgba(245, 158, 11, 0.1); color: #f59e0b; }
    .btn-delete { background: rgba(239, 68, 68, 0.1); color: #ef4444; }

    .action-btn:hover { transform: scale(1.1); }
</style>

<div class="users-container">
    <div class="page-header-actions">
        <div class="search-box">
            <i class="fas fa-search"></i>
            <input type="text" id="userSearch" placeholder="Pesquisar clientes...">
        </div>
        
        <a href="users_create.php" class="btn-primary-custom">
            <i class="fas fa-plus"></i>
            <span>Novo Cliente</span>
        </a>
    </div>

    <div class="table-card">
        <div style="overflow-x: auto;">
            <table class="custom-table">
                <thead>
                    <tr>
                        <th>Cliente</th>
                        <th>MAC Address</th>
                        <th>Usuário</th>
                        <th>Expiração</th>
                        <th>Status</th>
                        <th>Ações</th>
                    </tr>
                </thead>
                <tbody id="usersTableBody">
                    <?php 
                    $hasRows = false;
                    while($row = $result->fetchArray(SQLITE3_ASSOC)): 
                        $hasRows = true;
                        $is_expired = strtotime($row['expire_date']) < time();
                    ?>
                    <tr class="user-row">
                        <td><div style="font-weight: 700;"><?php echo htmlspecialchars($row['title'] ?? '-'); ?></div></td>
                        <td><span class="mac-badge"><?php echo htmlspecialchars($row['mac_address'] ?? '-'); ?></span></td>
                        <td><?php echo htmlspecialchars($row['username'] ?? '-'); ?></td>
                        <td><?php echo date('d/m/Y', strtotime($row['expire_date'])); ?></td>
                        <td>
                            <span class="status-badge <?php echo $is_expired ? 'status-expired' : 'status-active'; ?>">
                                <?php echo $is_expired ? 'EXPIRADO' : 'ATIVO'; ?>
                            </span>
                        </td>
                        <td>
                            <div style="display: flex; gap: 10px;">
                                <a href="users_update.php?update=<?php echo $row['id']; ?>" class="action-btn btn-edit" title="Editar">
                                    <i class="fas fa-edit"></i>
                                </a>
                                <a href="users_delete.php?id=<?php echo $row['id']; ?>" class="action-btn btn-delete" title="Excluir" onclick="return confirmDelete(event, this.href)">
                                    <i class="fas fa-trash"></i>
                                </a>
                            </div>
                        </td>
                    </tr>
                    <?php endwhile; ?>

                    <?php if (!$hasRows): ?>
                        <tr>
                            <td colspan="6" style="text-align: center; padding: 50px; color: var(--text-muted);">
                                <i class="fas fa-user-slash" style="font-size: 40px; margin-bottom: 15px; display: block;"></i>
                                Nenhum cliente encontrado.
                            </td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<script>
document.getElementById('userSearch').addEventListener('keyup', function() {
    const searchTerm = this.value.toLowerCase();
    const rows = document.querySelectorAll('.user-row');
    
    rows.forEach(row => {
        const text = row.textContent.toLowerCase();
        row.style.display = text.includes(searchTerm) ? '' : 'none';
    });
});

function confirmDelete(e, url) {
    e.preventDefault();
    Swal.fire({
        title: 'Excluir Cliente?',
        text: "Esta ação não pode ser desfeita!",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#ef4444',
        cancelButtonColor: '#6366f1',
        confirmButtonText: 'Sim, excluir',
        cancelButtonText: 'Cancelar',
        background: '#1e293b',
        color: '#f1f5f9'
    }).then((result) => {
        if (result.isConfirmed) {
            window.location.href = url;
        }
    });
}
</script>

<?php include 'includes/footer.php'; ?>
<?php $db->close(); ?>
