<template>
  <el-affix>
    <div class="bgf7 head-top">
      <div class="new-year-head">
        <div class="tip">Welcome to the official website of Shenzhen Hotking Electronics Co., Ltd.</div>
        <ul>
          <li>
            <a href="javascript:;" class="top-link new-fun">Hotline: 0755-82565333</a>
          </li>
          <li>
            <el-dropdown placement="bottom-end" @visible-change="visibleChangeLanguage" popper-class="language-popper">
              <span class="el-dropdown-link">
                <i class="icon"></i>Global Site
                <el-icon :class="{transformC:visibleChangeLanguageFalg}">
                  <ArrowDown />
                </el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu class="language-wrap">
                  <el-dropdown-item>
                    <div class="language-ul">
                      <div class="language-li">
                        <a :href="useRuntimeConfig().public.enUrl">China Site</a>
                      </div>
                      <div class="language-li current">
                        <a href="javascript:;">Global Site</a>
                      </div>
                    </div>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </li>
        </ul>
      </div>
    </div>
    <div class="p-header">
      <div class="max-w1280">
        <div class="f-c-fs head-logo">
          <div class="p-header-left">
            <NuxtLink to="/">
              <img src="/img/logo.png" class="p-logo" loading="lazy" width="250" height="52" alt="logo" />
            </NuxtLink>
            <div class="head-nav-wrap">
              <el-menu :default-active="activeIndex" :ellipsis="false" mode="horizontal" class="custom-menu" @select="handleSelect">
                <el-menu-item index="/" class="navigation">
                  <NuxtLink to="/" class="nav_text">Home</NuxtLink>
                </el-menu-item>
                <el-menu-item index="/newProduct/flq" class="navigation">
                  <NuxtLink to="/newProduct/flq?sort=2" class="nav_text">Products</NuxtLink>
                  <div class="list-link level-1-menu">
                    <NuxtLink to="/newProduct/flq?sort=2" class="has-children">Active Components
                      <div class="children level-2-menu">
                        <NuxtLink class="has-children level-3-trigger" v-for="item in flqData" :key="item.id" :to="{ path: '/newProduct/'+item.id+'/'+item.children[0].id, query: { page: 1, name: item.children[0].name } }">{{ item.name }}
                          <div class="children level-3-menu" v-if="item.children?.length>0">
                            <NuxtLink v-for="i in item.children" :key="i.id" :to="{ path: '/newProduct/'+item.id+'/'+i.id, query: { page: 1, name: i.name } } ">{{i.name}}</NuxtLink>
                          </div>
                        </NuxtLink>
                      </div>
                    </NuxtLink>
                    <NuxtLink to="/newProduct/flq?sort=1" class="has-children">Passive Components
                      <div class="children level-2-menu">
                        <NuxtLink class="has-children level-3-trigger" v-for="item in dzData" :key="item.id" :to="{ path: '/cproduct/'+item.id+'/'+item.children[0].id, query: { page: 1, seekType: 1, sort: 1 } }">{{ item.name }}
                          <div class="children level-3-menu" v-if="item.children?.length>0">
                            <NuxtLink v-for="i in item.children" :key="i.id" :to="{ path: '/cproduct/'+item.id+'/'+i.id, query: { page: 1, seekType: 1, sort: 1 } }">{{i.name}}</NuxtLink>
                          </div>
                        </NuxtLink>
                      </div>
                    </NuxtLink>
                  </div>
                </el-menu-item>
                <el-menu-item index="/pdtion/finfo" class="navigation">
                  <NuxtLink :to="{ path: '/pdtion/finfo'}" class="nav_text">Manufacturing</NuxtLink>
                  <div class="list-link">
                    <NuxtLink to="/pdtion/finfo" class="">Packaging & Testing</NuxtLink>
                    <NuxtLink to="/pdtion/encapsulation" class="">Package Types</NuxtLink>
                    <NuxtLink to="/pdtion/pktx" class="">Quality Control</NuxtLink>
                    <NuxtLink to="/pdtion/sysb" class="">Laboratory Center</NuxtLink>
                  </div>
                </el-menu-item>
                
                <el-menu-item index="/application" class="navigation">
                  <NuxtLink to="/application" class="nav_text">Applications</NuxtLink>
                </el-menu-item>
                <el-menu-item index="/news" class="navigation">
                  <NuxtLink :to="{path:'/news',query:{type:40,current:1}}" class="nav_text" @click="handleNewsClick(40)">News</NuxtLink>
                  <div class="list-link">
                    <NuxtLink :to="{path:'/news',query:{type:40,current:1}}" class="" @click="handleNewsClick(40)">Company News</NuxtLink>
                    <NuxtLink :to="{path:'/news',query:{type:41,current:1}}" class="" @click="handleNewsClick(41)">Industry News</NuxtLink>
                  </div>
                </el-menu-item>
                <el-menu-item index="/about/Introduction" class="navigation">
                  <NuxtLink :to="{ path: '/about/Introduction'}" class="nav_text">About Us</NuxtLink>
                  <div class="list-link">
                    <NuxtLink to="/about/Introduction#company-intro" class="">Company Profile</NuxtLink>
                    <NuxtLink to="/about/Introduction#company-culture" class="">Corporate Culture</NuxtLink>
                    <NuxtLink to="/about/Introduction#company-history" class="">Development History</NuxtLink>
                    <NuxtLink to="/about/Introduction#company-award" class="">Awards</NuxtLink>
                    <NuxtLink to="/about/zzzl" class="">Certificates & Patents</NuxtLink>
                    <NuxtLink to="/suppliers" class="">Supplier Lookup</NuxtLink>
                  </div>
                </el-menu-item>
                <el-menu-item index="/contact" class="navigation">
                  <NuxtLink to="/contact" class="nav_text">Contact Us</NuxtLink>
                </el-menu-item>
              </el-menu>
            </div>

          </div>
        </div>
      </div>
    </div>
    <cart></cart>
  </el-affix>
