<template>
  <header class="header">
    <div class="header-left">
      <el-button
        v-if="isMobile"
        type="text"
        :icon="Menu"
        @click="toggleSidebar"
      >菜单</el-button>
      <div class="logo">
        <h1>宝来威办公助手后台</h1>
      </div>
    </div>
    
    <div class="header-right">
      <div class="header-actions">
        <!-- 主题切换组件 -->
        <!-- <ThemeSwitcher /> -->
        
        <!-- 用户信息下拉菜单 -->
        <el-dropdown trigger="click">
          <span class="user-info">
            <el-avatar :size="32">
              {{ username.charAt(0).toUpperCase() }}
            </el-avatar>
            <span class="username">{{ username }}</span>
            <el-icon class="el-icon--right"><ArrowDown /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item divided @click="handleLogout">
                <el-icon><SwitchButton /></el-icon>
                <span>退出登录</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </header>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { Menu, ArrowDown, SwitchButton } from '@element-plus/icons-vue'
import { useAuthStore } from '../../store/auth'
import ThemeSwitcher from '../ThemeSwitcher.vue'


// 路由实例
const router = useRouter()

// 认证状态管理
const authStore = useAuthStore()

// 用户名
const username = computed(() => authStore.username)

// 响应式布局：判断是否为手机端
const isMobile = ref(false)

// 监听窗口大小变化
const handleResize = () => {
  isMobile.value = window.innerWidth < 768
}

// 初始化响应式状态
onMounted(() => {
  handleResize()
  window.addEventListener('resize', handleResize)
})

// 清理事件监听
onUnmounted(() => {
  window.removeEventListener('resize', handleResize)
})

// 打开侧边栏抽屉（手机端）
const toggleSidebar = () => {
  emit('open-menu')
}

// 登出处理
const handleLogout = () => {
  authStore.logout()
  router.push('/login')
}

// 定义事件
const emit = defineEmits(['open-menu'])
</script>

<style scoped lang="scss">
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 64px;
  padding: 0 24px;
  background-color: var(--background-color);
  border-bottom: 1px solid var(--border-color);
  box-shadow: var(--box-shadow);
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  transition: all 0.3s;
}

.logo {
  h1 {
    font-size: 20px;
    margin: 0;
    color: var(--text-color);
  }
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 16px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  color: var(--text-color);
  
  .username {
    font-size: 14px;
  }
}

/* 响应式样式 */
@media (max-width: 767px) {
  .header {
    padding: 0 6px;
  }
  
  .logo h1 {
    display: none;
  }
  
  .username {
    display: none;
  }
}
</style>
