<template>
  <!-- 移动端菜单 -->
  <div class="mobile-menu">
    <IconMenu 
      :openState="mobileMenuShow"
      @click="mobileMenuShow = !mobileMenuShow"
    />
    <Teleport to="body">
      <div 
        v-show="mobileMenuShow"
        :class="[
          'mobile-menu-popover',
          mobileMenuShow ? 'visible' : '',
        ]"
        @click="mobileMenuShow=false"
      >
        <div>
          <RouterLink to="/">首页</RouterLink>
          <RouterLink to="/about">保护区概况</RouterLink> 
          <RouterLink to="/news">资讯动态</RouterLink>
          <RouterLink to="/introduction">文化常识</RouterLink>
          <RouterLink to="/inheritor">保护传承</RouterLink>
          <RouterLink to="/communicate">传播交流</RouterLink>
          <RouterLink to="/introduction/policy">政策法规</RouterLink>
          <RouterLink to="/research">理论研究</RouterLink>
          <RouterLink to="/fusion">文旅融合</RouterLink>
        </div>
      </div>
    </Teleport>
  </div>

  <!-- 导航栏 -->
  <nav 
    :class="[
      'main',
      headerBlur ? 'need-blur' : '',
      scrollValue > 200 ? 'nav-scrolled' : 'nav-not-scrolled',
    ]"
  >
    <div></div>
    <div class="group">
      <div class="link-placeholder" />
      <RouterLink to="/">首页</RouterLink>
      <RouterLink to="/about">保护区概况</RouterLink> 
      <RouterLink to="/news">资讯动态</RouterLink>
      <RouterLink to="/introduction">文化常识</RouterLink>
    </div>
    <div class="group center">
      <div class="headerlogos">
        <img class="main-clickable" src="https://mn.wenlvti.net/app_static/minnan/logo-large-light.png" @click="goIndex" />
        <div>
          <p class="large">世界闽南文化交流中心</p>
          <p>闽南文化生态保护区<span>(厦门市)</span></p>
        </div>
      </div>
    </div>
    <div class="group">
      <RouterLink to="/inheritor">保护传承</RouterLink>
      <RouterLink to="/communicate">传播交流</RouterLink>
      <RouterLink to="/introduction/policy">政策法规</RouterLink>
      <RouterLink to="/research">理论研究</RouterLink>
      <RouterLink to="/fusion">文旅融合</RouterLink>
    </div>
    <div></div>
  </nav>
</template>

<script setup lang="ts">
import { computed, onBeforeUnmount, onMounted, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import IconMenu from './icons/IconMenu.vue';

const router = useRouter();
const route = useRoute();
const scrollValue = ref(0);
const mobileMenuShow = ref(false);
const headerBlur = computed(() => {
  return route.name != 'home';
});

function goIndex() {
  router.push({ path: '/' });
}
function onScroll() {
  scrollValue.value = window.scrollY;
}

onMounted(() => {
  window.addEventListener('scroll', onScroll);
});
onBeforeUnmount(() => {
  window.removeEventListener('scroll', onScroll);
});

</script>

<style lang="scss">
@use '@/assets/scss/colors.scss' as *;

$nav-height: 70px;
$nav-color: $primary-color;
$nav-text-color: $text-color-light;

.nav-placeholder {
  height: $nav-height; 
  background-color: $nav-color;
}
nav.main {
  position: fixed;
  display: flex;
  justify-content: space-around;
  align-items: center;
  z-index: 100;
  width: 100%;
  top: 0;
  height: $nav-height;
  background-color: $nav-color;
  border-bottom: 1px solid rgba(#fff, 0.2);
  color: $nav-text-color;
  transition: all ease-in-out 0.3s;

  &.nav-scrolled {
    background-color: $nav-color;
  }
  &.need-blur.nav-not-scrolled {
    //background-color: transparent;
    backdrop-filter: blur(10px);
  }
  a {
    color: rgba($nav-text-color, 0.8);
    text-align: center;
    text-decoration: none;

    &:focus {
      outline: none;
    }
    &.router-link-active, &.router-link-exact-active, &:hover {
      color: $nav-text-color;
    }
    &.router-link-exact-active {
      font-weight: bold;
    }
  }

  .group {
    display: flex;
    gap: 1rem; 

    a, .link-placeholder {
      width: 100px;
      height: $nav-height;
      line-height: $nav-height;
    }

  }
  .headerlogos {
    display: flex;
    flex-direction: row;
    align-items: center;

    img {
      width: 95px;
      height: 55px; 
      margin-right: 10px;
    }
    > div {
      display: flex;
      flex-direction: column;
      justify-content: center;
      font-family: nzgrRuyinZouZhangKai;
      margin-bottom: 6px;

      p {
        margin: 0;
        font-size: 1rem;
        height: 20px;
        letter-spacing: 0.15rem;

        span {
          font-size: 1rem;
          margin-left: 10px;
        }
        &.large {
          height: 33px;
          font-size: 1.6rem;
          letter-spacing: -0.1rem;
        }
      }
    }
  }
}

.mobile-menu {
  display: none;
  position: fixed;
  left: 25px;
  top: 20px;
  z-index: 120;
  width: 30px;
  height: 30px;
  color: $text-color-light;
}
.mobile-menu-popover {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: $box-color;
  border-bottom: $border-grey-color;
  color: $text-color;
  z-index: 101;
  background-color: rgba(#000, 0.1);

  > div {
    display: flex;
    flex-direction: column;
    padding-top: $nav-height;
    height: 100%;
    width: 190px;
    text-align: center;
    background-color: #FBF8F3;
  }

  font-family: SourceHanSerifCNBold;
  animation: mobile-menu-popover-fade 0.3s ease-in-out;

  a {
    line-height: 40px;
    height: 40px;
    width: 100%;
    color: $text-color;
  }

  &.visible {
    display: flex; 
  }
}

@media (max-width: 1460px) {
  nav.main {
    
    .group {
      gap: 0.5rem; 

      a {
        width: 80px;
      }
    }
    .headerlogos > div {
      display: none;
    }
  }
}
@media (max-width: 1024px) {
 
}
@media (max-width: 768px) {
  nav.main {
    justify-content: space-between;
    padding: 0 30px;

    .group:not(.center) {
      display: none;
    }
    .headerlogos > div {
      display: initial;
    }
  }
  .mobile-menu {
    display: block;
  } 
}
@media (max-width: 550px) {
  nav.main {
    .headerlogos {
      img {
        width: 55px;
        height: 35px; 
        margin-right: 7px;
      }
      > div {
        display: initial;
        margin-bottom: 0;

        p {
          font-size: 0.7rem;
          height: 20px;

          span {
            font-size: 0.7rem;
            margin-left: 10px;
          }
          &.large {
            height: 23px;
            font-size: 1.4rem;
            letter-spacing: -0.1rem;
          }
        }
      }
    }
  }
}
@media (max-width: 388px) {
  nav.main {
    .headerlogos > div {
      display: none;
    }
  }
}

@keyframes mobile-menu-popover-fade {
  0% { opacity: 0; transform: translateX(-100px); }
  100% { opacity: 1; transform: translateX(0); }
}
</style>