</template>
<script setup lang="ts">
import { Search, ArrowDown, Close } from "@element-plus/icons-vue";
import { useAll } from "~/api/pubAll/service";
let searchVal: Ref<string> = ref("");
const route = useRoute();
const { getGoodsgoryList } = useAll();
const user = useUserStore();
let activeIndex = ref("1");
type HeaderCategoryItem = {
  id: string | number;
  level?: string | number;
  nameEn?: string;
  name?: string;
  subCatgory?: HeaderCategoryItem[];
  children?: HeaderCategoryItem[];
};

/**
 * 提取分类接口返回的真实列表，兼容不同响应包裹层。
 * @param res 分类接口响应数据。
 * @returns 分类列表或空数组。
 * @throws 不主动抛出异常。
 */
function getCategoryPayload(res: any) {
  return res?.data?.data || res?.data || res || [];
}

/**
 * 将接口分类字段转换为头部菜单原有字段结构。
 * @param res 分类接口响应数据。
 * @returns 头部菜单可直接渲染的分类列表。
 * @throws 不主动抛出异常。
 */
function normalizeHeaderCategoryList(res: any) {
  const rawList = getCategoryPayload(res);
  if (!Array.isArray(rawList)) return [];

  return rawList
    .map((item: HeaderCategoryItem) => ({
      id: item.id,
      level: item.level,
      name: item.nameEn || item.name || "",
      children: (item.subCatgory || item.children || []).map((sub: HeaderCategoryItem) => ({
        id: sub.id,
        level: sub.level,
        name: sub.nameEn || sub.name || "",
      })),
    }))
    .filter((item) => item.children.length > 0);
}

const { data: dzCategoryData } = await useAsyncData("header-category-sort-1", () =>
  getGoodsgoryList({ sort: 1 })
);
const { data: flqCategoryData } = await useAsyncData("header-category-sort-2", () =>
  getGoodsgoryList({ sort: 2 })
);

const dzData = computed(() => normalizeHeaderCategoryList(dzCategoryData.value));
const flqData = computed(() => normalizeHeaderCategoryList(flqCategoryData.value));
const visibleChangeLanguageFalg = ref(false);

const clubFalg: any = useCookie("clubFalg", {
  maxAge: 60 * 5,
  sameSite: "strict", // 防止 CSRF 攻击
});
function visibleChangeLanguage(e: any) {
  visibleChangeLanguageFalg.value = e;
}
const handleSelect = (key: string, keyPath: string[]) => {};
function routeChange() {
  if (route.path == "/") {
    activeIndex.value = "/";
  } else if (route.path == "/about/Introduction") {
    activeIndex.value = "/about/Introduction";
  } else if (route.path == "/about") {
    activeIndex.value = "/about/Introduction";
  } else if (route.path == "/news") {
    activeIndex.value = "/news";
  } else if (route.path == "/application") {
    activeIndex.value = "/application";
  } else if (route.path == "/contact") {
    activeIndex.value = "/contact";
  } else if (route.path == "/newProduct/flq") {
    activeIndex.value = "/newProduct/flq";
  } else {
    activeIndex.value = "-";
  }
}
if (route.query.key) {
  searchVal.value = route.query.key as string;
}

