<!-- layout/components/TopNavbar/index.vue -->
<template>
  <div class="top-navbar">
    <div class="logo-area">
      <img src="@/assets/images/tba-slgc-logo.png" alt="logo"/>
    </div>

    <div class="menu-area">
      <!-- 首页菜单 -->
      <div
          :class="[
          'menu-link',
          { active: activeMenu === '/' || activeMenu === '/index' },
        ]"
          @click="handleMenuClick('/index')"
      >
        首页
      </div>

      <!-- 动态菜单 -->
      <div
          v-for="item in topMenus"
          :key="item.path"
          :class="['menu-link', { active: activeMenu === item.path }]"
          @click="handleMenuClick(item.path)"
      >
        {{ item.meta?.title }}
      </div>
    </div>

    <div class="right-menu">
      <el-dropdown
          @command="handleCommand"
          class="avatar-container"
          trigger="hover"
      >
        <div class="avatar-wrapper">
          <!-- 用户图标 -->
          <svg-icon icon-class="user" class="user-icon"/>
          <!-- 用户名 - 添加溢出处理 -->
          <span
              class="user-nickname"
              :title="userStore.nickName || userStore.name"
          >
            {{ userStore.nickName || userStore.name }}
          </span>
          <!-- 下拉箭头 -->
          <el-icon class="dropdown-icon">
            <ArrowDown/>
          </el-icon>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <router-link to="/user/profile">
              <el-dropdown-item>个人中心</el-dropdown-item>
            </router-link>
            <el-dropdown-item
                command="setLayout"
                v-if="settingsStore.showSettings"
            >
              <span>布局设置</span>
            </el-dropdown-item>
            <el-dropdown-item command="lockScreen">
              <span>锁定屏幕</span>
            </el-dropdown-item>
            <el-dropdown-item divided command="logout">
              <span>退出登录</span>
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script>
import {computed, ref} from "vue";
import {useRoute, useRouter} from "vue-router";
import {ElMessageBox} from "element-plus";
import {ArrowDown} from "@element-plus/icons-vue";
import usePermissionStore from "@/store/modules/permission";
import useUserStore from "@/store/modules/user";
import useSettingsStore from "@/store/modules/settings";
import useLockStore from "@/store/modules/lock";

export default {
  name: "TopNavbar",
  components: {
    ArrowDown,
  },
  props: {
    activeMenu: {
      type: String,
      default: "",
    },
  },
  emits: ["menu-click", "setLayout"],
  setup(props, {emit}) {
    const route = useRoute();
    const router = useRouter();
    const permissionStore = usePermissionStore();
    const userStore = useUserStore();
    const settingsStore = useSettingsStore();
    const lockStore = useLockStore();

    const isFullscreen = ref(false);

    const topMenus = computed(() => {
      const routes = permissionStore.topbarRouters || [];
      return routes.filter((item) => item.meta?.title && !item.hidden);
    });

    const handleMenuClick = (path) => {
      emit("menu-click", path);
    };

    const handleCommand = (command) => {
      switch (command) {
        case "setLayout":
          emit("setLayout");
          break;
        case "lockScreen":
          lockScreen();
          break;
        case "logout":
          logout();
          break;
        default:
          break;
      }
    };

    const lockScreen = () => {
      const currentPath = route.fullPath;
      lockStore.lockScreen(currentPath);
      router.push("/lock");
    };

    const logout = () => {
      ElMessageBox.confirm("确定注销并退出系统吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
          .then(() => {
            userStore.logOut().then(() => {
              location.href = "/slgc/index";
            });
          })
          .catch(() => {
          });
    };

    return {
      topMenus,
      userStore,
      settingsStore,
      isFullscreen,
      handleMenuClick,
      handleCommand,
    };
  },
};
</script>

<style scoped>
.top-navbar {
  height: 56px;
  background: linear-gradient(135deg, #1a6be0, #217ff4);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  box-shadow: 0 2px 8px rgba(33, 127, 244, 0.15);
  user-select: none;
}

.logo-area {
  flex-shrink: 0;
  display: flex;
  align-items: center;
}

.logo-area img {
  height: 45px;
  width: auto;
}

.logo-text {
  color: #fff;
  font-size: 18px;
  font-weight: 600;
  letter-spacing: 2px;
  white-space: nowrap;
}

.menu-area {
  flex: 1;
  display: flex;
  gap: 2px;
  margin: 0 16px;
  overflow-x: auto;
  justify-content: flex-start;
}

.menu-link {
  padding: 0 20px;
  height: 56px;
  line-height: 56px;
  color: rgba(255, 255, 255, 0.85);
  cursor: pointer;
  font-size: 15px;
  font-weight: 500;
  white-space: nowrap;
  flex-shrink: 0;
  transition: all 0.2s;
  position: relative;
}

.menu-link:hover {
  color: #fff;
  background: rgba(255, 255, 255, 0.12);
}

.menu-link.active {
  color: #fff;
  background: rgba(255, 255, 255, 0.18);
}

.menu-link.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 60%;
  height: 3px;
  background: #fff;
  border-radius: 3px 3px 0 0;
}

.right-menu {
  display: flex;
  align-items: center;
  color: #fff;
  flex-shrink: 0;
  height: 100%;
}

.avatar-container {
  margin-right: 0;
  padding-right: 0;
}

.avatar-wrapper {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  padding: 0 10px;
  height: 56px;
  max-width: 200px;
  transition: background 0.2s;
  border-radius: 4px;
}

.avatar-wrapper:hover {
  background: rgba(255, 255, 255, 0.12);
}

.user-icon {
  width: 16px;
  height: 16px;
  color: #fff;
  flex-shrink: 0;
}

.user-nickname {
  font-size: 14px;
  color: #fff;
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex-shrink: 1;
}

.dropdown-icon {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.75);
  transition: transform 0.3s;
  flex-shrink: 0;
}

.avatar-container:hover .dropdown-icon {
  transform: rotate(180deg);
}
</style>
