<template>
  <div class="user-dropdown" @click="toggleDropdown" v-click-outside="closeDropdown">
    <!-- 用户头像和信息 -->
    <div class="user-info">
      <div class="user-avatar">
        <img :src="userAvatar" :alt="user?.username" v-if="userAvatar">
        <span class="avatar-placeholder" v-else>{{ avatarText }}</span>
      </div>
      <div class="user-details" v-if="!collapsed">
        <div class="user-name">{{ user?.realName || user?.username }}</div>
        <div class="user-role">{{ primaryRole }}</div>
      </div>
      <i class="dropdown-icon" :class="{ 'open': dropdownVisible }"></i>
    </div>
    
    <!-- 下拉菜单 -->
    <transition name="dropdown">
      <div class="dropdown-menu" v-if="dropdownVisible">
        <div class="dropdown-item" @click="goToProfile">
          <i class="item-icon icon-profile"></i>
          <span>个人资料</span>
        </div>
        <div class="dropdown-item" @click="goToSettings">
          <i class="item-icon icon-settings"></i>
          <span>账户设置</span>
        </div>
        <div class="dropdown-divider"></div>
        <div class="dropdown-item danger" @click="handleLogout">
          <i class="item-icon icon-logout"></i>
          <span>退出登录</span>
        </div>
      </div>
    </transition>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';
import { useRouter } from 'vue-router';
import type { UserVO } from '@/types';

// Props
interface Props {
  user?: UserVO | null;
  collapsed?: boolean;
}

const props = defineProps<Props>();

// Emits
const emit = defineEmits<{
  logout: [];
}>();

// 状态
const dropdownVisible = ref(false);

// Composition API
const router = useRouter();

// 计算属性
const userAvatar = computed(() => {
  return props.user?.avatar || '';
});

const avatarText = computed(() => {
  const name = props.user?.realName || props.user?.username || '';
  return name.charAt(0).toUpperCase();
});

const primaryRole = computed(() => {
  // 这里可以从store中获取用户角色信息
  return '管理员'; // 暂时硬编码
});

// 方法 - 使用 function 声明
function toggleDropdown() {
  dropdownVisible.value = !dropdownVisible.value;
}

function closeDropdown() {
  dropdownVisible.value = false;
}

function goToProfile() {
  closeDropdown();
  router.push('/profile');
}

function goToSettings() {
  closeDropdown();
  router.push('/profile/settings');
}

function handleLogout() {
  closeDropdown();
  emit('logout');
}

// 点击外部关闭指令
const vClickOutside = {
  mounted(el: any, binding: any) {
    el._clickOutside = (event: Event) => {
      if (!(el === event.target || el.contains(event.target as Node))) {
        binding.value();
      }
    };
    document.addEventListener('click', el._clickOutside);
  },
  unmounted(el: any) {
    document.removeEventListener('click', el._clickOutside);
    delete el._clickOutside;
  }
};
</script>

<style lang="scss" scoped>
.user-dropdown {
  position: relative;
  cursor: pointer;
  user-select: none;
}

.user-info {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  border-radius: 4px;
  transition: background-color 0.2s ease;
  
  &:hover {
    background-color: #f5f5f5;
  }
}

.user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  overflow: hidden;
  margin-right: 12px;
  
  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
  
  .avatar-placeholder {
    width: 100%;
    height: 100%;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    font-weight: 600;
  }
}

.user-details {
  flex: 1;
  min-width: 0;
  
  .user-name {
    font-size: 14px;
    font-weight: 500;
    color: #333;
    line-height: 1.2;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  
  .user-role {
    font-size: 12px;
    color: #666;
    margin-top: 2px;
    line-height: 1;
  }
}

.dropdown-icon {
  margin-left: 8px;
  font-size: 12px;
  color: #666;
  transition: transform 0.2s ease;
  
  &::before {
    content: "▼";
  }
  
  &.open {
    transform: rotate(180deg);
  }
}

.dropdown-menu {
  position: absolute;
  top: 100%;
  right: 0;
  min-width: 160px;
  background: white;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  border: 1px solid #e8e8e8;
  z-index: 1000;
  overflow: hidden;
}

.dropdown-item {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  cursor: pointer;
  transition: background-color 0.2s ease;
  font-size: 14px;
  color: #333;
  
  &:hover {
    background-color: #f5f5f5;
  }
  
  &.danger {
    color: #ff4d4f;
    
    &:hover {
      background-color: #fff2f0;
    }
  }
}

.item-icon {
  width: 16px;
  margin-right: 12px;
  text-align: center;
  font-size: 14px;
}

.dropdown-divider {
  height: 1px;
  background-color: #e8e8e8;
  margin: 4px 0;
}

/* 动画效果 */
.dropdown-enter-active,
.dropdown-leave-active {
  transition: all 0.2s ease;
}

.dropdown-enter-from,
.dropdown-leave-to {
  opacity: 0;
  transform: translateY(-4px);
}

/* 图标字体类 */
.icon-profile::before { content: "👤"; }
.icon-settings::before { content: "⚙️"; }
.icon-logout::before { content: "🚪"; }
</style>
