<template>
  <div class="app-header">
    <div class="top">
      <a> <span class="iconfont icon-youxiang"></span> market@aporesearch.com</a>
      <a><span class="iconfont icon-lianxidianhua"></span> 1-332-251-9412</a>
      <div><span class="iconfont icon-weidenglu"></span>Login/Register</div>
    </div>
    <div class="navBox">
      <a class="navBox_l" href="/">
        <img src="/assets/images/img2.png" alt="">
      </a>
      <div class="navBox_c">
        <n-dropdown :options="reportOptions" trigger="hover" size="huge" @select="chooseReportMenu">
          <a :class="{'router-link-active':(route.name=='reports')}" :href="jumpLink('/report-industries')">
            Reports
          </a>
        </n-dropdown>
        <a :class="{'router-link-active':route.path=='/market'}" :href="jumpLink('/market')">
          Custom Researc
        </a>
        <n-dropdown :options="newsOptions" trigger="hover" size="huge" @select="chooseNewsMenu">
          <a :class="{'router-link-active':(route.name=='newsCategories'||route.path=='/news-categories')}" :href="jumpLink('/news-categories')">
            Industry News
          </a>
        </n-dropdown>
        <n-dropdown :options="aboutOptions" trigger="hover" size="huge" @select="chooseAboutMenu">
          <a :class="{'router-link-active':route.path=='/about'||route.path=='/link'||route.path=='/order'||route.path=='/term'||route.path=='/qualify'}" :href="jumpLink('/about')">
            About Us
          </a>
        </n-dropdown>
        <a :class="{'router-link-active':route.path=='/contactUs'}" :href="jumpLink('/contactUs')">Contact</a>
      </div>
      <div class="navBox_r">
        <div class="search">
          <div class="search_l">
            <n-dropdown trigger="hover" :keyboard="false" :options="selectTypeList">
              <div class="search_l_type">
                Industry News
                <span class="iconfont icon-dkw_guanbi-"></span>
              </div>
            </n-dropdown>
          </div>
          <div class="search_c">
            <n-input v-model:value="searchVal" @keydown.enter="handleSearch()" type="text" clearable placeholder="Please Enter Keywords" />
          </div>
          <div class="search_r" @click="handleSearch()">
            Search
          </div>
        </div>
      </div>
    </div>
    <n-modal :show="signInVisible" preset="dialog" :title="signInTitle" :showIcon="false" :close-on-esc="false" :mask-closable="false" @close="signInVisible = false" :class="{ 'login-dialog': !pcShow }">
      <SignInLogin @closeSginDialog="closeSginDialog" v-if="sginType == 'login'"></SignInLogin>
      <SignInRegister @closeSginDialog="closeSginDialog" v-if="sginType == 'register'"></SignInRegister>
      <SignInForgot @closeSginDialog="closeSginDialog" v-if="sginType == 'forgot'"></SignInForgot>
    </n-modal>
    <n-modal v-model:show="logoutVisible" preset="dialog" title="提示" :showIcon="false" content="确认退出登录吗?" positive-text="确认" negative-text="取消" :close-on-esc="false" :mask-closable="false" @positive-click="submitLogoutCallback" @negative-click="cancelLogoutCallback" :class="{ 'login-dialog': !pcShow }" />
  </div>
</template>

