import { defineStore } from 'pinia'

export const userStore = defineStore('user', {
    state: () => ({
        user: JSON.parse(localStorage.getItem('userInfo')) || ''
    }),
    getters: {
        getUser: (state) => state.user
    },
    actions: {
        // 获取用户信息
        setUserInfo(user) {
            if (user) {
                this.user = {
                    userName: user.fullName,
                    email: user.email,
                    role: user.role,
                    companyId: user.companyId,
                    area: user.area
                }
                localStorage.setItem('userInfo', JSON.stringify(this.user))
            } else {
                this.user = {
                    userName: '',
                    email: '',
                    role: '',
                    companyId: '',
                    area: ''
                }
            }
        },
        // 清除用户信息
        clearUserInfo() {
            this.user = ''
            localStorage.removeItem('userInfo')
        }
    }
})
