<template>
  <div class="app-shell">
    <div class="bg-orbs" aria-hidden="true">
      <div class="bg-orb bg-orb-a" />
      <div class="bg-orb bg-orb-b" />
      <div class="bg-orb bg-orb-c" />
      <div class="bg-orb bg-orb-d" />
    </div>

    <el-container class="app-container">
      <el-aside :width="ui.sidebarCollapsed ? '76px' : '240px'" class="aside">
        <div class="logo" :class="{ collapsed: ui.sidebarCollapsed }">
          <template v-if="ui.sidebarCollapsed">
            <img src="/FRLD-logo-white-no_title.png" alt="FRLD" class="logo-img-small" />
            <span class="logo-abbr">迷毂</span>
          </template>
          <template v-else>
            <img src="/FRLD-logo-white.png" alt="FRLD" class="logo-img-full" />
            <div class="logo-titles">
              <span class="logo-title-zh">迷 毂 · 智能调度平台</span>
              <span class="logo-title-en">
                <b>M</b>i <b>G</b>u · <b>I</b>ntelligent <b>D</b>ispatch <b>P</b>latform
              </span>
            </div>
          </template>
        </div>

        <el-menu
          :default-active="activePath"
          :collapse="ui.sidebarCollapsed"
          :collapse-transition="false"
          background-color="transparent"
          text-color="rgba(255, 255, 255, 0.72)"
          active-text-color="#fff"
          router
          class="menu">
          <template v-for="m in currentMenu" :key="m.path">
            <el-sub-menu v-if="m.children" :index="m.path">
              <template #title>
                <el-icon><component :is="m.icon" /></el-icon>
                <span>{{ m.label }}</span>
              </template>
              <el-menu-item v-for="c in m.children" :key="c.path" :index="c.path">
                <span>{{ c.label }}</span>
              </el-menu-item>
            </el-sub-menu>
            <el-menu-item v-else :index="m.path">
              <el-icon><component :is="m.icon" /></el-icon>
              <template #title>{{ m.label }}</template>
            </el-menu-item>
          </template>
        </el-menu>

        <div class="aside-footer">
          <span class="aside-footer-badge">v1.6</span>
          <span v-if="!ui.sidebarCollapsed">{{ ui.activeTheme.name }}</span>
        </div>
      </el-aside>

      <el-container class="container-right">
        <el-header class="header">
          <div class="header-left">
            <button class="icon-btn" @click="ui.toggleSidebar()" title="切换侧栏">
              <el-icon size="18"><Fold v-if="!ui.sidebarCollapsed" /><Expand v-else /></el-icon>
            </button>
            <el-breadcrumb separator="/">
              <el-breadcrumb-item>{{ scopeLabel }}</el-breadcrumb-item>
              <el-breadcrumb-item v-if="currentTitle">{{ currentTitle }}</el-breadcrumb-item>
            </el-breadcrumb>
          </div>
          <div class="header-right">
            <ThemeSwitcher />
            <ScopeSwitcher />
            <span class="runmode-tag" :class="auth.runMode">{{ runModeLabel }}</span>
            <el-dropdown trigger="click" @command="onUserCommand">
              <span class="user-link">
                <el-avatar size="small">{{ initial }}</el-avatar>
                <span class="user-name">{{ auth.user?.displayName ?? '未登录' }}</span>
                <el-icon><CaretBottom /></el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item v-if="auth.scope === 'Platform'" command="wizard">配置向导</el-dropdown-item>
                  <el-dropdown-item command="status">服务状态</el-dropdown-item>
                  <el-dropdown-item divided disabled class="legacy-theme-label">高级 · 兼容主题</el-dropdown-item>
                  <el-dropdown-item
                    v-for="t in ui.legacyThemes"
                    :key="t.id"
                    :command="`theme:${t.id}`"
                    :class="{ 'is-legacy-active': t.id === ui.themeId }">
                    <span class="legacy-swatch" :style="{ background: t.preview }" />
                    {{ t.name }}
                  </el-dropdown-item>
                  <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </el-header>

        <el-main class="main mg-content">
          <!-- 不用 mode="out-in"：懒加载子路由切换时易出现「离场后新组件未挂载」→ 主区全白，刷新才恢复。 -->
          <router-view v-slot="{ Component, route: rv }">
            <component :is="Component" v-if="Component" :key="rv.fullPath" class="route-page" />
          </router-view>
        </el-main>

        <el-footer class="footer">
          <span>{{ scopeLabel }} · Projection :{{ slPort }} · API {{ apiBase }}</span>
        </el-footer>
      </el-container>
    </el-container>

    <!-- 全局 AI 助手：右侧抽屉 + 右下角悬浮唤起，所有界面可呼出。后端要求 Platform 权限，故按 scope 显示。 -->
    <AiAssistantDrawer v-if="auth.scope === 'Platform'" />
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { Fold, Expand, CaretBottom } from '@element-plus/icons-vue'
import { ADMIN_MENU, MONITOR_MENU, type NavMenuItem } from '@/config/navMenu'
import { useAuthStore } from '@/stores/auth'
import { useUiStore } from '@/stores/ui'
import ScopeSwitcher from '@/components/ScopeSwitcher.vue'
import ThemeSwitcher from '@/components/ThemeSwitcher.vue'
import AiAssistantDrawer from '@/components/assistant/AiAssistantDrawer.vue'

