<?php
// Patch user_logs.php — insere botao Exportar
$f = "/var/www/html/views/user_logs.php";
$c = file_get_contents($f);

if (strpos($c, 'export-wrap') !== false) { echo "SKIP\n"; exit; }

// Ancora exata no arquivo
$anchor = "    <!-- Filtros -->";
if (strpos($c, $anchor) === false) { echo "ANCHOR_NOT_FOUND\n"; exit; }

$btn = <<<'BTNHTML'
            <!-- Exportar CSV -->
            <div style="position:relative;align-self:flex-end;" id="export-wrap">
                <button id="export-btn"
                    style="display:inline-flex;align-items:center;gap:8px;padding:10px 18px;background:#fff;border:1px solid #e2e8f0;border-radius:10px;font-weight:700;font-size:0.88rem;color:#475569;cursor:pointer;">
                    &#8659; Exportar CSV &#9660;
                </button>
                <div id="export-menu" style="display:none;position:absolute;right:0;top:calc(100% + 6px);background:#fff;border:1px solid #e2e8f0;border-radius:12px;box-shadow:0 8px 24px rgba(0,0,0,.1);min-width:220px;z-index:100;overflow:hidden;">
BTNHTML;

// Adiciona os 3 links dinamicamente (sem aspas simples aninhadas)
$btn .= "                    <?php\n";
$btn .= "                    \$_exportOpts = [7 => '7 dias', 30 => '30 dias', 90 => '90 dias'];\n";
$btn .= "                    foreach (\$_exportOpts as \$_d => \$_lbl):\n";
$btn .= "                        \$_eurl = h('index.php?page=export_logs&ip_id=' . \$ip['id'] . '&days=' . \$_d . '&status=' . \$filterStatus);\n";
$btn .= "                    ?>\n";
$btn .= "                    <a href=\"<?= \$_eurl ?>\" style=\"display:flex;align-items:center;gap:10px;padding:12px 18px;color:#374151;font-size:0.88rem;font-weight:600;text-decoration:none;border-bottom:1px solid #f1f5f9;background:#fff;\" onmouseover=\"this.style.background='#fff7ed'\" onmouseout=\"this.style.background='#fff'\">\n";
$btn .= "                        &#128196; Exportar <?= \$_lbl ?>\n";
$btn .= "                        <?php if (\$filterStatus !== 'all'): ?><span style=\"margin-left:auto;font-size:0.72rem;background:#f1f5f9;padding:2px 6px;border-radius:5px;color:#64748b;\"><?= \$filterStatus ?></span><?php endif; ?>\n";
$btn .= "                    </a>\n";
$btn .= "                    <?php endforeach; ?>\n";

$btn .= <<<'BTNHTML2'
                </div>
            </div>
        </div>
    </div>
<script>
(function(){
    var btn=document.getElementById("export-btn"),menu=document.getElementById("export-menu");
    if(!btn||!menu)return;
    btn.addEventListener("click",function(e){e.stopPropagation();menu.style.display=menu.style.display==="none"?"block":"none";});
    document.addEventListener("click",function(){menu.style.display="none";});
    btn.addEventListener("mouseover",function(){this.style.borderColor="#ea580c";this.style.color="#ea580c";});
    btn.addEventListener("mouseout",function(){this.style.borderColor="#e2e8f0";this.style.color="#475569";});
})();
</script>

BTNHTML2;

// Remove o fechamento existente que fica antes dos Filtros e substitui pelo bloco completo
$oldEnding = "        </div>\n    </div>\n\n    <!-- Filtros -->";
$newEnding = $btn . "    <!-- Filtros -->";

if (strpos($c, $oldEnding) !== false) {
    $c = str_replace($oldEnding, $newEnding, $c);
    file_put_contents($f, $c);
    echo "OK\n";
} else {
    // fallback: insere antes do ancora de filtros
    $c = str_replace($anchor, $btn . $anchor, $c);
    file_put_contents($f, $c);
    echo "OK_FALLBACK\n";
}
