<script setup>
import { computed, ref, watch } from "vue";
import { RouterLink, useRoute } from "vue-router";
import { Icon } from "@iconify/vue";

const route = useRoute();
const mobileOpen = ref(false);
const activeDropdown = ref(null);
const dropdownTimer = ref(null);

const dropdowns = {
  cases: [
    { label: "设备管理", to: "/cases/1" },
    { label: "QHSE", to: "/cases/2" },
    { label: "智能注气", to: "/cases/3" },
  ],
};

const navItems = [
  { label: "产品", to: "/products" },
  { label: "解决方案", to: "/solutions" },
  {
    label: "客户案例",
    to: "/cases",
    dropdown: dropdowns.cases,
  },
  { label: "新闻动态", to: "/news" },
];

watch(
  () => route.fullPath,
  () => {
    mobileOpen.value = false;
    activeDropdown.value = null;
    if (dropdownTimer.value) {
      clearTimeout(dropdownTimer.value);
    }
  },
);

const handleMouseEnter = (item) => {
  if (item.dropdown) {
    if (dropdownTimer.value) {
      clearTimeout(dropdownTimer.value);
      dropdownTimer.value = null;
    }
    activeDropdown.value = item.label;
  }
};

const handleMouseLeave = (item) => {
  if (item.dropdown) {
    dropdownTimer.value = setTimeout(() => {
      activeDropdown.value = null;
    }, 200);
  }
};

const mobileLabel = computed(() =>
  mobileOpen.value ? "关闭菜单" : "打开菜单",
);
</script>

<template>
  <header class="app-header">
    <div class="app-header__inner">
      <div style="display: flex; align-items: center">
        <RouterLink class="brand" to="/" aria-label="工业互联网门户">
          <img
            src="../../assets//images/logo.png"
            alt=""
            style="width: auto; height: 36px"
          />
        </RouterLink>

        <nav class="nav" aria-label="主导航">
          <div
            v-for="item in navItems"
            :key="item.to"
            class="nav__item"
            @mouseenter="handleMouseEnter(item)"
            @mouseleave="handleMouseLeave(item)"
          >
            <RouterLink class="nav__link" :to="item.to">
              {{ item.label }}
              <!-- <Icon
                v-if="item.dropdown"
                icon="lucide:chevron-down"
                width="16"
                height="16"
              /> -->
            </RouterLink>

            <div
              v-if="item.dropdown"
              v-show="activeDropdown === item.label"
              class="nav__dropdown-menu"
              @mouseenter="handleMouseEnter(item)"
              @mouseleave="handleMouseLeave(item)"
            >
              <RouterLink
                v-for="subItem in item.dropdown"
                :key="subItem.to"
                class="nav__dropdown-item"
                :to="subItem.to"
              >
                {{ subItem.label }}
              </RouterLink>
            </div>
          </div>
        </nav>
      </div>

      <div class="actions">
        <RouterLink class="btn actions__login" to="/contact">登录</RouterLink>
        <RouterLink class="btn btn-primary" to="/contact">免费注册</RouterLink>

        <button
          class="hamburger"
          type="button"
          :aria-label="mobileLabel"
          :aria-expanded="mobileOpen ? 'true' : 'false'"
          @click="mobileOpen = !mobileOpen"
        >
          <Icon icon="lucide:menu" width="24" height="24" />
        </button>
      </div>
    </div>

    <div v-if="mobileOpen" class="mobile">
      <div class="mobile__inner container">
        <RouterLink
          v-for="item in navItems"
          :key="item.to"
          class="mobile__link"
          :to="item.to"
        >
          {{ item.label }}
        </RouterLink>
        <div class="divider"></div>
        <RouterLink class="mobile__link" to="/contact">联系咨询</RouterLink>
      </div>
    </div>
  </header>
</template>

<style scoped>
.nav {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 2px;
}

.nav__item {
  position: relative;
}

.nav__link {
  padding: 10px 12px;
  color: var(--slate-700);
  font-size: 18px;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.nav__link:hover {
  color: #1d4ed8;
}

.nav__link.router-link-exact-active {
  color: var(--brand-700);
}

.nav__dropdown-menu {
  position: absolute;
  top: 100%;
  left: 0;
  min-width: 140px;
  background: #fff;
  box-shadow: 0 10px 40px rgba(15, 23, 42, 0.15);
  padding: 8px 10px;
  margin-top: 4px;
  z-index: 100;
  animation: slideDown 0.2s ease-out;
  display: flex;
  flex-direction: column;
  align-items: center;
}

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

.nav__dropdown-item {
  display: block;
  padding: 10px 20px;
  width: 90%;
  height: 50px;
  color: var(--slate-700);
  font-size: 15px;
  transition: all 0.15s ease;
  text-align: center;
}

.nav__dropdown-item:hover {
  border-bottom: 4px solid #0052d9;
  font-weight: bold;
}

.nav__dropdown-item.router-link-exact-active {
  background: rgba(37, 99, 235, 0.12);
  color: var(--brand-700);
  font-weight: 600;
}

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

.app-header {
  position: fixed;
  z-index: 50;
  top: 0;
  left: 0;
  right: 0;
  height: var(--header-h);
  background: rgba(255, 255, 255, 0.86);
  backdrop-filter: blur(14px);
  border-bottom: none;
  box-shadow: 0 12px 20px rgba(15, 23, 42, 0.05);
}

.app-header__inner {
  height: var(--header-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding: 0 20px;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  min-width: 240px;
}

.brand__mark {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  background: radial-gradient(
    circle at 30% 30%,
    var(--brand-200),
    var(--brand-600)
  );
  box-shadow: 0 12px 20px rgba(29, 78, 216, 0.22);
}

.brand__text {
  display: grid;
  line-height: 1.1;
}

.brand__name {
  font-weight: 800;
  letter-spacing: -0.02em;
}

.brand__tag {
  font-size: 12px;
}

.nav {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 2px;
}

.nav__link {
  padding: 10px 12px;
  border-radius: 10px;
  color: var(--slate-700);
  font-size: 18px;
}

.nav__link:hover {
  color: #1d4ed8;
}

.nav__link.router-link-exact-active {
  color: var(--brand-700);
}

.actions {
  display: inline-flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
}

.hamburger {
  display: none;
  width: 42px;
  height: 42px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: #fff;
  cursor: pointer;
}

.hamburger > span {
  display: block;
  height: 2px;
  background: var(--slate-700);
  margin: 5px 10px;
  border-radius: 2px;
}

.mobile {
  border-bottom: 1px solid var(--border);
  background: #fff;
}

.mobile__inner {
  padding: 14px 0 18px;
  display: grid;
  gap: 8px;
}

.mobile__link {
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  font-weight: 650;
}

.mobile__link.router-link-exact-active {
  border-color: rgba(37, 99, 235, 0.35);
  background: rgba(37, 99, 235, 0.08);
  color: var(--brand-700);
}

@media (max-width: 1024px) {
  .brand {
    min-width: auto;
  }

  .nav {
    display: none;
  }

  .actions__login {
    display: none;
  }

  .actions__cta {
    display: none;
  }

  .hamburger {
    display: inline-block;
  }
}
</style>
