<?php
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>
    :root {
        --primary: #00BFFF; /* Azul Celeste */
        --primary-dark: #4682B4; /* Azul Metálico */
        --bg-dark: #0a192f;
        --card-bg: rgba(17, 34, 64, 0.7);
        --text-main: #e6f1ff;
        --text-muted: #8892b0;
        --glow: 0 0 15px rgba(0, 191, 255, 0.4);
    }

    .users-container {
        padding: 1.5rem;
        animation: fadeIn 0.5s ease-in-out;
    }

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

    .page-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 2rem;
        flex-wrap: wrap;
        gap: 1rem;
    }

    .page-header h1 {
        font-size: 2rem;
        font-weight: 800;
        background: linear-gradient(90deg, var(--primary), var(--primary-dark));
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
    }

    .search-wrapper {
        position: relative;
        flex: 1;
        max-width: 400px;
        min-width: 250px;
    }

    .search-input {
        width: 100%;
        padding: 0.8rem 1rem 0.8rem 2.8rem;
        background: rgba(17, 34, 64, 0.8);
        border: 1px solid rgba(0, 191, 255, 0.2);
        border-radius: 12px;
        color: var(--text-main);
        transition: all 0.3s ease;
        font-size: 0.95rem;
    }

    .search-input:focus {
        outline: none;
        border-color: var(--primary);
        box-shadow: var(--glow);
        background: rgba(17, 34, 64, 1);
    }

    .search-icon {
        position: absolute;
        left: 1rem;
        top: 50%;
        transform: translateY(-50%);
        color: var(--primary);
        font-size: 1.1rem;
    }

    .btn-add {
        background: linear-gradient(135deg, var(--primary), var(--primary-dark));
        color: white !important;
        padding: 0.8rem 1.5rem;
        border-radius: 12px;
        text-decoration: none !important;
        font-weight: 700;
        transition: all 0.3s ease;
        display: flex;
        align-items: center;
        gap: 8px;
        box-shadow: 0 4px 15px rgba(0, 191, 255, 0.2);
    }

    .btn-add:hover {
        transform: translateY(-2px);
        box-shadow: 0 6px 20px rgba(0, 191, 255, 0.4);
        filter: brightness(1.1);
    }

    .table-card {
        background: var(--card-bg);
        border-radius: 20px;
        border: 1px solid rgba(0, 191, 255, 0.1);
        backdrop-filter: blur(10px);
        overflow: hidden;
    }

    .table-responsive {
        width: 100%;
        overflow-x: auto;
    }

    table {
        width: 100%;
        border-collapse: collapse;
    }

    th {
        background: rgba(0, 191, 255, 0.05);
        color: var(--primary);
        font-size: 0.8rem;
        text-transform: uppercase;
        letter-spacing: 1px;
        padding: 1.2rem 1rem;
        text-align: left;
        border-bottom: 1px solid rgba(0, 191, 255, 0.1);
    }

    td {
        padding: 1rem;
        border-bottom: 1px solid rgba(255, 255, 255, 0.05);
        font-size: 0.9rem;
        color: var(--text-main);
    }

    tr:hover td {
        background: rgba(0, 191, 255, 0.03);
    }

    .mac-code {
        font-family: 'Courier New', Courier, monospace;
        background: rgba(0, 191, 255, 0.1);
        padding: 2px 8px;
        border-radius: 4px;
        color: var(--primary);
    }

    .action-links {
        display: flex;
        gap: 8px;
    }

    .btn-action {
        padding: 6px 12px;
        border-radius: 8px;
        text-decoration: none !important;
        font-size: 0.8rem;
        font-weight: 600;
        transition: all 0.2s ease;
    }

    .btn-edit {
        background: rgba(255, 176, 82, 0.1);
        color: #ffb052;
        border: 1px solid #ffb052;
    }

    .btn-edit:hover {
        background: #ffb052;
        color: #0a192f;
    }

    .btn-delete {
        background: rgba(255, 82, 82, 0.1);
        color: #ff5252;
        border: 1px solid #ff5252;
    }

    .btn-delete:hover {
        background: #ff5252;
        color: white;
    }

    .empty-state {
        text-align: center;
        padding: 4rem 2rem;
        color: var(--text-muted);
    }

    .empty-state i {
        font-size: 3rem;
        margin-bottom: 1rem;
        opacity: 0.5;
    }

    @media (max-width: 768px) {
        .page-header {
            flex-direction: column;
            align-items: stretch;
        }
        .search-wrapper {
            max-width: 100%;
        }
        .btn-add {
            justify-content: center;
        }
    }
