import { reactive } from 'vue'
import request from './helpers/request'
import { getAuth, getUserType, setUserInfo } from './helpers/utils'
import mitt from '@/helpers/mitt'
type status = 'init' | 'login' | 'logout' | 'error'
export const state = reactive({
  user: {
    status: 'init' as status,
    data: {} as any
  },
  openLiveStatus: false,
  authVisibleShow: false, // 授权搜索徐跑完图
  loginPopupStatus: false, // 登录弹窗状态
  loginPopupTimer: null as any // 登录弹窗定时器
})

export const getUserInfo = async () => {
  const token = getAuth()
  const userType = getUserType()
  // 判断是否有token，token和userType 判断是否登录
  if (!token) {
    return
  }

  try {
    const url =
      userType === 'TEACHER'
        ? '/api-website/teacher/queryUserInfo'
        : '/api-website/student/queryUserInfo'
    const res = await request.get(url)
    state.user.data = res.data || {}
    // 存储登录人信息
    setUserInfo(JSON.stringify(state.user.data))
    // 登录状态
    mitt.emit('mittFn')
    state.user.status = 'login'
  } catch {
    state.user.status = 'init'
  }
}
