<script setup lang="ts">
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'

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

// 实走监控入口需要 aid，从模拟结果页进入；侧栏保持分组激活即可
const activeMenu = computed(() => {
  const p = route.path
  if (p.startsWith('/paper')) return p
  return p
})

// 顶栏动态页名（按路由推断，替代静态口号占位）
const PAGE_TITLE: Array<{ match: RegExp; group: string; title: string }> = [
  { match: /^\/dashboard$/, group: '工作台', title: '工作台' },
  { match: /^\/backtest\/new$/, group: '回测', title: '新建回测' },
  { match: /^\/backtest\/portfolio$/, group: '回测', title: '组合回测' },
  { match: /^\/backtest\/optimize$/, group: '回测', title: '参数优化' },
  { match: /^\/backtest\/history$/, group: '回测', title: '历史任务' },
  { match: /^\/backtest\/progress\//, group: '回测', title: '任务进度' },
  { match: /^\/backtest\/result\//, group: '回测', title: '回测结果' },
  { match: /^\/factor\/new$/, group: '投研', title: '因子分析' },
  { match: /^\/factor\/progress\//, group: '投研', title: '因子分析进度' },
  { match: /^\/factor\/result\//, group: '投研', title: '因子分析结果' },
  { match: /^\/paper\/new$/, group: '模拟', title: '新建模拟盘' },
  { match: /^\/paper\/result\//, group: '模拟', title: '模拟盘结果' },
  { match: /^\/paper\/live\//, group: '模拟', title: '实走监控' },
  { match: /^\/paper$/, group: '模拟', title: '模拟交易' },
  { match: /^\/live\/new$/, group: '实盘模拟', title: '新建实盘' },
  { match: /^\/live\/monitor\//, group: '实盘模拟', title: '实盘监控' },
  { match: /^\/live$/, group: '实盘模拟', title: '实盘模拟' },
]
const pageMeta = computed(() => {
  const p = route.path
  return PAGE_TITLE.find((r) => r.match.test(p)) ?? { group: '', title: '控制台' }
})

function navigate(path: string): void {
  router.push(path)
}

function onLogout(): void {
  auth.logout()
  router.push('/login')
}
</script>

<template>
  <el-container class="layout">
    <aside class="sidebar">
      <div class="logo">
        <span class="logo-mark">SG</span>
        <span class="logo-text">三国量化</span>
      </div>
      <el-menu :default-active="activeMenu" class="nav" @select="navigate">
        <el-menu-item index="/dashboard"><span class="nav-label">工作台</span></el-menu-item>
        <el-sub-menu index="backtest">
          <template #title>
            <span class="nav-label">回测</span>
          </template>
          <el-menu-item index="/backtest/new">新建回测</el-menu-item>
          <el-menu-item index="/backtest/portfolio">组合回测</el-menu-item>
          <el-menu-item index="/backtest/optimize">参数优化</el-menu-item>
          <el-menu-item index="/backtest/history">历史任务</el-menu-item>
        </el-sub-menu>

        <el-sub-menu index="factor">
          <template #title>
            <span class="nav-label">投研</span>
          </template>
          <el-menu-item index="/factor/new">因子分析</el-menu-item>
        </el-sub-menu>

        <el-sub-menu index="paper">
          <template #title>
            <span class="nav-label">模拟</span>
          </template>
          <el-menu-item index="/paper">模拟盘列表</el-menu-item>
          <el-menu-item index="/paper/new">新建模拟盘</el-menu-item>
        </el-sub-menu>

        <el-sub-menu index="live">
          <template #title>
            <span class="nav-label">实盘模拟</span>
          </template>
          <el-menu-item index="/live">实盘列表</el-menu-item>
          <el-menu-item index="/live/new">新建实盘</el-menu-item>
        </el-sub-menu>
      </el-menu>
      <div class="sidebar-foot">v2 · 量化研究台</div>
    </aside>

    <el-container direction="vertical">
      <header class="header">
        <div class="sys-crumb">
          <span v-if="pageMeta.group" class="sys-group">{{ pageMeta.group }}</span>
          <span v-if="pageMeta.group" class="sys-sep">/</span>
          <span class="sys-name">{{ pageMeta.title }}</span>
        </div>
        <div class="user-area">
          <span class="user-avatar">{{ (auth.username ?? '?').slice(0, 1).toUpperCase() }}</span>
          <span class="user-name">{{ auth.username }}</span>
          <span class="sep">|</span>
          <el-button link type="primary" @click="onLogout">登出</el-button>
        </div>
      </header>
      <el-main class="main">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<style scoped>
.layout { height: 100vh; }

.sidebar {
  width: var(--sidebar-w);
  background: var(--bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}
.logo {
  height: var(--header-h);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 16px;
  border-bottom: 1px solid var(--border);
}
.logo-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  border-radius: var(--r-sm);
  background: var(--brand);
  color: #fff;
  font-weight: 700;
  font-size: 12px;
  font-family: var(--mono);
}
.logo-text {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
  letter-spacing: 0.5px;
}

.nav {
  flex: 1;
  padding: 8px;
  border-right: none;
}
:deep(.nav .el-menu-item),
:deep(.nav .el-sub-menu__title) {
  height: 38px;
  line-height: 38px;
  border-radius: var(--r-sm);
  margin: 2px 0;
}
.nav-label { font-size: 13px; }
.nav-tip {
  color: var(--text-3);
  font-style: normal;
  font-size: 11px;
  margin-left: 4px;
}

.sidebar-foot {
  padding: 10px 16px;
  border-top: 1px solid var(--border);
  font-size: 11px;
  color: var(--text-3);
  font-family: var(--mono);
}

.header {
  height: var(--header-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
}
.sys-crumb {
  display: flex;
  align-items: center;
  gap: 8px;
}
.sys-group {
  font-size: 13px;
  color: var(--text-3);
  font-weight: 500;
}
.sys-sep {
  color: var(--border-2);
  font-size: 13px;
}
.sys-name {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  letter-spacing: 0.4px;
}
.user-area {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-2);
}
.user-avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--bg-hover);
  border: 1px solid var(--border);
  color: var(--text-2);
  font-size: 11px;
  font-weight: 600;
}
.user-name { color: var(--text); }
.sep { color: var(--border); }

.main {
  background: var(--bg);
  padding: 20px;
}
</style>
