<script setup lang="ts">
import { computed, onMounted } from 'vue'
import { useAppStore } from '@/store/modules/app'
import { ConfigGlobal } from '@/components/ConfigGlobal'
import { useDesign } from '@/hooks/web/useDesign'
import { useUserStore } from '@/store/modules/user'
import router from './router'
import { loginApi, getRsaKey, getTestRoleApi, getAdminRoleApi, getRefreshToken } from '@/api/login'

const { getPrefixCls } = useDesign()

const prefixCls = getPrefixCls('app')

const appStore = useAppStore()
const userStore = useUserStore()

const currentSize = computed(() => appStore.getCurrentSize)

const greyMode = computed(() => appStore.getGreyMode)

// Method to handle token check and refresh
const checkTokenAndRefresh = () => {
  const token = userStore.getToken
  if (!token) {
    router.push('/login') // Redirect to login if token is not found
  } else {
    setInterval(function () {
      console.log('ssssssssss1111')
      if (userStore.getToken) {
        getRefreshToken(userStore.getToken)
          .then((res) => {
            userStore.setToken(res.data)
          })
          .catch(() => {
            localStorage.clear()
            window.location.href = '/'
          })
      }
    }, 480000)
  }
}

appStore.initTheme()

onMounted(() => {
  checkTokenAndRefresh() // Check token and start token refresh
})
</script>

<template>
  <ConfigGlobal :size="currentSize">
    <RouterView :class="greyMode ? `${prefixCls}-grey-mode` : ''" />
  </ConfigGlobal>
</template>

<style lang="less">
@prefix-cls: ~'@{namespace}-app';

.size {
  width: 100%;
  height: 100%;
}

html,
body {
  padding: 0 !important;
  margin: 0;
  overflow: hidden;
  .size;

  #app {
    .size;
  }
}

.@{prefix-cls}-grey-mode {
  filter: grayscale(100%);
}
</style>
