<script setup lang="ts">
import { reactive, inject, type Ref } from 'vue';
import { Expand, Fold, ArrowDown } from '@element-plus/icons-vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { useTokenStore } from '@/stores/mytoken';
import { logout } from '@/API/users';  // 确保路径与实际存储位置匹配
import router from '@/router';

// 获取 tokenStore 实例
const tokenStore = useTokenStore();

// 从 store 中解构出 userid 和 name 来初始化 userInfo
const userInfo = reactive({
  name: tokenStore.token.name,
  userid: tokenStore.token.userid,
  loginType: tokenStore.token.loginType || 'password', // 默认为密码登录
});

// 注入 isCollapse 值
const isCollapse = inject<Ref<boolean>>('isCollapse');
if (!isCollapse) {
  throw new Error('isCollapse was not provided by a parent component.');
}

const props = defineProps<{
  currentProject: string;
}>();
const emit = defineEmits(['switch-project']);

const toggleSidebar = () => {
  if (isCollapse !== undefined) { // 防止未定义的 isCollapse 导致运行时错误
    isCollapse.value = !isCollapse.value;
  }
};

const handleLogout = async () => {
  await ElMessageBox.confirm("确认退出", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: 'warning'
  }).then(async () => {
    try {
      await logout();
      ElMessage.success("退出成功");
      
      // 使用已经获取的 tokenStore 实例
      tokenStore.clearToken(); // 假设 clearToken 已经在 store 中定义
      router.push({name: "login"});
    } catch (error) {
      console.error('Logout failed:', error);
      ElMessage.error("退出失败");
    }
  }).catch(() => {
    ElMessage.info("退出操作已取消");
  });
};
</script>

<template>
  <el-header>
    <div class="header-content">
      <!-- 展开/折叠侧边栏 -->
      <el-icon @click="toggleSidebar" class="toggle-icon">
        <component :is="isCollapse ? Expand : Fold" />
      </el-icon>

      <!-- 显示当前项目 -->
      <span v-if="props.currentProject" class="current-project">{{ props.currentProject }}</span>

 
      <el-dropdown >
        <span class="el-dropdown-link">
          <el-avatar 
            :size="32"
            :src="'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'"
          />
          <el-icon class="el-icon--right">
            <ArrowDown />
          </el-icon>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item>{{ userInfo.name }}</el-dropdown-item>
            <el-dropdown-item divided @click="handleLogout">退出</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>

    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/" class="breadcrumb">
      <el-breadcrumb-item v-for="(item, index) in $route.matched.filter(route => route.meta && !route.meta.hiddenBreadcrumb)" :key="index" :to="{ path: item.path }">
        {{ item.meta.title }}
      </el-breadcrumb-item>
    </el-breadcrumb>
  </el-header>
</template>

<style scoped>
/* 样式保持不变 */
.el-header {
  display: flex;
  flex-direction: column;
  justify-content: space-between; /* 确保面包屑有足够空间 */
  align-items: stretch; /* 让子元素占据整个宽度 */
  background-color: #f8f9fa; /* 更柔和的背景色 */
  padding: 10px 20px; /* 增加内边距 */
  border-bottom: 1px solid #e5e7eb; /* 添加底部边框增加分隔感 */
  height: auto; /* 根据内容自动调整高度 */
}

.header-content {
  display: flex;
  justify-content: space-between; /* 在侧边栏切换图标、项目选择按钮组和下拉框之间创建空间 */
  align-items: center;
  margin-bottom: 10px; /* 给header-content下方添加一些间隔，防止与面包屑重叠 */
}

/* 下拉框样式 */
.el-dropdown {
  margin-left: auto; /* 将下拉框推到最右侧 */
}

.el-dropdown-link {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.toggle-icon {
  font-size: 1.5em; /* 增大图标尺寸 */
  cursor: pointer;
  color: #6b7280; /* 设置图标颜色 */
  transition: color 0.2s ease-in-out; /* 添加颜色过渡效果 */
}

.toggle-icon:hover {
  color: #2ECC71; /* 鼠标悬停时改变颜色 */
}

.current-project {
  margin-left: 20px; /* 调整间距 */
  font-size: 1rem;
  color: #333;
}

.breadcrumb {
  margin-top: 10px; /* 与上面的内容间隔 */
  font-size: 0.875rem;
  white-space: nowrap; /* 防止面包屑换行 */
  overflow: hidden; /* 超出部分隐藏 */
  text-overflow: ellipsis; /* 显示省略号 */
  padding-bottom: 5px; /* 给面包屑下方添加一些内边距，增强视觉分离 */
}

/* 确保面包屑不会因为内容过多而撑破布局 */
.el-breadcrumb__inner {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>



