<?php
require_once __DIR__ . '/../auth.php';
if (function_exists('require_login')) require_login('../login.php');
if (function_exists('require_perm')) @require_perm('can_view_subscribers');
require_once __DIR__ . '/../db_bootstrap.php';
require_once __DIR__ . '/../router_port_match_lib.php';
if (function_exists('ensure_db')) ensure_db(true);
$conn = $GLOBALS['conn'] ?? null;
header('Content-Type: application/json; charset=utf-8');
if (!($conn instanceof mysqli)) {
    http_response_code(500);
    echo json_encode(['ok'=>false,'message'=>'DB not ready'], JSON_UNESCAPED_UNICODE);
    exit;
}
$routerId = (int)($_GET['router_id'] ?? 0);
try {
    if ($routerId <= 0) {
        echo json_encode(['ok'=>true,'routers'=>ua_rpm_router_list($conn)], JSON_UNESCAPED_UNICODE);
        exit;
    }
    echo json_encode(ua_rpm_list_interfaces($conn, $routerId), JSON_UNESCAPED_UNICODE);
} catch (Throwable $e) {
    http_response_code(500);
    echo json_encode(['ok'=>false,'message'=>$e->getMessage()], JSON_UNESCAPED_UNICODE);
}
