<?php
// ── 1. Adiciona rota export_logs_pdf no index.php ──────────────────────────
$f1 = "/var/www/html/index.php";
$c1 = file_get_contents($f1);
$old1 = "    case 'export_logs':
        require __DIR__ . '/views/export_logs.php';";
$new1 = "    case 'export_logs':
        require __DIR__ . '/views/export_logs.php';
        break;

    case 'export_logs_pdf':
        require __DIR__ . '/views/export_logs_pdf.php';";
if (strpos($c1, 'export_logs_pdf') !== false) {
    echo "SKIP index.php\n";
} elseif (strpos($c1, $old1) !== false) {
    file_put_contents($f1, str_replace($old1, $new1, $c1));
    echo "OK index.php\n";
} else {
    echo "FAIL index.php\n";
}

// ── 2. Adiciona opcoes PDF no dropdown do user_logs.php ────────────────────
$f2 = "/var/www/html/views/user_logs.php";
$c2 = file_get_contents($f2);

// Âncora: linha divisória antes do endforeach do dropdown
$old2 = "                    <?php endforeach; ?>\n                </div>\n            </div>";
$new2 = "                    <?php endforeach; ?>\n";
// Adiciona separador + opcoes PDF
$new2 .= "                    <div style=\"padding:6px 18px;font-size:9px;font-weight:700;text-transform:uppercase;letter-spacing:.05em;color:#94a3b8;background:#f8fafc;border-top:1px solid #e2e8f0;border-bottom:1px solid #e2e8f0;\">PDF</div>\n";
$new2 .= "                    <?php\n";
$new2 .= "                    \$_pdfOpts = [7 => '7 dias', 30 => '30 dias', 90 => '90 dias'];\n";
$new2 .= "                    foreach (\$_pdfOpts as \$_pd => \$_pl):\n";
$new2 .= "                        \$_purl = h('index.php?page=export_logs_pdf&ip_id=' . \$ip['id'] . '&days=' . \$_pd . '&status=' . \$filterStatus);\n";
$new2 .= "                    ?>\n";
$new2 .= "                    <a href=\"<?= \$_purl ?>\" target=\"_blank\" 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='#fef3c7'\" onmouseout=\"this.style.background='#fff'\">\n";
$new2 .= "                        &#128196; PDF <?= \$_pl ?>\n";
$new2 .= "                        <?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";
$new2 .= "                    </a>\n";
$new2 .= "                    <?php endforeach; ?>\n";
$new2 .= "                </div>\n            </div>";

if (strpos($c2, 'export_logs_pdf') !== false) {
    echo "SKIP user_logs.php\n";
} elseif (strpos($c2, $old2) !== false) {
    file_put_contents($f2, str_replace($old2, $new2, $c2));
    echo "OK user_logs.php\n";
} else {
    echo "FAIL user_logs.php — ancora: " . (strpos($c2, 'endforeach') !== false ? 'endforeach ok' : 'sem endforeach') . "\n";
}

echo "DONE\n";
