<?php
// Silencia avisos para manter o visual limpo
ini_set('display_errors', 0);
error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE);

$db = new SQLite3('a/.eggziedb.db');
$today = date('Y-m-d');

// Lógica de exclusão
if (isset($_GET['delete'])) {
    $db->exec('DELETE FROM ibo WHERE id='.$_GET['delete']);
    header('Location: users.php');
} else if (isset($_GET['delete-expired'])) {
    $results = $db->query('SELECT * FROM ibo');
    while($row = $results->fetchArray()){
        if($row["expire_date"] < $today){
            $db->exec("DELETE FROM ibo WHERE id=" . $row['id']);
        }
    }
    header("Location: users.php");
}

$IIIIIII1IlII = $db->query('SELECT * FROM ibo ORDER by id ASC');

include 'includes/header.php';
?>

<style>
    /* RESET E FUNDO */
    #content-wrapper, #content, .container-fluid, main { background-color: #06070a !important; }

    /* BUSCA COMPACTA */
    .dataTables_filter { width: 100% !important; float: none !important; margin-bottom: 15px !important; }
    .dataTables_filter input {
        background: rgba(255,255,255,0.03) !important;
        border: 1px solid rgba(139, 92, 246, 0.2) !important;
        border-radius: 12px !important;
        color: white !important;
        padding: 12px !important;
        width: 100% !important;
        margin-left: 0 !important;
        font-size: 14px !important;
    }
    .dataTables_length { display: none !important; }

    /* ESTILO MOBILE (TABLE TO CARDS) */
    @media (max-width: 768px) {
        thead { display: none; }
        .container-fluid { padding: 0 10px !important; }
        
        tr {
            display: block;
            background: #0f111a;
            margin-bottom: 15px;
            border-radius: 20px;
            padding: 16px;
            border: 1px solid rgba(255,255,255,0.05) !important;
        }

        td {
            display: flex;
            justify-content: space-between;
            align-items: center;
            border: none !important;
            padding: 6px 0 !important;
            font-size: 12px !important;
        }

        td::before {
            content: attr(data-label);
            font-weight: 800;
            text-transform: uppercase;
            font-size: 9px;
            color: #8b5cf6;
            letter-spacing: 1px;
        }

        .flex-actions { 
            width: 100%; 
            justify-content: center !important; 
            gap: 20px !important;
            margin-top: 10px; 
            padding-top: 12px;
            border-top: 1px solid rgba(255,255,255,0.05); 
        }
        
        .btn-action { width: 45px; height: 45px; display: flex; items-center; justify-center; border-radius: 12px; }
    }
</style>

<main class="py-4 px-2">
    <div class="flex flex-col items-center mb-6 gap-4">
        <div class="text-center">
            <h1 class="text-xl font-black text-white tracking-[0.2em] uppercase italic">Usuários</h1>
            <p class="text-[9px] text-gray-500 font-bold tracking-widest uppercase">Vu Flux Management</p>
        </div>
        
        <div class="flex gap-2 w-full max-w-sm">
            <a href="./users_create.php" class="flex-1 flex items-center justify-center bg-violet-600 text-white font-black py-3 rounded-xl text-[10px] uppercase tracking-wider">
                <i class="fas fa-plus mr-2 text-xs"></i> Novo
            </a>
            <a href="#" data-href="users.php?delete-expired" data-toggle="modal" data-target="#confirm-delete" class="flex-1 flex items-center justify-center bg-red-500/10 text-red-500 border border-red-500/20 font-black py-3 rounded-xl text-[10px] uppercase tracking-wider">
                <i class="fas fa-trash-alt mr-2 text-xs"></i> Limpar
            </a>
        </div>
    </div>

    <div class="md:bg-[#0f111a] md:rounded-[2rem] md:p-6">
        <table class="table w-full border-none" id="Dtable">
            <thead>
                <tr class="text-[10px] font-black text-violet-400 uppercase tracking-widest border-b border-white/5">
                    <th>CLIENTE</th>
                    <th>MAC</th>
                    <th class="text-center">VENCIMENTO</th>
                    <th class="text-center">STATUS</th>
                    <th class="text-center">AÇÕES</th>
                </tr>
            </thead>
            <tbody class="text-sm">
                <?php while ($row = $IIIIIII1IlII->fetchArray()): 
                    $expired = $row['expire_date'] < $today;
                ?>
                <tr class="hover:bg-white/[0.02] transition-all">
                    <td data-label="Cliente" class="text-white font-black uppercase"><?php echo $row['title']; ?></td>
                    <td data-label="MAC" class="font-mono text-gray-400 text-[11px]"><?php echo $row['mac_address']; ?></td>
                    <td data-label="Vencimento" class="text-center text-gray-400"><?php echo date('d/m/Y', strtotime($row['expire_date'])); ?></td>
                    <td data-label="Status" class="text-center">
                        <span class="<?php echo $expired ? 'text-red-500 bg-red-500/10' : 'text-emerald-500 bg-emerald-500/10'; ?> text-[9px] font-black px-3 py-1 rounded-md uppercase">
                            <?php echo $expired ? 'Expirado' : 'Ativo'; ?>
                        </span>
                    </td>
                    <td data-label="Gerenciar">
                        <div class="flex gap-3 flex-actions">
                            <a href="./users_update.php?update=<?php echo $row['id']; ?>" class="btn-action bg-blue-500/10 text-blue-400 flex items-center justify-center"><i class="fas fa-edit"></i></a>
                            <a href="#" data-href="./users.php?delete=<?php echo $row['id']; ?>" data-toggle="modal" data-target="#confirm-delete" class="btn-action bg-red-500/10 text-red-400 flex items-center justify-center"><i class="fas fa-trash"></i></a>
                        </div>
                    </td>
                </tr>
                <?php endwhile; ?>
            </tbody>
        </table>
    </div>
</main>

<div class="modal fade" id="confirm-delete" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered px-6">
        <div class="modal-content bg-[#0a0b14] border border-white/10 rounded-[2rem] p-6 text-white text-center">
            <div class="text-red-500 mb-4"><i class="fas fa-exclamation-circle fa-3x"></i></div>
            <h4 class="text-sm font-black uppercase mb-2">Excluir Registro?</h4>
            <p class="text-[10px] text-gray-500 uppercase font-bold mb-6">Esta ação não pode ser desfeita.</p>
            <div class="flex gap-3">
                <button type="button" class="flex-1 py-3 text-[10px] font-black text-gray-500 uppercase" data-dismiss="modal">Sair</button>
                <a class="btn-ok flex-1 py-3 bg-red-600 rounded-xl text-[10px] font-black text-white uppercase">Apagar</a>
            </div>
        </div>
    </div>
</div>

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

<script>
$(document).ready(function() {
    if ($.fn.DataTable.isDataTable('#Dtable')) { $('#Dtable').DataTable().destroy(); }
    $('#Dtable').DataTable({
        "paging": true, "ordering": true, "info": false, "searching": true,
        "language": { "search": "", "searchPlaceholder": "Pesquisar por Cliente ou MAC..." }
    });
});
</script>