<template>
  <el-container class="customer-dashboard">
    <el-header>
      <h1>中医药与心理健康</h1>
      <div class="menu-wrapper">
        <div class="custom-menu">
          <div class="menu-item"
               :class="{'active': activeIndex === '/customer/home'}"
               @click="$router.push('/customer/home')">
            首页
          </div>

          <div class="menu-item dropdown"
               :class="{'active': activeIndex === '/customer/education' ||
                                 activeIndex.includes('/customer/knowledge-base') ||
                                 activeIndex.includes('/customer/wellness-guide')}"
               @mouseover="showDropdown = 'education'"
               @mouseleave="showDropdown = ''">
            <div class="dropdown-title">教育与预防</div>
            <div class="dropdown-menu" v-show="showDropdown === 'education'">
              <div class="dropdown-item"
                   :class="{'active': activeIndex === '/customer/knowledge-base'}"
                   @click="handleNavigation('/customer/knowledge-base')">知识库</div>
              <div class="dropdown-item"
                   :class="{'active': activeIndex === '/customer/wellness-guide'}"
                   @click="handleNavigation('/customer/wellness-guide')">养生指南</div>
            </div>
          </div>

          <div class="menu-item dropdown"
               :class="{'active': activeIndex === '/customer/assessment' ||
                                 activeIndex.includes('/customer/psychological-test') ||
                                 activeIndex.includes('/customer/questionnaire')}"
               @mouseover="showDropdown = 'assessment'"
               @mouseleave="showDropdown = ''">
            <div class="dropdown-title">心理健康评估</div>
            <div class="dropdown-menu" v-show="showDropdown === 'assessment'">
              <div class="dropdown-item"
                   :class="{'active': activeIndex === '/customer/psychological-test'}"
                   @click="handleNavigation('/customer/psychological-test')">心理测试</div>
              <div class="dropdown-item"
                   :class="{'active': activeIndex === '/customer/questionnaire'}"
                   @click="handleNavigation('/customer/questionnaire')">问卷调查</div>
            </div>
          </div>

          <div class="menu-item"
               :class="{'active': activeIndex === '/customer/social-circle'}"
               @click="$router.push('/customer/social-circle')">
            匿名分享圈
          </div>
        </div>
      </div>
      <div class="user-avatar-container">
        <el-dropdown>
          <el-avatar :size="40" :src="userAvatar" class="user-avatar" @click="$router.push('/customer/profile')"></el-avatar>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="$router.push('/customer/profile')">个人信息</el-dropdown-item>
              <el-dropdown-item @click="logout" divided>退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-header>
    <el-main>
      <router-view></router-view>
    </el-main>
  </el-container>
</template>

<script>
export default {
  name: 'CustomerDashboard',
  data () {
    return {
      userAvatar: require('@/assets/user/1.jpg'),
      showDropdown: '',
      currentActive: ''
    }
  },
  computed: {
    activeIndex () {
      // 如果当前有手动设置的活动项，优先使用
      if (this.currentActive) {
        return this.currentActive
      }

      return this.$route.path
    }
  },
  methods: {
    handleNavigation (path) {
      this.currentActive = path
      this.$router.push(path)
      // 不要立即隐藏下拉菜单，以便用户可以看到高亮效果
      // 采用延迟关闭方式
      setTimeout(() => {
        this.showDropdown = ''
      }, 300)
    },
    logout () {
      this.$router.push('/login')
    }
  }
}
</script>

