<template>
  <el-container class="layout-container">
    <el-aside :width="isCollapse ? '64px' : '220px'" class="layout-aside">
      <div class="logo">
        <span v-if="!isCollapse">企业AI平台</span>
        <span v-else>AI</span>
      </div>
      <el-menu
        :default-active="activeMenu"
        :collapse="isCollapse"
        router
        background-color="#304156"
        text-color="#bfcbd9"
        active-text-color="#409EFF"
      >
        <el-menu-item index="/user/dashboard">
          <el-icon><Monitor /></el-icon>
          <span>工作台</span>
        </el-menu-item>

        <el-sub-menu index="monitor" v-if="can('monitor:read')">
          <template #title>
            <el-icon><TrendCharts /></el-icon>
            <span>工作监控</span>
          </template>
          <el-menu-item index="/user/monitor/employees">员工列表</el-menu-item>
        </el-sub-menu>

        <el-sub-menu index="task" v-if="can('task:read')">
          <template #title>
            <el-icon><List /></el-icon>
            <span>任务管理</span>
          </template>
          <el-menu-item index="/user/task/list">任务列表</el-menu-item>
        </el-sub-menu>

        <el-menu-item index="/user/agent/list">
          <el-icon><ChatDotRound /></el-icon>
          <span>智能体</span>
        </el-menu-item>

        <el-menu-item index="/user/document/manager">
          <el-icon><FolderOpened /></el-icon>
          <span>文档管理</span>
        </el-menu-item>

        <el-menu-item index="/user/rag/knowledge">
          <el-icon><Search /></el-icon>
          <span>知识库</span>
        </el-menu-item>

        <el-menu-item index="/user/wecom/config">
          <el-icon><Connection /></el-icon>
          <span>企微配置</span>
        </el-menu-item>

        <el-menu-item index="/user/notification/center">
          <el-icon><Bell /></el-icon>
          <span>通知中心</span>
        </el-menu-item>

        <el-menu-item index="/user/profile">
          <el-icon><User /></el-icon>
          <span>个人中心</span>
        </el-menu-item>

        <el-menu-item index="/user/settings">
          <el-icon><Tools /></el-icon>
          <span>系统配置</span>
        </el-menu-item>

        <el-menu-item v-if="userStore.isAdmin" index="/admin" style="margin-top: 20px; border-top: 1px solid rgba(255,255,255,0.1); padding-top: 10px">
          <el-icon><Setting /></el-icon>
          <span>管理后台</span>
        </el-menu-item>
      </el-menu>
    </el-aside>

    <el-container>
      <el-header class="layout-header">
        <div class="header-left">
          <el-button @click="isCollapse = !isCollapse" :icon="Fold" text />
          <el-breadcrumb separator="/" style="margin-left: 16px">
            <el-breadcrumb-item :to="{ path: '/user/dashboard' }">工作台</el-breadcrumb-item>
            <el-breadcrumb-item v-if="route.meta.title">{{ route.meta.title }}</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <div class="header-right">
          <el-dropdown @command="handleCommand">
            <span class="user-info">
              <el-icon><User /></el-icon>
              {{ userStore.displayName || userStore.user?.username }}
              <el-icon><ArrowDown /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">个人信息</el-dropdown-item>
                <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      <el-main class="layout-main">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { Fold, User, ArrowDown, Tools, Search } from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()
const isCollapse = ref(false)

const activeMenu = computed(() => {
  const path = route.path
  if (path.startsWith('/user/monitor')) return '/user/monitor/employees'
  if (path.startsWith('/user/task')) return '/user/task/list'
  if (path.startsWith('/user/agent')) return '/user/agent/list'
  if (path.startsWith('/user/document')) return '/user/document/manager'
  if (path.startsWith('/user/wecom')) return '/user/wecom/config'
  if (path.startsWith('/user/notification')) return '/user/notification/center'
  if (path.startsWith('/user/settings')) return '/user/settings'
  return path
})

function can(code: string): boolean {
  return userStore.hasPermission(code)
}

function handleCommand(cmd: string) {
  if (cmd === 'logout') {
    userStore.logout()
    router.push('/login')
  }
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
}
.layout-aside {
  background-color: #304156;
  overflow-y: auto;
  transition: width 0.3s;
}
.logo {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 18px;
  font-weight: bold;
  border-bottom: 1px solid rgba(255,255,255,0.1);
}
.el-menu {
  border-right: none;
}
.layout-header {
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid #e6e6e6;
  height: 60px;
  padding: 0 20px;
}
.header-left {
  display: flex;
  align-items: center;
}
.header-right {
  display: flex;
  align-items: center;
}
.user-info {
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 14px;
}
.layout-main {
  background: #f0f2f5;
  padding: 20px;
  overflow-y: auto;
}
</style>