const route = useRoute()
const router = useRouter()
const auth = useAuthStore()
const ui = useUiStore()

const slPort = computed(() => (import.meta.env.VITE_SL_PORT as string | undefined) ?? '8222')
const apiBase = computed(() => (import.meta.env.VITE_API_BASE as string | undefined) ?? '/api')

const initial = computed(() => (auth.user?.displayName ?? auth.user?.username ?? '?').slice(0, 1).toUpperCase())

const scopeLabel = computed(() => (auth.scope === 'Platform' ? '管理员 (Platform)' : '运营 (RCSMonitor)'))

const runModeLabel = computed(() => {
  switch (auth.runMode) {
    case 'WebOnly': return 'Web-Only'
    case 'WebEnabled': return 'Web + Desktop'
    case 'Detached': return 'SimpleLite 未连接'
    default: return String(auth.runMode ?? 'Unknown')
  }
})

// key 即「权限页面 Key」（= vue-router route.name），用于按 auth.allowedPages 过滤菜单。
// 分组节点（无 key）只要还有可见子项就保留。
type MenuItem = NavMenuItem

function filterMenu(items: MenuItem[]): MenuItem[] {
  const out: MenuItem[] = []
  for (const m of items) {
    if (m.children) {
      const kids = m.children.filter((c) => !c.key || auth.hasPage(c.key))
      if (kids.length) out.push({ ...m, children: kids })
    } else if (!m.key || auth.hasPage(m.key)) {
      out.push(m)
    }
  }
  return out
}

const currentMenu = computed<MenuItem[]>(() => filterMenu(auth.scope === 'Platform' ? ADMIN_MENU : MONITOR_MENU))

const activePath = computed(() => {
  const path = route.path
  const flat: MenuItem[] = []
  const walk = (items: MenuItem[]) => {
    items.forEach((i) => {
      flat.push(i)
      if (i.children) walk(i.children)
    })
  }
  walk(currentMenu.value)
  const hit = flat.find((i) => i.path === path)
  return hit?.path ?? path
})

const currentTitle = computed(() => (route.meta.title as string | undefined) ?? '')

function onUserCommand(cmd: string) {
  if (cmd.startsWith('theme:')) {
    ui.setThemeId(cmd.slice('theme:'.length))
    return
  }
  if (cmd === 'logout') {
    auth.logout()
    router.push('/login')
  } else if (cmd === 'status') {
    router.push('/status')
  } else if (cmd === 'wizard') {
    router.push('/wizard')
  }
}
</script>