<style scoped>
.customer-dashboard {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.el-header {
  background: #4CAF50;
  color: white;
  padding: 1rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.menu-wrapper {
  flex: 1;
  display: flex;
  justify-content: center;
  max-width: 80%;
  margin: 0 15px;
}

.custom-menu {
  display: flex;
  width: 100%;
  justify-content: center;
  position: relative;
}

.menu-item {
  font-size: 15px;
  height: 56px;
  line-height: 56px;
  margin: 0 15px;
  padding: 0 10px;
  cursor: pointer;
  position: relative;
  transition: all 0.3s;
  border-bottom: 2px solid transparent;
}

.menu-item.active, .dropdown-item.active {
  color: #ffd04b;
  background-color: rgba(255, 255, 255, 0.2);
  border-bottom: 2px solid #ffd04b;
}

.menu-item:hover {
  color: #ffd04b;
  background-color: rgba(255, 255, 255, 0.2);
}

.dropdown {
  position: relative;
}

.dropdown-title {
  display: flex;
  align-items: center;
}

.dropdown-title:after {
  content: '';
  display: inline-block;
  margin-left: 5px;
  border-top: 5px solid #fff;
  border-right: 5px solid transparent;
  border-left: 5px solid transparent;
  transition: all 0.3s;
}

.active .dropdown-title:after,
.dropdown:hover .dropdown-title:after {
  border-top-color: #ffd04b;
}

.dropdown-menu {
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  min-width: 180px;
  background: #ffffff;
  box-shadow: 0 6px 16px -8px rgba(0, 0, 0, 0.08),
              0 9px 28px 0 rgba(0, 0, 0, 0.05),
              0 12px 48px 16px rgba(0, 0, 0, 0.03);
  border-radius: 4px;
  z-index: 10;
  padding: 4px 0;
  margin-top: 8px;
  transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
}

.dropdown-menu:before, .dropdown-menu:after {
  content: '';
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
}

.dropdown-menu:before {
  top: -8px;
  border-left: 8px solid transparent;
  border-right: 8px solid transparent;
  border-bottom: 8px solid rgba(0, 0, 0, 0.06);
}

.dropdown-menu:after {
  top: -7px;
  border-left: 7px solid transparent;
  border-right: 7px solid transparent;
  border-bottom: 7px solid white;
}

.dropdown-item {
  color: #606266;
  padding: 8px 16px;
  line-height: 1.5;
  cursor: pointer;
  transition: all 0.15s;
  font-size: 14px;
  position: relative;
  text-align: center;
  white-space: nowrap;
}

.dropdown-item.active {
  background-color: rgba(76, 175, 80, 0.1);
  color: #4CAF50;
  font-weight: 500;
  position: relative;
  border-radius: 3px;
}

.dropdown-item.active:before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  width: 4px;
  height: 70%;
  transform: translateY(-50%);
  background-color: #4CAF50;
  border-radius: 0 2px 2px 0;
}

.dropdown-item:hover {
  background-color: rgba(76, 175, 80, 0.05);
  color: #4CAF50;
}

.dropdown-item:active {
  background-color: rgba(76, 175, 80, 0.15);
}

.user-avatar-container {
  display: flex;
  align-items: center;
  margin-left: 10px;
}

.user-avatar {
  cursor: pointer;
  border: 2px solid white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  transition: all 0.3s ease;
}

.user-avatar:hover {
  transform: scale(1.05);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
}

@media (max-width: 992px) {
  .el-header {
    flex-direction: column;
    align-items: flex-start;
    padding: 0.5rem;
  }

  .menu-wrapper {
    width: 100%;
    max-width: 100%;
    margin: 10px 0;
  }

  .custom-menu {
    width: 100%;
    flex-wrap: wrap;
  }

  .menu-item {
    margin: 0 5px;
  }

  .user-avatar-container {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    margin-left: 0;
  }

  .dropdown-menu {
    width: 100%;
  }
}
</style>

<style>
/* 非scoped样式，确保可以覆盖element-ui原生样式 */
.el-menu--horizontal > .el-submenu.is-active .el-submenu__title {
  border-bottom: 2px solid #ffd04b !important;
  color: #ffd04b !important;
  background-color: rgba(255, 255, 255, 0.2) !important;
}

.el-menu--horizontal > .el-menu-item.is-active {
  border-bottom: 2px solid #ffd04b !important;
}

/* 特别为顶级子菜单添加悬停效果 */
.el-menu--horizontal > .el-submenu > .el-submenu__title:hover {
  color: #ffd04b !important;
  background-color: rgba(255, 255, 255, 0.2) !important;
  border-bottom: 2px solid #ffd04b !important;
}

.el-menu--horizontal .el-submenu:focus .el-submenu__title,
.el-menu--horizontal .el-submenu:hover .el-submenu__title,
.el-menu--horizontal .el-menu-item:focus,
.el-menu--horizontal .el-menu-item:hover {
  color: #ffd04b !important;
  background-color: rgba(255, 255, 255, 0.2) !important;
}

.el-submenu__title i {
  color: #fff !important;
}

.el-submenu.is-active .el-submenu__title i {
  color: #ffd04b !important;
}

.el-submenu:hover .el-submenu__title i {
  color: #ffd04b !important;
}

/* 针对inline-block元素的修复 */
.el-menu--horizontal .el-submenu {
  display: inline-block !important;
}

.el-menu--horizontal .el-submenu .el-submenu__title {
  height: 56px !important;
  line-height: 56px !important;
}
</style>
