<template>
  <a-layout class="admin-layout">
    <a-layout-sider
      v-model:collapsed="collapsed"
      :trigger="null"
      collapsible
      :width="220"
      class="admin-layout__sider"
    >
      <div class="admin-layout__logo">
        <span v-if="!collapsed" class="admin-layout__logo-text">Admin Console</span>
        <span v-else class="admin-layout__logo-text">AC</span>
      </div>
      <a-menu
        v-model:selectedKeys="selectedKeys"
        mode="inline"
        theme="dark"
        class="admin-layout__menu"
      >
        <a-menu-item key="dashboard" @click="router.push('/admin/dashboard')">
          <DashboardOutlined /><span>概览</span>
        </a-menu-item>
        <a-menu-item key="departments" @click="router.push('/admin/departments')">
          <ApartmentOutlined /><span>部门管理</span>
        </a-menu-item>
        <a-menu-item key="users" @click="router.push('/admin/users')">
          <TeamOutlined /><span>用户管理</span>
        </a-menu-item>
        <a-menu-item key="llm" @click="router.push('/admin/llm')">
          <CloudOutlined /><span>LLM 配置</span>
        </a-menu-item>
        <a-menu-item key="skills" @click="router.push('/admin/skills')">
          <ThunderboltOutlined /><span>Skill 管理</span>
        </a-menu-item>
        <a-menu-item key="kb" @click="router.push('/admin/kb')">
          <BookOutlined /><span>知识库</span>
        </a-menu-item>
        <a-menu-item key="usage" @click="router.push('/admin/usage')">
          <BarChartOutlined /><span>用量仪表盘</span>
        </a-menu-item>
      </a-menu>
    </a-layout-sider>

    <a-layout class="admin-layout__main">
      <a-layout-header class="admin-layout__header">
        <div class="admin-layout__header-left">
          <button class="admin-layout__collapse-btn" @click="collapsed = !collapsed">
            <MenuFoldOutlined v-if="!collapsed" />
            <MenuUnfoldOutlined v-else />
          </button>
          <a-button type="link" @click="router.push('/agent/chat')">
            <RollbackOutlined />
            返回主界面
          </a-button>
        </div>
        <div class="admin-layout__header-right">
          <span class="admin-layout__username">{{ authStore.user?.username ?? 'admin' }}</span>
        </div>
      </a-layout-header>

      <a-layout-content class="admin-layout__content">
        <router-view />
      </a-layout-content>
    </a-layout>
  </a-layout>
</template>

<script setup lang="ts">
/**
 * AdminLayout — left sidebar navigation + top bar + content area.
 *
 * Wraps all ``/admin/*`` child routes. The sidebar highlights the
 * active section based on the current route's first path segment.
 * Full-height layout with a dark sidebar (Ant Design dark menu theme)
 * and a light content area that respects the global CSS variables for
 * dark-mode support.
 */
import { ref, computed, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import {
  DashboardOutlined,
  ApartmentOutlined,
  TeamOutlined,
  CloudOutlined,
  ThunderboltOutlined,
  BookOutlined,
  BarChartOutlined,
  MenuFoldOutlined,
  MenuUnfoldOutlined,
  RollbackOutlined,
} from '@ant-design/icons-vue'
import { useAuthStore } from '@/stores/auth'

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

const collapsed = ref(false)

/** Derive the active menu key from the current route path. */
const activeKey = computed<string>(() => {
  const segment = route.path.split('/')[2] ?? 'dashboard'
  return segment === '' ? 'dashboard' : segment
})

const selectedKeys = ref<string[]>([activeKey.value])

watch(activeKey, (val) => {
  selectedKeys.value = [val]
})
</script>

<style scoped>
.admin-layout {
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}

.admin-layout__sider {
  background: var(--bg-secondary);
  border-right: 1px solid var(--border-color);
}

.admin-layout__sider :deep(.ant-layout-sider-children) {
  display: flex;
  flex-direction: column;
}

.admin-layout__logo {
  height: var(--topnav-height);
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom: 1px solid var(--border-color-split);
  flex-shrink: 0;
}

.admin-layout__logo-text {
  font-size: var(--font-md);
  font-weight: var(--font-weight-bold);
  color: var(--text-primary);
  letter-spacing: 0.3px;
}

.admin-layout__menu {
  flex: 1;
  border-right: none;
}

.admin-layout__main {
  background: var(--bg-tertiary);
  overflow: hidden;
}

.admin-layout__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--topnav-height);
  padding: 0 var(--space-5);
  background: var(--bg-primary);
  border-bottom: 1px solid var(--border-color-split);
  flex-shrink: 0;
}

.admin-layout__header-left {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.admin-layout__header-right {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.admin-layout__username {
  font-size: var(--font-sm);
  color: var(--text-secondary);
  font-weight: var(--font-weight-medium);
}

.admin-layout__collapse-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border: none;
  background: transparent;
  color: var(--text-tertiary);
  cursor: pointer;
  border-radius: var(--radius-md);
  transition: all var(--transition-fast);
}

.admin-layout__collapse-btn:hover {
  color: var(--text-primary);
  background: var(--bg-tertiary);
}

.admin-layout__content {
  overflow: auto;
  padding: 0;
  background: var(--bg-tertiary);
}
</style>