<script lang="ts" setup>
import { useI18n } from "#imports";
import SignInLogin from "@/components/Login/index.vue";
import SignInRegister from "@/components/Login/register.vue";
import SignInForgot from "@/components/Login/forgot.vue";
import { useLocaleStoreWithOut } from "@/store";
import {
  // Component,
  // h,
  onMounted,
  ref,
  onUnmounted,
  onBeforeMount,
  watch,
  computed,
  inject,
  onServerPrefetch,
} from "vue";
import { createDiscreteApi } from "naive-ui";
import { useRouter, useRoute } from "vue-router";
import {
  MdApps,
  MdHome,
  IosPaper,
  IosSearch,
  MdToday,
  MdPeople,
  MdSearch,
  IosCall,
  IosGlobe,
} from "@vicons/ionicons4";
import { useUserStore } from "@/store/user";
const message = createDiscreteApi(["message"]);
const { t, locale, setLocale } = useI18n();
const switchLocalePath = useSwitchLocalePath();
const router = useRouter();
const route = useRoute();
const userStore = useUserStore();
const pcShow = ref(true);
const keyword = ref<string>(""); //关键字
userStore.setLang(locale.value == "en" ? "en-US" : "zh-CN");
const value = userStore.getLang;
const language = ref("");
const langSelected = ref("zh");
language.value = value === "en-US" ? "en" : "";
langSelected.value = value === "en-US" ? "en" : "zh";
const renderIcon = (icon: string) => {
  return () => {
    return h("span", { class: icon });
  };
};
const renderIconC4 = (icon: Component) => {
  return () => {
    return h(NIcon, null, {
      default: () => h(icon),
    });
  };
};

// const renderIconLabel = (option: DropdownOption) => {
//   return h('a',{ onclick: () => chooseMenu, target: '_blank' },{ default: () => option.label});
// }
const reportOptions = [] as any; //报告列表
const newsOptions = [] as any; //行业资讯
const aboutOptions = [] as any; //关于我们
const bulletinOptions = [] as any; // 简报智库

const mobileMenu = [
  { label: t("common.navigate.home"), key: "home", name: "home" },
  {
    label: t("common.navigate.report"),
    key: "reports",
    children: reportOptions,
    name: "reports",
  },
  { label: t("common.navigate.market"), key: "market", name: "marketIndex" },
  {
    label: t("common.navigate.news"),
    key: "newsCategories",
    children: newsOptions,
    name: "newsCategories",
  },
  {
    label: t("common.navigate.aboutUs"),
    key: "about",
    children: aboutOptions,
    name: "about",
  },
  {
    label: t("common.navigate.contactUs"),
    key: "contactUs",
    name: "contactUsIndex",
  },
  // {
  //   label: "简报智库",
  //   key: "bulletin",
  //   children: bulletinOptions,
  //   name: "bulletin",
  // },
];
const selectTypeValue = ref();
const selectTypeList = ref([
  {
    label: t("common.navigate.report"),
    value: "reports",
  },
  {
    label: t("common.navigate.news"),
    value: "newsCategories",
  },
  {
    label: t("common.navigate.bulletin"),
    value: "bulletinThinkTank",
  },
]);
const signInVisible = ref<boolean>(false);
const signInTitle = ref<string>("");
const sginType = ref<string>("login");
const logoutVisible = ref(false);

const userInfo = computed(() => userStore.getUserInfo);
const isLogin = computed(() =>
  userStore.getToken && userStore.getUserInfo ? true : false
);

const jumpLink = (url: string) => {
  return (language.value == "en" ? "/en" : "") + url;
};
watch(
  () => userStore.getSearchType,
  (val) => {
    selectTypeValue.value = val || null;
  }
);

watch(langSelected, () => {
  if (langSelected.value == "en") {
    selectTypeList.value = [
      {
        label: t("common.navigate.report"),
        value: "reports",
      },
      {
        label: t("common.navigate.news"),
        value: "newsCategories",
      },
    ];
    // userStore.resetState();
  }
});

watch(
  () => userStore.getShowLoginDialog,
  (newval, oldval) => {
    signInVisible.value = newval;
    sginType.value = "login";
    signInTitle.value = t("common.login.title");
  }
);

watch(signInVisible, (val) => {
  if (val === false) {
    userStore.setShowLoginDialog(false);
  }
});

// 中英文切换
const langChange = async (lng: string) => {
  userStore.setLang(lng == "zh" ? "zh-CN" : "en-US");
  const path = switchLocalePath(lng);
  setLocale(lng);
  // router.push(path);
  // location.reload();
  location.href = path;
};

// 研究报告菜单切换
function chooseReportMenu(key: string | number, _option: DropdownOption) {
  router.push({
    name: "reports",
    params: { category: key, lang: language.value },
  });
}