<style scoped>
/* ─────────────── App shell ─────────────── */
.app-shell {
  position: relative;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

.bg-orbs {
  position: absolute; inset: 0; pointer-events: none; z-index: 0;
}
.bg-orb {
  position: absolute; border-radius: 50%;
  filter: blur(120px);
  animation: drift 24s ease-in-out infinite;
  will-change: transform;
}
.bg-orb-a {
  width: 520px; height: 520px;
  top: -240px; left: -220px;
  background: radial-gradient(circle, var(--mg-primary-hover) 0%, var(--mg-primary) 55%, transparent 85%);
  opacity: var(--mg-orb-a-opacity, 0.55);
}
.bg-orb-b {
  width: 640px; height: 640px;
  bottom: -280px; right: -260px;
  background: radial-gradient(circle, var(--mg-primary) 0%, var(--mg-primary-active) 55%, transparent 90%);
  opacity: var(--mg-orb-b-opacity, 0.50);
  animation-delay: -7s;
}
.bg-orb-c {
  width: 320px; height: 320px;
  top: 58%; right: -140px;
  background: radial-gradient(circle, var(--mg-accent) 0%, var(--mg-primary-hover) 55%, transparent 95%);
  opacity: var(--mg-orb-c-opacity, 0.40);
  animation-delay: -14s;
}
.bg-orb-d {
  width: 380px; height: 380px;
  top: 12%; right: 28%;
  background: radial-gradient(circle, rgba(99, 102, 241, 0.85) 0%, var(--mg-primary) 60%, transparent 92%);
  opacity: 0.30;
  animation-delay: -18s;
  filter: blur(140px);
}
@keyframes drift {
  0%, 100% { transform: translate(0, 0) scale(1); }
  50%      { transform: translate(36px, -42px) scale(1.08); }
}

.app-container {
  position: relative;
  z-index: 1;
  height: 100vh;
  height: 100dvh;
}

/* ─────────────── Sidebar ─────────────── */
.aside {
  position: relative;
  background: linear-gradient(180deg, rgba(var(--mg-bg-aside-rgb), 0.78) 0%, rgba(var(--mg-bg-app-deep-rgb), 0.85) 100%);
  backdrop-filter: blur(24px) saturate(170%);
  -webkit-backdrop-filter: blur(24px) saturate(170%);
  border-right: 1px solid rgba(var(--mg-accent-rgb), 0.18);
  display: flex;
  flex-direction: column;
  transition: width .28s cubic-bezier(.25, .8, .25, 1);
  overflow: hidden;
  flex-shrink: 0;
  box-shadow:
    inset -1px 0 0 rgba(255, 255, 255, 0.04),
    4px 0 32px rgba(0, 0, 0, 0.45);
}
.aside::before {
  content: '';
  position: absolute;
  top: 0; right: 0;
  width: 1px; height: 100%;
  background: linear-gradient(180deg,
    transparent 0%,
    rgba(var(--mg-accent-rgb), 0.6) 50%,
    transparent 100%);
  pointer-events: none;
  opacity: 0.4;
}

.logo {
  flex-shrink: 0;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 22px 14px 18px;
  border-bottom: 1px solid rgba(var(--mg-accent-rgb), 0.16);
  background:
    radial-gradient(ellipse at center top, rgba(var(--mg-primary-hover-rgb), 0.30) 0%, transparent 70%),
    linear-gradient(180deg, rgba(var(--mg-primary-rgb), 0.16) 0%, transparent 100%);
}
.logo.collapsed { padding: 18px 4px 14px; }
.logo-img-full {
  height: 36px;
  margin-bottom: 12px;
  filter: drop-shadow(0 2px 10px rgba(var(--mg-accent-rgb), 0.5));
}
.logo-img-small {
  height: 26px;
  margin-bottom: 4px;
  filter: drop-shadow(0 2px 8px rgba(var(--mg-accent-rgb), 0.5));
}
.logo-titles { display: flex; flex-direction: column; align-items: center; gap: 3px; }
.logo-title-zh {
  font-size: 13px; font-weight: 700;
  letter-spacing: 2.5px;
  background: linear-gradient(90deg, #ffffff 0%, var(--mg-accent) 60%, #ffffff 100%);
  -webkit-background-clip: text; background-clip: text; color: transparent;
  white-space: nowrap;
  text-shadow: 0 0 18px rgba(var(--mg-accent-rgb), 0.3);
}
.logo-title-en {
  color: rgba(232, 215, 245, 0.45);
  font-size: 7.5px;
  letter-spacing: 0.3px;
  white-space: nowrap;
  text-align: center;
}
.logo-title-en b { color: rgba(255, 255, 255, 0.9); font-weight: 700; font-size: 8.5px; }
.logo-abbr {
  color: rgba(255, 255, 255, 0.85);
  font-size: 10.5px;
  font-weight: 700;
  letter-spacing: 1.5px;
}

/* Menu */
.menu {
  flex: 1; overflow-y: auto; overflow-x: hidden;
  border-right: none !important;
  padding: 8px 0;
  scrollbar-width: thin;
  scrollbar-color: rgba(var(--mg-accent-rgb), 0.30) transparent;
}
.menu::-webkit-scrollbar { width: 4px; }
.menu::-webkit-scrollbar-thumb {
  background: rgba(var(--mg-accent-rgb), 0.25);
  border-radius: 2px;
}
.menu::-webkit-scrollbar-thumb:hover {
  background: rgba(var(--mg-accent-rgb), 0.55);
}
:deep(.el-menu) { border-right: none; background: transparent !important; }
:deep(.el-menu-item),
:deep(.el-sub-menu__title) {
  background: transparent !important;
  margin: 2px 10px;
  border-radius: 10px;
  height: 42px;
  line-height: 42px;
  transition: background .2s ease, color .2s ease, box-shadow .2s ease;
}
:deep(.el-menu--collapse .el-menu-item),
:deep(.el-menu--collapse .el-sub-menu__title) {
  margin: 2px 8px;
  padding: 0 !important;
  justify-content: center;
}
:deep(.el-menu-item:hover),
:deep(.el-sub-menu__title:hover) {
  background: rgba(var(--mg-primary-rgb), 0.20) !important;
  color: #fff !important;
  box-shadow: inset 0 0 0 1px rgba(var(--mg-accent-rgb), 0.18);
}
/* active 菜单：减噪版本（单一 left-border + 一层柔和 bg + 字重 600，不再叠 5 层 shadow） */
:deep(.el-menu-item.is-active) {
  background: linear-gradient(90deg,
    rgba(var(--mg-primary-rgb), 0.55) 0%,
    rgba(var(--mg-primary-hover-rgb), 0.20) 70%,
    transparent 100%) !important;
  color: #fff !important;
  font-weight: 600;
  box-shadow:
    inset 3px 0 0 var(--mg-accent),
    0 4px 14px rgba(var(--mg-primary-rgb), 0.25);
}
:deep(.el-sub-menu.is-active > .el-sub-menu__title) {
  color: #fff !important;
  font-weight: 600;
}
:deep(.el-sub-menu .el-menu) {
  background: rgba(0, 0, 0, 0.18) !important;
  border-radius: 8px;
  margin: 0 12px;
  padding: 4px 0;
}
:deep(.el-sub-menu .el-menu .el-menu-item) {
  margin: 1px 4px;
  border-radius: 6px;
  font-size: 13px;
  height: 36px;
  line-height: 36px;
}
:deep(.el-sub-menu .el-menu .el-menu-item.is-active) {
  background: rgba(var(--mg-accent-rgb), 0.18) !important;
  box-shadow: inset 2px 0 0 var(--mg-primary-hover);
  color: #fff !important;
}

/* Sidebar footer */
.aside-footer {
  flex-shrink: 0;
  padding: 12px 16px;
  border-top: 1px solid rgba(var(--mg-accent-rgb), 0.14);
  display: flex; align-items: center; gap: 8px;
  font-size: 10.5px;
  color: rgba(255, 255, 255, 0.55);
  letter-spacing: 0.4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.aside-footer-badge {
  padding: 2px 7px;
  border-radius: 8px;
  background: linear-gradient(135deg, rgba(var(--mg-primary-rgb), 0.35), rgba(var(--mg-accent-rgb), 0.20));
  border: 1px solid rgba(var(--mg-accent-rgb), 0.36);
  color: #fff;
  font-weight: 600;
  font-size: 10px;
  font-family: var(--mg-font-mono);
  font-variant-numeric: tabular-nums;
  letter-spacing: 0;
  box-shadow: 0 0 12px rgba(var(--mg-primary-hover-rgb), 0.35);
}

/* ─────────────── Header ─────────────── */
.container-right {
  background: transparent;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
.header {
  position: relative;
  display: flex; justify-content: space-between; align-items: center;
  background: linear-gradient(180deg,
    rgba(var(--mg-bg-aside-rgb), 0.72) 0%,
    rgba(var(--mg-bg-aside-rgb), 0.55) 100%);
  backdrop-filter: blur(24px) saturate(170%);
  -webkit-backdrop-filter: blur(24px) saturate(170%);
  border-bottom: 1px solid rgba(var(--mg-accent-rgb), 0.18);
  padding: 0 20px;
}
.header::after {
  content: '';
  position: absolute;
  left: 0; right: 0; bottom: 0;
  height: 1px;
  background: linear-gradient(90deg,
    transparent 0%,
    rgba(var(--mg-accent-rgb), 0.45) 30%,
    rgba(var(--mg-primary-hover-rgb), 0.6) 50%,
    rgba(var(--mg-accent-rgb), 0.45) 70%,
    transparent 100%);
  opacity: 0.55;
}
.header-left, .header-right { display: flex; align-items: center; gap: 14px; }
.icon-btn {
  appearance: none;
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.18);
  color: rgba(255, 255, 255, 0.75);
  border-radius: 8px;
  width: 32px; height: 32px;
  display: inline-flex; align-items: center; justify-content: center;
  cursor: pointer;
  transition: all .2s;
}
.icon-btn:hover {
  background: rgba(var(--mg-accent-rgb), 0.18);
  border-color: rgba(var(--mg-accent-rgb), 0.5);
  color: #fff;
}
:deep(.el-breadcrumb__inner) { color: rgba(255, 255, 255, 0.78); font-weight: 500; }
:deep(.el-breadcrumb__separator) { color: rgba(255, 255, 255, 0.35); }
:deep(.el-breadcrumb__item:last-child .el-breadcrumb__inner) { color: #fff; font-weight: 600; }

.runmode-tag {
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 11.5px;
  letter-spacing: 0.5px;
  font-family: var(--mg-font-mono);
  font-variant-numeric: tabular-nums;
  border: 1px solid rgba(255, 255, 255, 0.18);
  background: rgba(255, 255, 255, 0.05);
  color: rgba(255, 255, 255, 0.85);
}
.runmode-tag.WebEnabled {
  background: rgba(var(--mg-status-success-rgb), 0.18);
  border-color: rgba(var(--mg-status-success-rgb), 0.45);
  color: var(--mg-status-success);
}
.runmode-tag.WebOnly {
  background: rgba(var(--mg-primary-hover-rgb), 0.18);
  border-color: rgba(var(--mg-primary-hover-rgb), 0.45);
  color: var(--mg-accent);
}
/* 会话 N+1 增量：SimpleLite 未拉起时的降级展示，用警示色而非成功色，引导用户感知"实际只有 Platform"。 */
.runmode-tag.Detached {
  background: rgba(var(--mg-status-warning-rgb), 0.18);
  border-color: rgba(var(--mg-status-warning-rgb), 0.45);
  color: var(--mg-status-warning);
}

.user-link {
  display: inline-flex; align-items: center; gap: 8px;
  cursor: pointer; color: rgba(255, 255, 255, 0.85);
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(255, 255, 255, 0.04);
  transition: all .2s;
}
.user-link:hover {
  background: rgba(var(--mg-accent-rgb), 0.18);
  border-color: rgba(var(--mg-accent-rgb), 0.4);
}
.user-name { font-size: 13px; color: #fff; font-weight: 500; }
:deep(.el-avatar) {
  background: linear-gradient(135deg, var(--mg-accent) 0%, var(--mg-primary) 60%, var(--mg-primary-active) 100%);
  color: #fff;
  font-weight: 700;
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.2);
}

/* ─────────────── Main / Footer ─────────────── */
.main {
  flex: 1;
  min-height: 0;
  overflow: auto;
  display: flex;
  flex-direction: column;
  padding: 24px 24px;
  background: transparent;
  color: #fff;
}
.footer {
  background: rgba(var(--mg-bg-aside-rgb), 0.25);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid rgba(255, 255, 255, 0.06);
  display: flex; align-items: center; justify-content: center;
  color: rgba(255, 255, 255, 0.55);
  font-size: 12px;
  font-family: var(--mg-font-mono);
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.3px;
  height: 32px !important; line-height: 32px;
}

.route-page {
  min-height: 0;
  flex: 1;
  display: flex;
  flex-direction: column;
}
</style>

<style>
/* 用户菜单「兼容主题」项（teleport 到 body） */
.legacy-theme-label {
  font-size: 11px !important;
  opacity: 0.65;
  cursor: default !important;
}
.legacy-swatch {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-right: 8px;
  vertical-align: middle;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.12);
}
.el-dropdown-menu__item.is-legacy-active {
  color: var(--mg-primary) !important;
  font-weight: 600;
}
</style>
