<template>
  <div class="user-info-container" @click="toggleUserMenu">
    <div class="user-avatar">
      <el-icon size="16"><User /></el-icon>
    </div>
    <div class="username">{{ username }}</div>
    <el-icon size="12" class="dropdown-icon"><ArrowDown /></el-icon>

    <!-- 用户菜单 -->
    <div v-if="showUserMenu" class="user-menu" @click.stop>
      <div class="menu-item logout" @click="handleLogout">
        <el-icon><SwitchButton /></el-icon>
        <span>退出登录</span>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { User, ArrowDown, SwitchButton } from '@element-plus/icons-vue'

const router = useRouter()
const emit = defineEmits(['logout'])

// 响应式数据
const username = ref('')
const showUserMenu = ref(false)

// 更新用户信息
const updateUserInfo = () => {
  const savedUsername = localStorage.getItem('username')
  username.value = savedUsername || '未登录'
}

// 切换用户菜单
const toggleUserMenu = () => {
  showUserMenu.value = !showUserMenu.value
}

// 关闭菜单
const closeMenu = () => {
  showUserMenu.value = false
}

// 点击外部关闭菜单
const handleClickOutside = (event: MouseEvent) => {
  const target = event.target as HTMLElement
  if (!target.closest('.user-info-container')) {
    closeMenu()
  }
}

// 退出登录
const handleLogout = async () => {
  try {
    await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
      customClass: 'logout-confirm-dialog',
    })

    // 清除登录状态
    localStorage.removeItem('isAuthenticated')
    localStorage.removeItem('username')
    localStorage.removeItem('loginTime')
    localStorage.removeItem('mqttTopic')
    localStorage.removeItem('machineData')

    // 触发父组件事件
    emit('logout')

    ElMessage.success('退出登录成功')
    router.push('/')
  } catch {
    // 用户取消
    closeMenu()
  }
}

// 组件挂载时
onMounted(() => {
  updateUserInfo()
  window.addEventListener('storage', updateUserInfo)
  document.addEventListener('click', handleClickOutside)
})

// 组件卸载时
onUnmounted(() => {
  window.removeEventListener('storage', updateUserInfo)
  document.removeEventListener('click', handleClickOutside)
})
</script>

<style scoped>
.user-info-container {
  display: flex;
  align-items: center;
  padding: 6px 12px;
  background: rgba(0, 0, 0, 0.7);
  border-radius: 6px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
  color: white;
  font-family: 'Microsoft YaHei', 微软雅黑, sans-serif;
  cursor: pointer;
  position: relative;
  user-select: none;
  transition: all 0.3s ease;
  min-width: 120px;
}

.user-info-container:hover {
  background: rgba(0, 0, 0, 0.8);
  border-color: rgba(255, 255, 255, 0.3);
  transform: translateY(-1px);
}

.user-avatar {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 8px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  flex-shrink: 0;
}

.user-avatar .el-icon {
  color: white;
}

.username {
  font-size: 12px;
  font-weight: 500;
  color: #fff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}

.dropdown-icon {
  margin-left: 6px;
  color: rgba(255, 255, 255, 0.6);
  transition: transform 0.3s ease;
  flex-shrink: 0;
}

.user-info-container:hover .dropdown-icon {
  color: rgba(255, 255, 255, 0.8);
}

/* 用户菜单样式 */
.user-menu {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.95);
  border-radius: 6px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  margin-top: 4px;
  overflow: hidden;
  z-index: 1001;
  min-width: 100px;
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  color: white;
  cursor: pointer;
  transition: background-color 0.2s;
  font-size: 12px;
}

.menu-item:hover {
  background: rgba(255, 255, 255, 0.1);
}

.menu-item .el-icon {
  margin-right: 6px;
  font-size: 12px;
}

.menu-item.logout {
  color: #ff7d7d;
}

.menu-item.logout:hover {
  background: rgba(255, 125, 125, 0.1);
}

/* 动画效果 */
.user-menu {
  animation: slideDown 0.2s ease;
}

@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateY(-8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 响应式设计 */
@media (max-width: 1400px) {
  .user-info-container {
    padding: 4px 8px;
    min-width: 100px;
  }

  .username {
    font-size: 11px;
  }

  .user-avatar {
    width: 20px;
    height: 20px;
    margin-right: 6px;
  }

  .user-avatar .el-icon {
    font-size: 12px;
  }
}
</style>

<style>
/* 全局对话框样式 */
.logout-confirm-dialog {
  background: rgba(0, 0, 0, 0.8) !important;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2) !important;
  border-radius: 8px !important;
}

.logout-confirm-dialog .el-message-box__title {
  color: white !important;
}

.logout-confirm-dialog .el-message-box__content {
  color: rgba(255, 255, 255, 0.8) !important;
}

.logout-confirm-dialog .el-message-box__btns button {
  border-radius: 4px !important;
}
</style>
<script lang="ts">
export default {
  name: 'UserInfo',
}
</script>
