<template>
  <ConfigProvider v-if="!isLock" :locale="locale">
    <RouterView />
  </ConfigProvider>
  <transition v-if="isLock && $route.name !== 'login'" name="slide-up">
    <LockScreen />
  </transition>
  <global-websocket :uri="'/api/websocket/' + userInfo.id" @rollback="rollback" />
</template>

<script lang="ts" setup>
  import { computed, onMounted, onUnmounted, defineAsyncComponent, h } from 'vue';
  import { ConfigProvider, notification } from 'ant-design-vue';
  import { LockScreen } from '@/components/Lockscreen';
  import { useLockscreenStore } from '@/store/modules/lockscreen';
  import { useRoute } from 'vue-router';
  import { useUserStore } from '@/store/modules/user';
  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 userStore = useUserStore();
  const userInfo: object = userStore.getUserInfo || {};
  const isLock = computed(() => useLockscreen.isLock);
  const lockTime = computed(() => useLockscreen.lockTime);
  const { getIsWaterMark } = useProjectSetting();
  const GlobalWebsocket = defineAsyncComponent(() => import('@/components/Websocket/index.vue'));
  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);
  };

  /**
   * 通知回调
   */
  const rollback = (msg) => {
    notification.info({
      message: '通知',
      description: () => h('div', msg),
    });
  };

  /**
   * 钩子函数
   */
  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>
