<?php
session_start();
include 'auth.php'; 

// Força o fuso no topo por segurança
date_default_timezone_set('America/Sao_Paulo');

$db_path = __DIR__ . '/api/atividades.db';

try {
    $db = new PDO("sqlite:" . $db_path);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $logs = $db->query("SELECT * FROM logs_revenda ORDER BY id DESC LIMIT 100")->fetchAll(PDO::FETCH_ASSOC);
} catch(Exception $e) { $logs = []; }

include ('includes/header.php');
?>

<style>
    body { background-color: #020617 !important; color: #e2e8f0; font-family: 'Inter', sans-serif; }
    .container-logs { padding: 15px; max-width: 700px; margin: 0 auto; }
    .header-section { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; border-bottom: 1px solid #1e293b; padding-bottom: 15px; }
    .logs-toolbar { display: flex; gap: 10px; margin-bottom: 20px; position: sticky; top: 0; z-index: 100; background: #020617; padding: 10px 0; }
    .btn-tool { flex: 1; border: none; padding: 12px; border-radius: 12px; font-size: 0.75rem; font-weight: 800; cursor: pointer; display: flex; align-items: center; justify-content: center; gap: 8px; transition: 0.3s; text-decoration: none !important; }
    .btn-clear { background: rgba(239, 68, 68, 0.1); color: #ef4444 !important; border: 1px solid rgba(239, 68, 68, 0.2); }
    .btn-delete-sel { background: #3b82f6; color: #fff !important; box-shadow: 0 4px 15px rgba(59, 130, 246, 0.3); }
    .log-card { background: #070e1b; border: 1px solid #1e293b; border-radius: 20px; padding: 20px; margin-bottom: 15px; position: relative; }
    .check-container { position: absolute; top: 20px; right: 20px; }
    .log-checkbox { width: 22px; height: 22px; cursor: pointer; accent-color: #3b82f6; }
    .user-badge { color: #3b82f6; font-weight: 800; font-size: 0.8rem; text-transform: uppercase; display: flex; align-items: center; gap: 6px; margin-bottom: 8px;}
    .date-badge { color: #64748b; font-size: 0.75rem; margin-bottom: 10px; display: block;}
    .acao-badge { display: inline-block; padding: 4px 12px; border-radius: 8px; font-size: 0.7rem; font-weight: 800; margin-bottom: 12px; text-transform: uppercase; }
    .bg-green { background: rgba(34, 197, 94, 0.1); color: #22c55e; border: 1px solid rgba(34, 197, 94, 0.2); }
    .bg-red { background: rgba(239, 68, 68, 0.1); color: #ef4444; border: 1px solid rgba(239, 68, 68, 0.2); }
    .bg-blue { background: rgba(59, 130, 246, 0.1); color: #3b82f6; border: 1px solid rgba(59, 130, 246, 0.2); }
    .detalhes-box { background: rgba(15, 23, 42, 0.5); padding: 12px; border-radius: 12px; font-family: monospace; font-size: 0.8rem; color: #94a3b8; word-wrap: break-word; overflow-wrap: break-word; word-break: break-all; line-height: 1.4; border: 1px solid rgba(255,255,255,0.02); }
    .btn-voltar { display: flex; align-items: center; justify-content: center; gap: 8px; margin: 30px auto; color: #64748b; font-weight: 700; text-transform: uppercase; font-size: 0.8rem; text-decoration: none; }
</style>

<div class="container-logs">
    <div class="header-section">
        <h2 style="margin:0; font-size: 1.1rem; font-weight: 800;"><i class="fas fa-terminal" style="color:#3b82f6;"></i> LOGS DE REVENDA</h2>
    </div>

    <form action="processar_logs_rev.php" method="POST">
        <div class="logs-toolbar">
            <button type="button" onclick="confirmarLimpeza()" class="btn-tool btn-clear">
                <i class="fas fa-broom"></i> LIMPAR TUDO
            </button>
            <button type="submit" class="btn-tool btn-delete-sel">
                <i class="fas fa-trash-alt"></i> EXCLUIR MARCADOS
            </button>
        </div>

        <?php if(empty($logs)): ?>
            <div style="text-align:center; padding: 50px; color:#64748b;">Nenhuma atividade recente.</div>
        <?php endif; ?>

        <?php foreach($logs as $l): 
            $cor = "bg-blue";
            $acao_val = strtolower($l['acao']);
            if(strpos($acao_val, 'excluiu') !== false) $cor = "bg-red";
            if(strpos($acao_val, 'cadastrou') !== false || strpos($acao_val, 'renovou') !== false) $cor = "bg-green";

            // Apenas formata a data que já foi salva corrigida no banco
            $data_exibir = date('d/m/Y H:i:s', strtotime($l['data_hora']));
        ?>
        <div class="log-card">
            <div class="check-container">
                <input type="checkbox" name="selected_logs[]" value="<?= $l['id'] ?>" class="log-checkbox">
            </div>
            <span class="user-badge"><i class="fas fa-user-shield"></i> <?= htmlspecialchars($l['usuario']) ?></span>
            <span class="date-badge"><i class="far fa-clock"></i> <?= $data_exibir ?></span>
            <div class="acao-badge <?= $cor ?>"><?= htmlspecialchars($l['acao']) ?></div>
            <div class="detalhes-box"><?= nl2br(htmlspecialchars($l['detalhes'])) ?></div>
        </div>
        <?php endforeach; ?>
    </form>
    <a href="index.php" class="btn-voltar"><i class="fas fa-chevron-left"></i> Voltar ao Painel</a>
</div>

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
function confirmarLimpeza() {
    Swal.fire({
        title: 'Limpar tudo?',
        text: "Isso apagará os logs permanentemente!",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#ef4444',
        cancelButtonColor: '#1e293b',
        confirmButtonText: 'Sim, limpar!',
        cancelButtonText: 'Cancelar',
        background: '#0f172a',
        color: '#fff'
    }).then((result) => { if (result.isConfirmed) window.location.href = 'processar_logs_rev.php?action=clear_all'; })
}
<?php if(isset($_GET['status'])): ?>
    Swal.fire({ icon: 'success', title: 'Sucesso!', showConfirmButton: false, timer: 1500, background: '#0f172a', color: '#fff' });
<?php endif; ?>
</script>

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