<template>
  <div class="user-dropdown-menu">
    <!-- 用户信息区域 -->
    <div class="user-info-section">
      <div class="user-avatar">
        <!-- 头像加载失败时显示Element Plus图标 -->
        <div v-if="avatarLoadFailed" class="user-avatar-icon">
          <el-icon class="avatar-icon">
            <User />
          </el-icon>
        </div>
        <!-- 头像加载成功时显示图片 -->
        <img 
          v-else
          :src="getAvatarUrl()"
          :alt="getUserDisplayName() + '的头像'"
          @error="handleAvatarError"
          @load="handleAvatarLoad"
        />
      </div>
      <div class="user-details">
        <div class="user-name">{{ getUserDisplayName() }}</div>
      </div>
    </div>

    <!-- VIP计划和积分区域 -->
    <div class="vip-points-section">
      <!-- VIP计划 -->
      <div class="vip-plan">
        <div class="vip-info">
          <div class="vip-icon">
            <img :src="vipIconUrl" :alt="vipIconAlt" />
          </div>
          <span class="vip-text">{{ getVipText() }}</span>
        </div>
        <el-button 
          class="vip-btn"
          @click="handleSubscribe"
        >
          {{ getVipButtonText() }}
        </el-button>
      </div>

      <!-- 分界线 -->
      <div class="divider"></div>

      <!-- 积分余额 -->
      <!-- <div class="points-balance">
        <div class="points-info">
          <div class="points-icon">
            <img src="../assets/header/1818.svg" alt="积分" />
          </div>
          <span class="points-text">积分余额 {{ formatBalance(userInfo?.balance) }}</span>
        </div>
        <el-button 
          class="points-btn"
          @click="handleUsePoints"
        >
          去使用
        </el-button>
      </div> -->
    </div>

    <!-- 功能图标区域 -->
    <div class="function-icons">
      <div class="icon-item" @click="handleFavorites">
        <div class="icon-container favorites-icon">
          <el-icon><Star /></el-icon>
        </div>
        <span class="icon-text">我的收藏</span>
      </div>
      
      <div class="icon-item" @click="handleOrders">
        <div class="icon-container orders-icon">
          <el-icon><Document /></el-icon>
        </div>
        <span class="icon-text">订单管理</span>
      </div>
      
      <div class="icon-item" @click="handleSettings">
        <div class="icon-container settings-icon">
          <el-icon><User /></el-icon>
        </div>
        <span class="icon-text">个人中心</span>
      </div>
    </div>

    <!-- 退出登录 -->
    <div class="logout-section">
      <el-button 
        class="logout-btn"
        @click="handleLogout"
      >
        退出账号
      </el-button>
    </div>
  </div>
</template>

<script>
import { Star, Document, Setting, User } from '@element-plus/icons-vue'

export default {
  name: 'UserDropdownMenu',
  components: {
    Star,
    Document,
    Setting,
    User
  },
  props: {
    userInfo: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      // 头像加载状态
      avatarLoadFailed: false
    }
  },
  computed: {
    vipIconUrl() {
      if (!this.userInfo || !this.userInfo.role) {
        return new URL('../assets/User_Vip/no_vip.svg', import.meta.url).href
      }
      switch (this.userInfo.role) {
        case 2: // VIP
          return new URL('../assets/User_Vip/vip.svg', import.meta.url).href
        case 3: // SVIP
          return new URL('../assets/User_Vip/svip.svg', import.meta.url).href
        case 1: // 普通用户
        default:
          return new URL('../assets/User_Vip/no_vip.svg', import.meta.url).href
      }
    },
    vipIconAlt() {
      if (!this.userInfo || !this.userInfo.role) {
        return '普通用户'
      }
      switch (this.userInfo.role) {
        case 2:
          return 'VIP用户'
        case 3:
          return 'SVIP用户'
        case 1:
        default:
          return '普通用户'
      }
    }
  },
  methods: {
    // 获取用户显示名称
    getUserDisplayName() {
      if (!this.userInfo) return '用户';
      
      return this.userInfo.username || 
             this.userInfo.phone || 
             '用户';
    },
    
    // 处理头像加载错误
    handleAvatarError(event) {
      // 设置头像加载失败状态，显示Element Plus图标
      this.avatarLoadFailed = true;
      console.log('头像加载失败，使用Element Plus图标替代');
    },
    
    // 处理头像加载成功
    handleAvatarLoad(event) {
      // 重置头像加载失败状态
      this.avatarLoadFailed = false;
      console.log('头像加载成功');
    },

    // 获取头像URL
    getAvatarUrl() {
      if (!this.userInfo || !this.userInfo.avatarUrl) {
        // 如果没有头像URL，直接设置加载失败状态
        this.avatarLoadFailed = true;
        return '';
      }
      return this.userInfo.avatarUrl;
    },
    
    // 处理订阅VIP
    handleSubscribe() {
      // 所有状态都跳转到购买会员页面
      this.$router.push('/membership');
    },
    
    // 处理使用积分
    handleUsePoints() {
      this.$emit('use-points');
    },
    
    // 处理我的收藏
    handleFavorites() {
      this.$router.push('/personal-center/favorites');
    },
    
    // 处理订单管理
    handleOrders() {
      this.$router.push('/personal-center/orders');
    },
    
    // 处理账号设置
  handleSettings() {
      this.$router.push('/personal-center/key-management');
    },
    
    // 处理退出登录
    handleLogout() {
      this.$emit('logout');
    },

    // 获取VIP文本
    getVipText() {
      if (this.userInfo?.role === 3) {
        return '您是SVIP会员';
      } else if (this.userInfo?.role === 2) {
        return '您是VIP会员';
      }
      return '加入VIP计划';
    },

    // 获取VIP按钮文本
    getVipButtonText() {
      if (this.userInfo?.role === 2 || this.userInfo?.role === 3) {
        return '去查看';
      }
      return '去订阅';
    },

    // 格式化余额
    formatBalance(balance) {
      if (!balance && balance !== 0) {
        return '0.00';
      }
      return parseFloat(balance).toFixed(2);
    }
  },
  mounted() {
    // 组件挂载时检查头像URL，如果没有则设置加载失败状态
    if (!this.userInfo || !this.userInfo.avatarUrl) {
      this.avatarLoadFailed = true;
    }
  }
}
</script>

