/*
 * @Author: micky
 * @Date: 2024-08-10 15:24:06
 * @LastEditors: micky
 * @LastEditTime: 2024-10-27 18:19:43
 * @FilePath: \chargingpile-uniapp\src\stores\user.ts
 */
import { apiUserInfo } from '@/api/user'
import { TOKEN_KEY, USER_INFO, CONFIG, ROLEINDEX } from '@/enums/cacheEnums'
import cache from '@/utils/cache'
import { defineStore } from 'pinia'

interface UserserInfo {
    [key: string]: any
    authenticationStatus: number
    bindMnp: boolean
    headPortrait: string
    physicalExaminationStatus: number
    name: string
    nickname: string
    refuseReason: string
    score: string
    sn: string
    staffId: number
    userId: number
    userName: string
    isOrder: number
    isReadr: number
}
interface UserSate {
    userInfo: UserserInfo
    token: string | null
    temToken: string | null
}
export const useUserStore = defineStore({
    id: 'userStore',
    state: (): UserSate => ({
        userInfo: cache.get(USER_INFO) || {},
        token: cache.get(TOKEN_KEY) || null,
        temToken: null
    }),
    getters: {
        isLogin: state => !!state.token
    },
    actions: {
        async getUser() {
            if (this.token || this.temToken) {
                const data = await apiUserInfo({
                    token: this.token || this.temToken
                })
                this.userInfo = data
                cache.set(USER_INFO, data)
            }
        },
        login(token: string) {
            this.token = token
            cache.set(TOKEN_KEY, token)
        },
        logout() {
            this.token = ''
            this.userInfo = {}
            cache.remove(TOKEN_KEY)
            cache.remove(USER_INFO)
            cache.remove(CONFIG)
            cache.remove(ROLEINDEX)
        }
    }
})
