<template>
  <header className="header flex items-center justify-between">
    <div class="logo">
      <img src="@/assets/image/logo.png" alt="logo" />
    </div>
    <div class="tabs">
      <!-- <div class="tab-item pt-[10px]" @click="handleLanguageChange">
        {{ currentLanguageLabel }}
        <img
          src="@/assets/image/home/768/icon-language.png"
          alt="language"
          class="w-[18px] ml-[13px]"
        />
      </div> -->

      <div class="ml-[53px] cursor" @click="handleMenuClick">
        <img
          src="@/assets/image/home/768/icon-menu.png"
          class="w-[115px]"
          alt="logo"
        />
      </div>
    </div>

    <n-drawer
      v-model:show="state.dialogMenu"
      default-width="100%"
      resizable
      placement="left"
    >
      <n-drawer-content>
        <template #header>
          <div class="flex flex-wrap items-center justify-between px-[143px]">
            <img src="@/assets/image/logo.png" class="w-[270px]" alt="logo" />
            <img
              src="@/assets/image/home/768/icon-header-close.png"
              class="w-[35px] h-[35px] cursor"
              alt="close"
              @click="state.dialogMenu = false"
            />
          </div>
        </template>
        <div class="flex flex-wrap font-semibold text-[35px] text-[#10253E]">
          <div
            class="w-full flex justify-between px-[188px] pt-[50px] pb-[46px] items-center"
            @click="handleTabClick('home')"
          >
            {{ t("home.nav.home") }}
            <img
              src="@/assets/image/home/768/icon-menu-right.png"
              class="w-[22px] h-[38px]"
            />
          </div>

          <div class="divider3"></div>
          <div
            class="w-full flex justify-between px-[188px] pt-[50px] pb-[46px] items-center"
            @click="handleTabClick('companyprofil')"
          >
            {{ t("home.nav.company") }}
            <img
              src="@/assets/image/home/768/icon-menu-right.png"
              class="w-[22px] h-[38px]"
            />
          </div>
          <div class="divider3"></div>
          <div
            class="w-full flex justify-between px-[188px] pt-[50px] pb-[46px] items-center"
            @click="handleTabClick('businessintroduction')"
          >
            {{ t("home.nav.businessintroduction") }}
            <img
              src="@/assets/image/home/768/icon-menu-right.png"
              class="w-[22px] h-[38px]"
            />
          </div>

          <div class="divider3"></div>
          <div
            class="w-full flex justify-between px-[188px] pt-[50px] pb-[46px] items-center"
            @click="handleTabClick('investor')"
          >
            {{ t("home.nav.investor") }}
            <img
              src="@/assets/image/home/768/icon-menu-right.png"
              class="w-[22px] h-[38px]"
            />
          </div>

          <div class="divider3"></div>
        </div>
      </n-drawer-content>
    </n-drawer>
  </header>

  <!-- 语言选择模态框 - 使用纯div实现 -->
  <div v-if="showLanguageModal" class="language-modal">
    <div class="language-modal-backdrop" @click="closeLanguageModal"></div>
    <div class="language-modal-content">
      <div class="modal-header font-semibold flex items-center">
        {{ $t("home.nav.please_select") }}
        <img
          src="@/assets/image/home/768/icon-close.png"
          alt="close"
          class="close-btn w-[102px]"
          @click="closeLanguageModal"
        />
      </div>
      <div class="language-modal-body">
        <div
          v-for="option in languageOptions"
          :key="option.value"
          class="language-option"
          :class="{ active: selectedLanguage === option.value }"
          @click="selectedLanguage = option.value"
        >
          {{ option.label }}
        </div>
      </div>
      <div class="language-modal-footer">
        <button class="confirm-btn" @click="confirmLanguageChange">
          {{ $t("home.nav.confirm_select") }}
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { onUnmounted, ref, watch, onMounted, computed, reactive } from "vue";
import { useI18n } from "vue-i18n";
const { t } = useI18n();
import { useHome } from "@/store/home/index.js";
const { currentTab } = useHome();
import { useRouter } from "vue-router";
const router = useRouter();
import { useLanguage } from "@/utils/changeLanguage.js";
import { NDrawer, NDrawerContent } from "naive-ui";
const {
  languageOptions,
  currentLanguageLabel,
  changeLanguage,
  initLanguage,
  currentLang,
} = useLanguage();