<style scoped>
.user-dropdown-menu {
  width: 240px;
  min-height: 280px;
  background-color: #ffffff;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
  border: 1px solid #e0e0e0;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  box-sizing: border-box;
}

/* 用户信息区域 */
.user-info-section {
  display: flex;
  align-items: center;
  gap: 12px;
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  overflow: hidden;
  border: 2px solid #f0f0f0;
  flex-shrink: 0;
}

.user-avatar img {
  width: 40px;
  height: 40px;
  object-fit: cover;
  display: block;
}

.user-avatar-icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: #f5f5f5;
  display: flex;
  align-items: center;
  justify-content: center;
}

.avatar-icon {
  font-size: 20px;
  color: #999999;
}

.user-details {
  flex: 1;
}

.user-name {
  font-family: Arial, sans-serif;
  font-weight: 600;
  font-size: 16px;
  line-height: 20px;
  color: #333333;
}

/* VIP计划和积分区域 */
.vip-points-section {
  background-color: rgba(255, 248, 232, 1);
  border: 1px solid rgba(254, 172, 51, 1);
  border-radius: 8px;
  padding: 0 7px;
  display: flex;
  flex-direction: column;
  gap: 0;
}

.vip-plan, .points-balance {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 30px;
  padding: 9px 0;
}

.vip-info, .points-info {
  display: flex;
  align-items: center;
  gap: 4px;
}

.vip-icon {
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 0;
}

.vip-icon img {
  width: 20px;
  height: 20px;
  object-fit: contain;
}

.vip-text, .points-text {
  font-family: Arial, sans-serif;
  font-weight: 400;
  font-size: 10px;
  line-height: 11px;
  letter-spacing: 0px;
  color: rgba(168, 98, 43, 1);
  text-align: left;
}

.points-icon {
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 0;
}

.points-icon img {
  width: 20px;
  height: 20px;
  object-fit: contain;
}

.vip-btn {
  background-color: rgba(34, 34, 34, 1);
  border-color: rgba(34, 34, 34, 1);
  color: rgba(255, 224, 153, 1);
  font-family: Arial, sans-serif;
  font-weight: 400;
  font-size: 10px;
  line-height: 11px;
  letter-spacing: 0px;
  padding: 0;
  width: 40px;
  height: 16px;
  position: relative;
  overflow: hidden;
}

.vip-btn:hover {
  background-color: rgba(34, 34, 34, 1);
  border-color: rgba(34, 34, 34, 1);
  color: rgba(255, 224, 153, 1);
}

.vip-btn:hover::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
  animation: shimmer 0.8s ease-in-out;
}

@keyframes shimmer {
  0% {
    left: -100%;
  }
  100% {
    left: 100%;
  }
}

.points-btn {
  background-color: transparent;
  border-color: rgba(168, 98, 43, 1);
  color: rgba(168, 98, 43, 1);
  font-family: Arial, sans-serif;
  font-weight: 400;
  font-size: 10px;
  line-height: 11px;
  letter-spacing: 0px;
  padding: 0;
  width: 40px;
  height: 16px;
}

.points-btn:hover {
  background-color: rgba(168, 98, 43, 0.1);
  border-color: rgba(168, 98, 43, 1);
  color: rgba(168, 98, 43, 1);
}

/* 分界线 */
.divider {
  height: 1px;
  background-color: rgba(254, 172, 51, 1);
  margin: 0;
}

/* 功能图标区域 */
.function-icons {
  display: flex;
  justify-content: space-around;
  align-items: center;
  min-height: 60px;
}

.icon-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  transition: transform 0.2s ease;
}

.icon-item:hover {
  transform: translateY(-2px);
}

.icon-container {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s ease;
}

.favorites-icon {
  background-color: rgba(255, 240, 229, 1);
}

.orders-icon {
  background-color: rgba(228, 242, 253, 1);
}

.settings-icon {
  background-color: rgba(227, 252, 244, 1);
}

.icon-container .el-icon {
  font-size: 18px;
  color: #666666;
}

.favorites-icon .el-icon {
  color: #ff9500;
}

.orders-icon .el-icon {
  color: #3b82f6;
}

.settings-icon .el-icon {
  color: #10b981;
}

.icon-text {
  font-family: Arial, sans-serif;
  font-weight: 400;
  font-size: 12px;
  line-height: 14px;
  color: #666666;
  text-align: center;
}

/* 退出登录区域 */
.logout-section {
  display: flex;
  justify-content: center;
  align-items: center;
  padding-top: 12px;
  border-top: 1px solid #f0f0f0;
  margin-top: 8px;
}

.logout-btn {
  font-family: Arial, sans-serif;
  font-weight: 400;
  font-size: 12px;
  line-height: 14px;
  color: #666666;
  background-color: #f8f9fa;
  border: 1px solid #e9ecef;
  padding: 8px 16px;
  border-radius: 6px;
  transition: all 0.2s ease;
  width: 100%;
}

.logout-btn:hover {
  color: #ff4757;
  background-color: rgba(255, 71, 87, 0.1);
  border-color: #ff4757;
}
</style> 