<template>
  <el-container class="layout-container">
    <el-aside :width="isCollapse ? '64px' : '200px'" class="sidebar">
      <div class="logo">
        <span v-if="!isCollapse">情绪博物馆</span>
        <span v-else>情</span>
      </div>
      <el-menu
        :default-active="activeMenu"
        :collapse="isCollapse"
        :unique-opened="true"
        router
      >
        <template v-for="item in menuRoutes" :key="item.path">
          <!-- 如果有子菜单，显示为子菜单 -->
          <el-sub-menu v-if="item.children && item.children.length > 0" :index="item.path">
            <template #title>
              <el-icon v-if="item.icon">
                <component :is="item.icon" />
              </el-icon>
              <span>{{ item.title }}</span>
            </template>
            <el-menu-item
              v-for="child in item.children"
              :key="child.path"
              :index="child.path"
            >
              <el-icon v-if="child.icon">
                <component :is="child.icon" />
              </el-icon>
              <span>{{ child.title }}</span>
            </el-menu-item>
          </el-sub-menu>
          <!-- 没有子菜单，直接显示为菜单项 -->
          <el-menu-item v-else :index="item.path">
            <el-icon v-if="item.icon">
              <component :is="item.icon" />
            </el-icon>
            <span>{{ item.title }}</span>
          </el-menu-item>
        </template>
      </el-menu>
    </el-aside>
    
    <el-container>
      <el-header class="header">
        <div class="header-left">
          <el-icon class="collapse-icon" @click="toggleCollapse">
            <Fold v-if="!isCollapse" />
            <Expand v-else />
          </el-icon>
        </div>
        <div class="header-right">
          <el-dropdown @command="handleCommand">
            <div class="user-info">
              <el-avatar :size="32" :src="adminInfo?.avatar">
                {{ adminInfo?.username?.charAt(0) }}
              </el-avatar>
              <span class="username">{{ adminInfo?.username }}</span>
            </div>
            <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="main-content">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { useAdminStore } from '@/stores/admin'
import { Fold, Expand } from '@element-plus/icons-vue'
import { menuConfig } from '@/config/menu'

const route = useRoute()
// const router = useRouter()
const adminStore = useAdminStore()

const isCollapse = ref(false)
const adminInfo = computed(() => adminStore.adminInfo)

const activeMenu = computed(() => route.path)

const menuRoutes = computed(() => {
  return menuConfig.filter(item => !item.hidden)
})

const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}

const handleCommand = (command: string) => {
  if (command === 'logout') {
    adminStore.logout()
  } else if (command === 'profile') {
    // TODO: 跳转到个人信息页面
  }
}

onMounted(() => {
  adminStore.initAdminInfo()
})
</script>

<style scoped lang="scss">
.layout-container {
  height: 100vh;
  background: transparent;
}

.sidebar {
  background: var(--ls-glass-bg);
  border-right: 1px solid var(--ls-glass-border);
  backdrop-filter: blur(25px) saturate(180%);
  -webkit-backdrop-filter: blur(25px) saturate(180%);
  transition: width 0.3s;
  
  .logo {
    height: 60px;
    line-height: 60px;
    text-align: center;
    font-size: 20px;
    font-weight: bold;
    color: var(--ls-text);
    background: rgba(255, 255, 255, 0.02);
    border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  }
  
  :deep(.el-menu) {
    border-right: none;
    background: transparent;
    
    .el-menu-item,
    .el-sub-menu__title {
      color: rgba(226, 232, 240, 0.75);
      border-radius: 14px;
      margin: 6px 10px;
      height: 44px;
      
      &:hover {
        background: rgba(255, 255, 255, 0.06);
      }
      
      &.is-active {
        background: rgba(255, 171, 145, 0.08);
        border: 1px solid rgba(255, 171, 145, 0.20);
        color: var(--ls-accent-orange) !important;
      }
    }
  }
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--ls-glass-bg);
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  box-shadow: none;
  padding: 0 20px;
  backdrop-filter: blur(25px) saturate(180%);
  -webkit-backdrop-filter: blur(25px) saturate(180%);
  
  .header-left {
    .collapse-icon {
      font-size: 20px;
      cursor: pointer;
      color: rgba(226, 232, 240, 0.85);
      
      &:hover {
        color: var(--ls-accent-orange);
      }
    }
  }
  
  .header-right {
    .user-info {
      display: flex;
      align-items: center;
      gap: 10px;
      cursor: pointer;
      color: rgba(226, 232, 240, 0.9);
      
      .username {
        font-size: 14px;
      }
    }
  }
}

.main-content {
  background: transparent;
  padding: 20px;
}
</style>