</style>

<div class="users-container">
    <div class="page-header">
        <h1><i class="fas fa-users"></i> Gerenciar Clientes</h1>
        
        <div class="search-wrapper">
            <i class="fas fa-search search-icon"></i>
            <input type="text" id="userSearch" class="search-input" placeholder="Pesquisar por nome, MAC ou usuário...">
        </div>

        <a href="users_create.php" class="btn-add">
            <i class="fas fa-plus"></i>
            <span>Novo Cliente</span>
        </a>
    </div>

    <div class="table-card">
        <div class="table-responsive">
            <table id="usersTable">
                <thead>
                    <tr>
                        <th>Cliente</th>
                        <th>MAC Address</th>
                        <th>Usuário</th>
                        <th>Expiração</th>
                        <th>Ações</th>
                    </tr>
                </thead>
                <tbody>
                    <?php 
                    $hasRows = false;
                    while($row = $result->fetchArray(SQLITE3_ASSOC)): 
                        $hasRows = true;
                        $is_expired = strtotime($row['expire_date']) < time();
                    ?>
                    <tr class="user-row">
                        <td><strong><?php echo htmlspecialchars($row['title'] ?? '-'); ?></strong></td>
                        <td><span class="mac-code"><?php echo htmlspecialchars($row['mac_address'] ?? '-'); ?></span></td>
                        <td><?php echo htmlspecialchars($row['username'] ?? '-'); ?></td>
                        <td>
                            <span style="color: <?php echo $is_expired ? '#ff5252' : 'inherit'; ?>">
                                <?php echo date('d/m/Y', strtotime($row['expire_date'])); ?>
                            </span>
                        </td>
                        <td>
                            <div class="action-links">
                                <a href="users_update.php?update=<?php echo $row['id']; ?>" class="btn-action btn-edit">
                                    <i class="fas fa-edit"></i> Editar
                                </a>
                                <a href="users_delete.php?id=<?php echo $row['id']; ?>" class="btn-action btn-delete" onclick="return confirmDelete(event, this.href)">
                                    <i class="fas fa-trash"></i> Excluir
                                </a>
                            </div>
                        </td>
                    </tr>
                    <?php endwhile; ?>

                    <?php if (!$hasRows): ?>
                        <tr>
                            <td colspan="5" class="empty-state">
                                <i class="fas fa-user-slash"></i>
                                <p>Nenhum cliente encontrado.</p>
                                <a href="users_create.php" style="color: var(--primary); text-decoration: underline;">Cadastrar primeiro cliente</a>
                            </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();
        if (text.includes(searchTerm)) {
            row.style.display = '';
        } else {
            row.style.display = 'none';
        }
    });
});

function confirmDelete(e, url) {
    e.preventDefault();
    Swal.fire({
        title: 'Tem certeza?',
        text: "Esta ação não pode ser revertida!",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#ff5252',
        cancelButtonColor: '#4682B4',
        confirmButtonText: 'Sim, excluir!',
        cancelButtonText: 'Cancelar',
        background: '#0a192f',
        color: '#e6f1ff'
    }).then((result) => {
        if (result.isConfirmed) {
            window.location.href = url;
        }
    });
}
</script>

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