watch(
  () => route.path,
  (newValue: any, oldValue: any) => {
    routeChange();
  },
  { immediate: true }
);

function handleNewsClick(type: any) {
  navigateTo({
    path: "/news",
    query: {
      type: type,
      current: 1,
      size: 6,
    },
  });
}
</script>
<style scoped lang="scss">
.p-header {
  justify-content: space-between;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
  background: #fff;
  z-index: 999;
  position: sticky;
  & > .max-w1280 {
    padding: 0 20px;
    width: 90%;
    max-width: 1600px;
  }
  .p-header-left {
    flex: 1;
    height: 90px;
    padding-right: 0px;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
.p-header-left {
  :deep(.el-input-group__append) {
    padding: 0;
    box-shadow: unset;
    border-radius: 35px;
    background: #eff1f2;
    margin-left: -92px;
    .search_btn {
      width: 92px;
      margin-left: 0;
      border-radius: 35px !important;
      border: none !important;
      height: 36px !important;
    }
  }
}
.p-logo {
  width: 250px;
  min-width: 250px;
  display: block;
}
.el-menu--horizontal.el-menu {
  border-bottom: none;
}
.el-menu-item.is-disabled {
  opacity: 1;
}
.el-menu--horizontal .el-menu-item:not(.is-disabled) {
  &:hover,
  &:focus {
    background: none;
  }
}
.custom-menu {
  margin-left: 0px;
  height: 36px;
  display: flex;
  align-items: center;
  position: relative;
  .el-menu-item {
    padding: 0 0px;
    margin-right: 45px;
    height: 100%;
    position: relative;
    &:hover {
      .list-link {
        opacity: 1;
        top: calc(100% + 2px);
        height: auto;
        overflow: unset;
      }
    }
    .is-active {
      color: $mainColor !important;
    }
    .list-link {
      position: absolute;
      left: -20px;
      opacity: 0;
      height: 0;
      overflow: hidden;
      top: calc(100% + 20px);
      background: #fff;
      width: -webkit-max-content;
      width: -moz-max-content;
      width: max-content;
      // min-width: 124px;
      // width: 124px;
      line-height: 40px;
      font-size: 14px;
      border: 1px solid #f4f4f4;
      border-radius: 3px;
      z-index: 1000;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
      padding: 0;
      transition: all 0.2s linear;
      a {
        display: block;
        text-align: center;
        padding: 0 10px;
        color: #333;
        &:hover {
          color: $mainColor;
        }
      }
    }
    &:last-child {
      margin-right: 0;
    }
    &.lastNavigation {
      margin-right: 0;
      position: absolute;
      right: 0;
    }
  }
}
.nav_text {
  height: 40px;
  font-size: 16px;
  font-family: PoppinsRegular;
  font-weight: bold;
  line-height: 40px;
  position: relative;
  padding: 0 10px;
  i {
    position: absolute;
    top: -10px;
    bottom: 0;
    left: 36px;
    width: 32px;
    height: 20px;
    font-size: 12px;
    color: #ffffff !important;
    font-style: normal;
    z-index: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    span {
      transform: scale(0.8);
      display: block;
      font-weight: 400;
    }
    .el-image {
      width: 32px;
      height: 20px;
      position: absolute;
      top: 0;
      left: 0;
      z-index: -1;
    }
    :deep(img) {
      width: 32px;
      height: 20px;
    }
  }
}
.btn_second {
  width: 90px;
  height: 40px;
  background: #000;
  color: #fff;
}
.btn_main {
  width: 90px;
  margin-right: 0px;
  margin-left: 32px;
}

.login-wrap {
  display: flex;
  justify-content: flex-end;
  margin-left: 0;
  align-items: center;
  a + a {
    margin-left: 25px;
  }
  .el-button.is-text {
    height: 36px !important;
  }
}
.search-box {
  position: relative;
  width: 100%;
  .history-panel {
    position: absolute;
    left: 65px;
    z-index: 2;
    width: 660px;
    margin-top: 10px;
    box-shadow: 0 0 18px rgba(29, 29, 31, 0.2);
    background: #fff;
    border-radius: 8px;
  }
  .panel-header {
    display: flex;
    justify-content: space-between;
    padding: 16px 24px 8px;
    font-size: 16px;
    color: rgba(0, 0, 0, 0.3);
    .btn {
      color: $mainColor;
      cursor: pointer;
    }
  }
  .history-list {
    padding-bottom: 14px;
    li {
      height: 36px;
      display: flex;
      align-items: center;
      padding: 0 32px;
      font-size: 14px;
      color: #000;
      cursor: pointer;
      justify-content: space-between;
      span {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        max-width: 95%;
      }
      &:hover {
        background: rgba(0, 0, 0, 0.05);
      }
    }
  }
}
.search-wrap {
  margin-left: 55px;
  margin-right: 0px;
  display: flex;
  :deep(.el-input__wrapper) {
    background: #f3f3f3;
    border-radius: 35px;
    box-shadow: inset 0 0 0 1px #eff1f2;
    font-size: 16px;
    height: 36px;
    line-height: 36px;
    padding: 1px 20px;
    max-width: 680px;
    .el-input__prefix {
      font-size: 20px;
      color: #888;
    }
    .el-input__inner {
      height: 36px;
      padding-right: 85px;
    }
    &.is-focus {
      box-shadow: 0 0 0px 1px #ff004f inset !important;
      background: #fff;
    }
  }
}
.header-balance {
  display: flex;
  font-weight: 400;
  font-size: 16px;
  color: #333333;
  align-items: center;
  height: 36px;
  border-radius: 24px 24px 24px 24px;
  border: 1px solid #dcdcdc;
  justify-content: center;
  padding: 0 20px;
  margin-right: 24px;
}

.cart-icon {
  width: 30px;
  display: flex;
  align-items: center;
  position: relative;
  cursor: pointer;
  i {
    position: absolute;
    width: 20px;
    height: 18px;
    line-height: 20px;
    font-size: 12px;
    background: #ff004f;
    color: #fff;
    border-radius: 20px;
    text-align: center;
    top: -5px;
    right: -10px;
    font-style: unset;
  }
  .p-cart {
    width: 36px;
    height: 36px;
  }
}
.el-button.is-text:not(.is-disabled):hover {
  background-color: unset;
}
.btn_reg {
  border-color: $mainColor;
  color: $mainColor;
  width: 152px;
  border-radius: 50px !important;
  height: 36px !important;
  &:hover {
    background-color: $mainColor;
    color: #fff;
  }
}

.head-top {
  display: flex;
  height: 32px;
  justify-content: center;
  ul {
    display: flex;
    align-items: center;
    width: 1600px;
    min-width: 1600px;
    justify-content: flex-end;
    color: #90939a;
    font-size: 12px;
    li {
      position: relative;
      line-height: 22px;
      & + li {
        margin-left: 24px;
        &::before {
          content: "";
          position: absolute;
          height: 12px;
          width: 1px;
          background: #dcdcdc;
          top: 5px;
          left: -12px;
        }
      }
    }
  }
  .el-dropdown {
    font-size: 12px;
    color: #90939a;
    line-height: unset;
    &:focus-visible {
      outline: unset;
    }
    .el-dropdown-link {
      display: flex;
      align-items: center;
      cursor: pointer;
      &:focus-visible {
        outline: unset;
      }
      i.icon {
        background: url(~/assets/img/ico/language.png) no-repeat center center;
        background-size: 16px 16px;
        width: 16px;
        height: 16px;
        display: block;
        margin-right: 4px;
      }
      .el-icon {
        transition: all 0.4s ease-out;
        svg {
          color: #90939a;
        }
      }
      .transformC {
        transform: rotate(180deg);
      }
    }
  }
}
.head-nav-wrap {
  height: 36px;
  .navigation {
    &:hover .level-1-menu {
      display: block;
      left: 0;
    }
  }
}
.head-logo {
  justify-content: space-between;
}
.code-ul {
  display: flex;
  align-items: center;
  .code-li {
    text-align: center;
    & + .code-li {
      margin-left: 24px;
    }
    p {
      margin-top: 12px;
    }
    img {
      width: 120px;
    }
  }
}
.ewmCode {
  padding: 16px;
  :deep(.el-dropdown-menu__item) {
    padding: 0;
  }
}

.language-ul {
  display: flex;
  align-items: flex-start;
  flex-direction: column;
  .language-li {
    text-align: center;
    position: relative;
    &.current {
      a {
        &::after {
          content: "";
          position: absolute;
          width: 8px;
          height: 8px;
          background: $mainColor;
          border-radius: 50%;
          left: 4px;
          top: 8px;
        }
      }
    }
    &:hover {
      a {
        color: $mainColor !important;
      }
    }
    a {
      line-height: 24px;
      font-size: 14px;
      padding-left: 26px;
      color: #666666 !important;
      &::before {
        content: "";
        position: absolute;
        width: 16px;
        height: 16px;
        border-radius: 50%;
        border: 1px solid #999999;
        left: 0;
        top: 4px;
      }
    }
    & + .language-li {
      margin-top: 5px;
    }
  }
}
.language-wrap {
  padding: 16px;
  :deep(.el-dropdown-menu__item) {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    padding: 0;
    .title {
      color: #333;
      font-size: 16px;
      font-weight: bold;
      line-height: 20px;
      margin-bottom: 12px;
    }
  }
}
.head-top .new-fun {
  background: #ff004f !important;
  color: #fff !important;
  border-radius: 25px;
  height: 20px;
  display: flex;
  align-items: center;
  padding: 0 5px;
}
.qrCode {
  width: 120px;
}
// 公告 start
.scroll-container {
  /* 设置滚动区域的宽度和高度 */
  overflow: hidden;
  white-space: nowrap;
  position: relative;
  display: flex;
  align-items: center;
  width: 500px;
}
.scroll-content {
  display: inline-block;
  white-space: nowrap;
  position: absolute;
  animation: scroll-left 50s linear infinite; /* 动画30秒循环，匀速滚动 */
  .content-wrap {
    display: flex;
    padding-right: 50px;
    font-size: 13px;
    color: $mainColor;
    cursor: pointer;
    .con {
      display: flex;
      align-items: center;
    }
  }
}
@keyframes scroll-left {
  0% {
    transform: translateX(550px);
  }
  100% {
    transform: translateX(-100%);
  }
}
.head-top .new-year-head {
  max-width: 1600px;
  width: 90%;
  padding: 0 20px;
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  ul {
    width: 650px;
    min-width: 650px;
  }
  .tip {
    display: flex;
    align-items: center;
  }
}
.level-1-menu {

  > a {
    display: block;
    text-align: center;
    padding: 0 10px;
  }

  // 二级菜单（level-2）- 对齐level-1顶部
  .level-2-menu {
    position: absolute;
    left: 100%; // 与level-1宽度一致，刚好衔接
    top: 0; // 核心：对齐level-1的顶部
    width: max-content;
    min-width: 100px;
    border: 1px solid #f4f4f4;
    display: none;
    background: #fff;
    z-index: 999;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);

    > a {
      display: block;
      padding: 0 10px;
      white-space: nowrap;
    }

    // 三级菜单（level-3）- 同样对齐level-1顶部
    .level-3-menu {
      position: absolute;
      left: 100%; // level-1 + level-2 宽度之和，衔接显示
      top: 0; // 核心：和level-2一样对齐level-1的顶部
      width: max-content;
      min-width: 100px;
      border: 1px solid #f4f4f4;
      display: none;
      background: #fff;
      z-index: 998;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);

      > a {
        display: block;
        padding: 0 10px;
        white-space: nowrap;
      }
    }

    // 三级菜单触发
    .level-3-trigger {
      &:hover {
        background-color: #f8f8f8;
      }
      &:hover > .level-3-menu {
        display: block !important;
      }
    }
  }

  // 二级菜单触发
  > a.has-children {
    &:hover {
      background-color: #f8f8f8;
    }
    &:hover > .level-2-menu {
      display: block !important;
    }
  }
}
// 公告 end
/* 仅覆盖产品中心多级下拉效果，修正宽度、层级展开和 hover 断层问题。 */
.head-nav-wrap {
  .navigation {
    .level-1-menu {
      min-width: max-content;
      width: max-content !important;

      .has-children {
        position: relative;
      }

      .level-2-menu,
      .level-3-menu {
        width: max-content;
        min-width: 100px;
        opacity: 0;
        visibility: hidden;
        pointer-events: none;
        transition: opacity 0.2s ease;
      }

      .level-2-menu {
        left: calc(100% - 1px);
        top: -1px;
      }

      .level-3-menu {
        left: calc(100% - 1px);
        top: -1px;
      }

      > a.has-children:hover > .level-2-menu,
      .level-3-trigger:hover > .level-3-menu {
        opacity: 1;
        visibility: visible;
        pointer-events: auto;
      }
    }
  }
}
</style>
<style lang="scss">
.language-wrap .el-dropdown-menu__item:not(.is-disabled):focus {
  background: unset;
  color: unset;
}
.language-popper.el-popper.is-light {
  max-width: unset;
  min-width: 120px;
}
.ewmCode .el-dropdown-menu__item:not(.is-disabled):focus {
  background: unset;
  color: unset;
}
.ewmCode-popper.el-popper.is-light {
  max-width: unset;
}
</style>
