<template>
  <div class="header-container">
    <div class="header-left">
      <el-icon class="collapse-btn" @click="$emit('toggle-collapse')">
        <Fold v-if="!collapse" />
        <Expand v-else />
      </el-icon>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item v-for="item in breadcrumbs" :key="item.path">
          {{ item.title }}
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    
    <div class="header-right">
      <!-- 主题切换按钮 -->
      <el-tooltip content="主题设置" placement="bottom">
        <div class="header-action" @click="themeStore.toggleThemeDrawer">
          <el-icon><Brush /></el-icon>
        </div>
      </el-tooltip>
      
      <el-dropdown trigger="click" @command="handleCommand">
        <div class="user-info">
          <el-avatar :size="32" :src="userInfo?.avatar || undefined">
            {{ userInfo?.realName?.charAt(0) || userInfo?.username?.charAt(0) || 'U' }}
          </el-avatar>
          <span class="username">{{ userInfo?.realName || userInfo?.username }}</span>
          <el-icon><ArrowDown /></el-icon>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="profile">个人中心</el-dropdown-item>
            <el-dropdown-item command="password">修改密码</el-dropdown-item>
            <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { Fold, Expand, ArrowDown, Brush } from '@element-plus/icons-vue'
import { useThemeStore } from '@/store/modules/theme'
import { ElMessageBox } from 'element-plus'
import { useUserStore } from '@/store/modules/user'

defineProps<{
  collapse: boolean
}>()

defineEmits(['toggle-collapse'])

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()
const themeStore = useThemeStore()

const userInfo = computed(() => userStore.userInfo)

const breadcrumbs = computed(() => {
  const matched = route.matched.filter(item => item.meta?.title)
  return matched.map(item => ({
    path: item.path,
    title: item.meta?.title as string
  }))
})

const handleCommand = async (command: string) => {
  switch (command) {
    case 'profile':
      router.push('/profile')
      break
    case 'password':
      router.push('/password')
      break
    case 'logout':
      try {
        await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await userStore.logout()
      } catch {
        // 取消退出
      }
      break
  }
}
</script>

<style scoped>
.header-container {
  width: 100%;
  height: 100%;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: var(--header-bg);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 15px;
}

.collapse-btn {
  font-size: 20px;
  cursor: pointer;
  color: var(--text-regular);
  transition: color var(--transition-duration) var(--transition-timing);
}

.collapse-btn:hover {
  color: var(--primary-color);
}

.header-action {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: var(--border-radius-base);
  cursor: pointer;
  color: var(--text-regular);
  margin-right: 12px;
  transition: all 0.2s;
}

.header-action:hover {
  background-color: var(--bg-hover);
  color: var(--primary-color);
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 6px 12px;
  border-radius: var(--border-radius-base);
  transition: background-color var(--transition-duration) var(--transition-timing);
}

.user-info:hover {
  background-color: var(--bg-hover);
}

.username {
  color: var(--text-regular);
}

:deep(.el-breadcrumb__inner) {
  color: var(--text-secondary);
}

:deep(.el-breadcrumb__item:last-child .el-breadcrumb__inner) {
  color: var(--text-primary);
}
</style>
