<?php
/**
 * 按域名返回启用的菜单选项（JSON）
 * 仅登录管理员可访问
 */
require __DIR__ . '/../../app/bootstrap.php';
require __DIR__ . '/../../app/database.php';
require_login();

header('Content-Type: application/json; charset=utf-8');
header('Cache-Control: no-store');

$domainId = isset($_GET['domain_id']) ? (int)$_GET['domain_id'] : 0;
$items = array();
$error = '';

if ($domainId > 0) {
    try {
        $menus = list_enabled_menu_options($domainId);
        foreach ($menus as $menu) {
            $items[] = array(
                'id' => (int)$menu['id'],
                'menu_name' => isset($menu['menu_name']) ? (string)$menu['menu_name'] : '',
                'menu_slug' => isset($menu['menu_slug']) ? (string)$menu['menu_slug'] : '',
                'is_home' => !empty($menu['is_home']) ? 1 : 0
            );
        }
    } catch (Exception $e) {
        $error = $e->getMessage();
    }
}

echo json_encode(array(
    'ok' => $error === '',
    'error' => $error,
    'items' => $items
), JSON_UNESCAPED_UNICODE);
