<template>
  <ConfigProvider v-if="!isLock" :locale="locale">
    <RouterView />
  </ConfigProvider>
  <transition v-if="isLock && $route.name !== 'login'" name="slide-up">
    <LockScreen />
  </transition>
</template>

<script lang="ts" setup>
  import { computed, onMounted, onUnmounted } from 'vue';
  import { ConfigProvider } from 'ant-design-vue';
  import { LockScreen } from '@/components/Lockscreen';
  import { useLockscreenStore } from '@/store/modules/lockscreen';
  import { useRoute } from 'vue-router';
  import zhCN from 'ant-design-vue/es/locale/zh_CN';
  import dayjs from 'dayjs';
  import { useProjectSetting } from '@/hooks/setting/useProjectSetting';
  import Watermark from '@/utils/wartermark';
  import 'dayjs/locale/zh-cn';
  dayjs.locale('zh-cn');

  const route = useRoute();
  const locale = zhCN;
  const useLockscreen = useLockscreenStore();
  const isLock = computed(() => useLockscreen.isLock);
  const lockTime = computed(() => useLockscreen.lockTime);
  const {getIsWaterMark} = useProjectSetting();
  let timer;

  const timekeeping = () => {
    clearInterval(timer);
    if (route.name == 'login' || isLock.value) return;
    // 设置不锁屏
    useLockscreen.setLock(false);
    // 重置锁屏时间
    useLockscreen.setLockTime();
    timer = setInterval(() => {
      // 锁屏倒计时递减
      useLockscreen.setLockTime(lockTime.value - 1);
      if (lockTime.value <= 0) {
        // 设置锁屏
        useLockscreen.setLock(true);
        return clearInterval(timer);
      }
    }, 1000);
  };

  onMounted(() => {
    if(getIsWaterMark.value) {
      const waterText = import.meta.env.VITE_GLOB_APP_TITLE;
      Watermark.set(waterText)
    } else {
      Watermark.del()
    }
    // document.addEventListener('mousedown', timekeeping);
  });

  onUnmounted(() => {
    // document.removeEventListener('mousedown', timekeeping);
  });
</script>