onMounted(() => {
  initLanguage();
});

const state = reactive({
  dialogMenu: false,
});
const handleMenuClick = () => {
  state.dialogMenu = true;
};
const emit = defineEmits(["triggerLanguageChange"]);
const handleTabClick = (tab) => {
  currentTab.value = tab;
  router.push("/" + tab);
};
// 语言选择模态框相关变量
const showLanguageModal = ref(false);
// 默认选中的语言,从localStorage获取,如果没有则默认为简体中文
const selectedLanguage = ref(localStorage.getItem("language") || "zh");

// 打开语言选择模态框
const handleLanguageChange = () => {
  showLanguageModal.value = true;
  // 打开时设置当前选中的语言
  selectedLanguage.value = localStorage.getItem("language") || "zh";
  // 禁止背景滚动
  document.body.style.overflow = "hidden";
};

// 确认语言选择
const confirmLanguageChange = () => {
  changeLanguage(selectedLanguage.value);
  emit("triggerLanguageChange", selectedLanguage.value);
  // 关闭模态框
  showLanguageModal.value = false;
  // 恢复背景滚动
  document.body.style.overflow = "";
  window.location.reload(true);
};

// 关闭语言选择模态框
const closeLanguageModal = () => {
  showLanguageModal.value = false;
  // 恢复背景滚动
  document.body.style.overflow = "";
};
</script>
<style scoped lang="scss">
.header {
  width: 100%;
  height: 155px;
  position: fixed;
  z-index: 10;
  top: 0;
  left: 0;
  right: 0;
  padding: 0 200px 0 188px;
  background-color: #fff;
}
.logo {
  img {
    width: 270px;
    height: 83px;
  }
}

.tabs {
  display: flex;
  align-items: center;
}
.tab-item {
  display: flex;
  align-items: center;
  font-size: 30px;
  color: #000000;
  cursor: pointer;
  transition: color 0.3s ease;

  &.active {
    color: #8b59fa;
  }

  &:hover {
    color: #8b59fa;
  }
}

/* 语言选择模态框样式 */
.language-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1000;
  touch-action: none; /* 防止触摸事件影响背景 */
}

.language-modal-backdrop {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 1001;
}

.language-modal-content {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  max-height: 90vh; /* 最大高度为视口高度的90% */
  background-color: #fff;
  z-index: 1002;
  box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column;
  overflow: hidden; /* 内容溢出隐藏 */
}

.modal-header {
  padding: 33px 38px 55px 38px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 40px;
  color: #000000;
  position: relative;
  flex-shrink: 0; /* 不允许头部收缩 */
}

.close-btn {
  cursor: pointer;
}

.language-modal-body {
  padding: 10px 0;
  overflow-y: auto; /* 只有内容区域可滚动 */
  -webkit-overflow-scrolling: touch; /* 为iOS设备提供平滑滚动 */
  flex: 1; /* 内容区域填充剩余空间 */
  overscroll-behavior: contain; /* 阻止滚动穿透 */
}

.language-option {
  padding: 30px 0;
  font-size: 40px;
  color: #333;
  cursor: pointer;
  transition: background-color 0.3s;
  text-align: center;
}

.language-option:hover,
.language-option.active {
  background-color: #f5f5f5;
  color: #8b59f7;
}

.language-modal-footer {
  padding: 40px 38px;
  display: flex;
  justify-content: center;
  flex-shrink: 0; /* 不允许底部收缩 */
}

.confirm-btn {
  background-color: #8b59f7;
  color: white;
  border: none;
  border-radius: 10px;
  padding: 23px 0;
  width: 100%;
  font-size: 40px;
  cursor: pointer;
}

.confirm-btn:hover {
  background-color: #7a48e6;
}

.divider3 {
  width: 100%;
  height: 10px;
  background-image: linear-gradient(to right, #e6eaee 50%, transparent 50%);
  background-size: 40px 6px; // 第一个值是宽度,第二个值是虚线的总长度(实线+空白)
  background-repeat: repeat-x;
}
</style>