<script setup>
import { useUniApi } from 'hooks/use-uni-api'
import { getUserInfoApi } from '@/api/system'
import { useUserStore } from '@/store/user'
const { navigateTo } = useUniApi()

const { setUserInfo, setUserRoles } = useUserStore()
const checkInfo = async () => {
  const { error, data } = await getUserInfoApi()
  if (error) {
    navigateTo('/pages/views/login/index')
  } else {
    const { user, roles } = data
    if (user) {
      setUserInfo(user)
      setUserRoles(roles)
    }
    if (roles.includes('superadmin') || roles.includes('maintenance')) {
      uni.switchTab({
        url: '/pages/tabbar/message/index'
      })
    } else if (roles.includes('expert')) {
      uni.reLaunch({
        url: '/pages/tabbar/expert/index'
      })
    } else {
      uni.reLaunch({
        url: '/pages/views/no-permission/index'
      })
    }
  }
}

const options = ref({})
onLoad((e) => {
  const { scene } = e
  if (!scene) {
    return checkInfo()
  } else {
    try {
      options.value = e
      const id = scene
      if (id) {
        uni.reLaunch({
          url: `/pages/views/basic-info/index?id=${id}`
        })
      } else {
        checkInfo()
      }
    } catch (error) {
      checkInfo()
    }
  }
})
</script>

<template>
  <view w-full h-100vh>
    <up-loading-page
      :loading="true"
      loading-mode="semicircle"
      loading-text=""
    />
  </view>
</template>
