<?php
$file = "/var/www/html/mikrotik/views/device_detail.php";
$content = file_get_contents($file);

// Corrige o redirect do POST handler: troca header() por JS redirect
$old = "    header('Location: ' . NMS_URL . '?page=nms_device_detail&id=' . \$id);
    exit;
}";

$new = "    $url = NMS_URL . '?page=nms_device_detail&id=' . \$id;
    echo '<script>location.href=' . json_encode(\$url) . ';</script>';
    exit;
}";

if (strpos($content, $old) !== false) {
    $content = str_replace($old, $new, $content);
    file_put_contents($file, $content);
    echo "PATCH_OK\n";
} else {
    echo "NAO_ENCONTRADO\n";
    // Mostrar contexto para debug
    preg_match('/header\(\'Location.*?exit;\s*\}/s', $content, $m);
    echo $m[0] ?? "nada";
}
