<template>
  <el-menu
    mode="horizontal"
    :default-active="'/admin/'"
    background-color="black"
    text-color="#fff"
    router
    class="custom-menu"
  >
    <!-- Logo 放在最左侧 -->
    <el-menu-item index="/admin/" class="logo-item">
      <img
        src="https://th.bing.com/th/id/R.e4d22d8403ffcab0afd33d7cd7eab3f3?rik=d3v%2bON4GW7LHQg&riu=http%3a%2f%2fimage.uisdc.com%2fwp-content%2fuploads%2f2016%2f05%2f26zh20160511.png&ehk=XZ9DkwWXnyL9Z2FyjOzco9RLqV9mDzUb2DAAvVHR97s%3d&risl=&pid=ImgRaw&r=0"
        alt="餐厅Logo"
        class="logo-img"
      />
    </el-menu-item>

    <!-- 首页 -->
    <el-menu-item index="/admin/">
      <el-icon><HomeFilled /></el-icon>
      首页
    </el-menu-item>

    <el-menu-item index="/admin/branchmanagement">
      <el-icon><DishDot /></el-icon>
      门店管理
    </el-menu-item>

    <el-menu-item index="/admin/dishmanagement">
      <el-icon><Phone /></el-icon>
      菜品管理
    </el-menu-item>

    <el-menu-item index="/admin/merchantmanagement">
      <el-icon><ChatLineSquare /></el-icon>
      账户管理
    </el-menu-item>

    <!-- 右侧下拉菜单 -->
    <div class="right-section">
      <el-dropdown trigger="hover" placement="bottom">
        <span class="el-dropdown-link">
          切换端口
          <el-icon><ArrowDownBold /></el-icon>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item>商家端</el-dropdown-item>
            <el-dropdown-item>用户端</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>

      <!-- 用户菜单 -->
      <el-dropdown @command="handleDropdownCommand" trigger="hover" placement="bottom">
        <span class="el-dropdown-link">
          管理员
          <el-icon><ArrowDownBold /></el-icon>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="profile">个人设置</el-dropdown-item>
            <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </el-menu>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'

const router = useRouter()
const authStore = useAuthStore()

// 处理下拉菜单命令
const handleDropdownCommand = (command: string) => {
  if (command === 'logout') {
    // 清除本地用户数据和token
    authStore.logout()
    // 返回登录界面
    router.push('/login')
  } else if (command === 'profile') {
    // 跳转到个人设置页面
    router.push('/change-password')
  }
}
</script>

<style scoped>
.custom-menu {
  height: 80px;
  line-height: 80px;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: space-between; /* 左右分开展示 */
  padding-right: 20px; /* 右侧留白 */
}

/* Logo 菜单项 */
.logo-item {
  padding: 0 20px;
  height: 80px;
  line-height: 80px;
  background-color: transparent !important;
}

/* Logo 图片样式 */
.logo-img {
  height: 50px;
  vertical-align: middle;
  border-radius: 4px;
}

/* 右侧下拉菜单样式 */
.right-section {
  display: flex;
  align-items: center;
}

.el-dropdown-link {
  display: flex;
  align-items: center;
  color: #fff;
  font-size: 16px;
  cursor: pointer;
  padding: 0 20px;
}

.el-dropdown-menu {
  min-width: 120px;
}
</style>
