<!-- layout/components/TopNavbar/index.vue -->
<template>
  <div class="top-navbar">
    <div class="logo-area">
      <img src="@/assets/images/tba-gxpt-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 class="menu-spacer"></div>

      <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="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 = "/index";
          });
        })
        .catch(() => {});
    };

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

<style scoped>
.top-navbar {
  height: 60px;
  background: #217ff4;
  display: flex;
  align-items: center;
  padding: 0 10px 0 30px;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}

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

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

.menu-area {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: 30px;
  overflow-x: auto;
}

.menu-spacer {
  flex: 1;
}

.menu-link {
  padding: 0 24px;
  height: 60px;
  line-height: 60px;
  color: #fff;
  cursor: pointer;
  font-size: 16px;
  font-weight: 500;
  letter-spacing: 1px;
  white-space: nowrap;
  flex-shrink: 0;
}

.menu-link:hover {
  background: rgba(255, 255, 255, 0.15);
}

.menu-link.active {
  background: rgba(255, 255, 255, 0.25);
}

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

.avatar-wrapper {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 0 12px;
  height: 60px;
  max-width: 200px;
}

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

.user-icon {
  width: 18px;
  height: 18px;
  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: 14px;
  color: #fff;
  transition: transform 0.3s;
  flex-shrink: 0;
}

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