<template>
  <div class="login">
    <n-spin>
      <template #description>
        正在进入系统中...
      </template>
    </n-spin>
  </div>
</template>

<script>
import { useRoute, useRouter } from 'vue-router'
import { useUserStore } from '@/store/modules/user'
import { signinRedirectCallback, signoutRedirectCallback, signoutRedirect, getPath, removePath } from '@/utils/oidc/index.js'
import { updateLoginTime } from '@/api/dashboard/index'

export default {
  name: 'LoginPage',
  setup() {
    const route = useRoute()
    const router = useRouter()
    const userStore = useUserStore()
    if (route.query?.code && route.query?.state) {
      signinRedirectCallback().then(res => {
        updateLoginTime().then(res => {})
        // router.push({ path: getPath() })
        // removePath()
        userStore.getUserInfos()
        const authority = res.profile.authority
        const { VITE_PLATFORM } = import.meta.env
        if (authority && (authority.includes(VITE_PLATFORM) || authority.includes('admin'))) {
          router.push({ path: '/' })
        } else {
          $message.error('暂无权限访问，请联系管理员')
          setTimeout(() => {
            signoutRedirect()
          }, 2000)
        }
      }).catch(err => {
        console.log(err)
        signoutRedirect()
      })
      // } else if (getPath()) {
      //   signoutRedirectCallback().then(res => {
      //     router.push({ path: getPath() })
      //     userStore.setUserInfo()
      //     removePath()
      //   })
    }
  }
}

</script>
<style scoped lang='scss'>
.login{
  width: 100vw;
  height: 100vh;
  position: relative;
  .n-spin-body{
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }
}
</style>
