<?php
// Adiciona botao Exportar CSV em user_logs.php
$f = "/var/www/html/views/user_logs.php";
$c = file_get_contents($f);

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

$old = '            <?php endif; ?>
        </div>
    </div>

    <!-- Filtros -->';

$new = '            <?php endif; ?>
            <!-- 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;">
                    <svg width="16" height="16" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"/></svg>
                    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:210px;z-index:100;overflow:hidden;">
                    <?php foreach ([7 => '7 dias', 30 => '30 dias', 90 => '90 dias'] as $d => $lbl):
                        $url = "index.php?page=export_logs&ip_id={$ip['id']}&days={$d}&status={$filterStatus}";
                    ?>
                    <a href="<?= h($url) ?>"
                       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'">
                        &#128196; Exportar <?= $lbl ?>
                        <?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; ?>
                    </a>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
    </div>
<script>
(function(){
    var btn  = document.getElementById("export-btn");
    var 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>

    <!-- Filtros -->';

if (strpos($c, $old) !== false) {
    file_put_contents($f, str_replace($old, $new, $c));
    echo "OK\n";
} else {
    // Tenta ancora alternativa
    echo "FAIL — mostrando contexto:\n";
    preg_match('/endif.*?<\/div>.*?<\/div>.*?Filtros/s', $c, $m);
    echo substr($m[0] ?? 'nada', 0, 200);
}