// 行业资讯
function chooseNewsMenu(key: string | number) {
  router.push({
    name: "newsCategories",
    params: { marketType: key, lang: language.value },
  });
}

onBeforeMount(async () => {
  pcShow.value = !isMobile();
});

// 关于我们跳转
function chooseAboutMenu(key: string | number, option: DropdownOption) {
  router.push({ name: option.name, params: { lang: language.value } });
}

// 关于简报智库
function chooseBulletinMenu(key: string | number, _option: DropdownOption) {
  router.push({
    name: "bulletinThinkTank",
    params: {
      marketType: _option.marketType,
    },
  });
}

// 手机端跳转
function chooseMenu(key: string | number, option: DropdownOption) {
  if ("reports" === option.name) {
    router.push({
      name: option.name,
      params: { category: key, lang: language.value },
    });
  } else if ("newsCategories" === option.name) {
    router.push({
      name: option.name,
      params: { marketType: key, lang: language.value },
    });
  } else if ("home" === option.name) {
    // router.push({
    //   name: language.value === "" ? "home" : "homeEn",
    //   params: { marketType: key, lang: language.value },
    // });
    jumpLink("/");
  } else {
    router.push({ name: option.name, params: { lang: language.value } });
  }
}

function handleSearch() {
  switch (selectTypeValue.value) {
    case "reports":
      router.push({
        name: "reports",
        params: { lang: language.value, keyword: keyword.value },
      });
      break;
    case "newsCategories":
      router.push({
        name: "newsCategories",
        params: { lang: language.value, keyword: keyword.value },
      });
      break;
    case "bulletinThinkTank":
      router.push({
        name: "bulletinThinkTank",
        params: { marketType: "industry-brief", title: keyword.value },
      });
      break;
    default:
      router.push({
        name: "reports",
        params: { lang: language.value, keyword: keyword.value },
      });
      break;
  }
}

function keyDown() {
  handleSearch();
}

const handleSignIn = () => {
  signInVisible.value = true;
  sginType.value = "login";
  signInTitle.value = t("common.login.title");
};

const toMine = () => {
  router.push({
    name: "mine",
  });
};

const handleLogout = () => {
  logoutVisible.value = true;
};

const submitLogoutCallback = () => {
  logOut_Api().then((res) => {
    message.message.success(t("report.demand.success"));
    userStore.resetState();
    router.push("/");
  });
};

const cancelLogoutCallback = () => {
  logoutVisible.value = false;
};

const closeSginDialog = (type: string) => {
  switch (type) {
    case "success":
      signInVisible.value = false;
      break;
    case "login":
      signInTitle.value = t("common.login.title");
      break;
    case "register":
      sginType.value = type;
      signInTitle.value = t("common.login.register");
      break;
    case "forgot":
      sginType.value = type;
      signInTitle.value = t("common.login.forgetPasswordTitle");
      break;
    default:
      break;
  }
};
// onMounted(async () => {
//window.addEventListener('scroll',onScroll);

