<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 v-if="authStore.loginType === 0" to="/inheritor">我的</RouterLink>
          <RouterLink v-else-if="authStore.loginType === 1" to="/admin">管理员</RouterLink>
        </div>
      </div>
    </Teleport>
  </div>

  <!-- 导航栏 -->
  <nav 
    :class="[
      'main',
      headerBlur ? 'need-blur' : '',
      scrollValue > 200 ? 'nav-scrolled' : 'nav-not-scrolled',
    ]"
  >
    <div></div>
    <div class="group">
    </div>
    <div class="group center">
      <div class="headerlogos">
        <img class="main-clickable" src="@/assets/images/LogoIcon.png" @click="goIndex" />
        <div>
          <p class="large">{{ TITLE }}</p>
          <p>闽南文化生态保护区<span>(厦门市)</span></p>
        </div>
      </div>
    </div>
    <div class="group">
    </div>
    
    <a-dropdown v-if="authStore.isLogged" :trigger="['click']">
      <a-image :src="IconUser" class="right-button" :preview="false" />
      <template #overlay>
        <a-menu>
          <a-menu-item key="3" @click="logout">退出登录</a-menu-item>
        </a-menu>
      </template>
    </a-dropdown>
    <div v-else></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';
import IconUser from '@/assets/images/IconUser.png';
import { TITLE } from '@/common/ConstStrings';
import { useAuthStore } from '@/stores/auth';
import { Modal } from 'ant-design-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;
}

const authStore = useAuthStore();

function logout() {
  Modal.confirm({
    title: '确认退出登录吗？',
    okText: '确认',
    okType: 'danger',
    onOk: async () => {
      await authStore.logout();
      router.push('/');
    }
  })
}

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

</script>

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

$nav-height: 70px;

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

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

    &:focus {
      outline: none;
    }
    &.router-link-active, &.router-link-exact-active, &:hover {
      color: #fff;
    }
    &.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: 45px;
      height: 45px; 
      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.35rem;

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

  .right-button {
    width: 30px;
    height: 30px;
    cursor: pointer;
  }
}

.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: 35px;
        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.15rem;
          }
        }
      }
    }
  }
}
@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>