import { create } from 'zustand'
import { getUser } from '@/api/base'

// 用户信息
export interface UserType {
  id: string
  mobile: string
  name: string
  email: string
  username: string
  avatar: string
}

// 角色信息(1学员；5教师; 6管理员)
export interface RoleType {
  id: 1 | 5 | 6
  name: string
}

// 权限信息
export interface PermissionType {
  desc: string
  effect_uris: string
  id: string
  name: string
  parent_id: string
  system_tag: number
  type: number
  tag: string
}

interface State {
  info: UserType | null
  role: RoleType | null
  permissions: PermissionType[]
  setUser: (data: any) => void
  getUser: () => Promise<void>
}

export const useUserStore = create<State>((set) => ({
  info: null,
  role: null,
  permissions: [],
  setUser: (data) => {
    set({ info: data.info, role: data.role, permissions: data.permissions.permissions })
  },
  getUser: async () => {
    const { data } = await getUser()
    set({ info: data.info, role: data.role, permissions: data.permissions.permissions })
  },
}))