// 下拉菜单
const getDictListData = async () => {
  const cacheDict = await getLocalSessionReport();
  cacheDict?.forEach(
    (vo: { dictLabel: any; dictValue: any; dictIcon: any }) => {
      reportOptions.push({
        label: vo.dictLabel,
        key: vo.dictValue,
        icon: renderIcon(vo.dictIcon),
        name: "reports",
      });
    }
  );

  const cacheDictNews = await getLocalSessionNews();
  cacheDictNews?.forEach((vo: any) => {
    newsOptions.push({
      label: vo.dictLabel,
      key: vo.dictValue,
      icon: renderIcon(vo.dictIcon),
      name: "newsCategories",
    });
  });
  aboutOptions.push({
    label: t("common.navAboutUs.comp"),
    name: "about",
    icon: renderIcon("iconfont iconfont icon-ziyuan"),
  });
  aboutOptions.push({
    label: t("common.navAboutUs.method"),
    name: "link",
    icon: renderIcon("iconfont icon-chaxun"),
  });
  aboutOptions.push({
    label: t("common.navAboutUs.order"),
    name: "order",
    icon: renderIcon("iconfont icon-a-dingdanguanli2x"),
  });
  aboutOptions.push({
    label: t("common.navAboutUs.term"),
    name: "term",
    icon: renderIcon("iconfont icon-shejiyukaifa-"),
  });
  aboutOptions.push({
    label: t("common.navAboutUs.qualify"),
    name: "qualify",
    icon: renderIcon("iconfont iconfont icon-ziyuan"),
  });
  bulletinOptions.push({
    label: "行业简报",
    name: "bulletinThinkTank",
    marketType: "industry-brief",
    icon: renderIcon("bqfl-iconfont bqfl-iconfont icon-zhinengjianbao"),
  });
  bulletinOptions.push({
    label: "企业洞察",
    name: "bulletinThinkTank",
    marketType: "enterprise-hole-analysis",
    icon: renderIcon("bqfl-iconfont bqfl-iconfont icon-yanjiubaogao"),
  });
  bulletinOptions.push({
    label: "行业简报",
    name: "bulletinThinkTank",
    marketType: "industry-brief",
    icon: renderIcon("bqfl-iconfont bqfl-iconfont icon-zhinengjianbao"),
  });
};
watch(
  () => route.path,
  (val) => {
    getDictListData();
  }
);
getDictListData();
</script>

<style lang='scss' >
.app-header {
  z-index: 9;
  width: 100%;
  position: fixed;
  left: 0;
  top: 0;
  .top {
    display: flex;
    align-items: center;
    justify-content: end;
    background: rgba(6, 17, 29, 0.5);
    padding: 0 var(--size-60);
    border-bottom: 1px solid rgba(255, 255, 255, 0.2);
    > a,
    > div {
      display: inline-block;
      font-size: var(--size-14);
      color: #ffffff;
      margin-left: var(--size-35);
      height: var(--size-45);
      line-height: var(--size-45);
      cursor: pointer;
      display: flex;
      align-items: center;
      span {
        font-size: var(--size-19);
        display: inline-block;
        margin-right: var(--size-4);
      }
      &:last-child {
        padding-left: var(--size-35);
        border-left: var(--size-1) solid #fff;
      }
    }
  }
  .navBox {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: rgba(6, 17, 29, 0.5);
    padding: 0 var(--size-60);
    border-bottom: 1px solid rgba(255, 255, 255, 0.2);
    height: var(--size-85);
    .navBox_l {
      font-size: var(--size-34);
      color: #ffffff;
      img {
        width: var(--size-366);
        height: var(--size-57);
        display: block;
      }
    }
    .navBox_c {
      font-size: var(--size-18);
      color: #ffffff;
      display: flex;
      cursor: pointer;
      > a {
        padding: var(--size-7) var(--size-43);
        border-right: var(--size-1) solid rgba(255, 255, 255, 0.5);
      }
    }
    .navBox_r {
      .search {
        display: flex;
        align-items: center;
        width: var(--size-450);
        height: var(--size-44);
        background: #ffffff;
        border-radius: 8px;
        overflow: hidden;
        .search_l {
          width: var(--size-148);
          font-size: var(--size-14);
          text-align: center;
          cursor: pointer;
          .search_l_type {
            span {
              transform: rotateZ(90deg);
              display: inline-block;
              color: #1a1a1a;
            }
          }
        }
        .search_c {
          border-left: 1px solid #cccccc;
          .n-input {
            --n-height: var(--size-26) !important;
            line-height: var(--size-26) !important;
          }
        }
        .search_r {
          width: var(--size-105);
          font-size: var(--size-16);
          color: #ffffff;
          text-align: center;
          height: var(--size-44);
          background: linear-gradient(0deg, #7b9c4f 0%, #2da19d 100%), #1a1a1a;
          line-height: var(--size-44);
          cursor: pointer;
        }
      }
    }
  }
}
.n-input {
  --n-border: none !important;
  --n-border-hover: none !important;
  --n-border-focus: none !important;
  --n-box-shadow-focus: none !important;
  --n-caret-color: #ef001f !important;
  font-size: var(--size-16) !important;
  --n-text-color-hover: none !important;
}
</style>