import { defineStore } from 'pinia'
import pinia from '~src/store/init'
import { clearStoreAll, getStore, setStore } from '~src/utils/storage'

export const useAppStore = defineStore('main', {
    state: () => ({
        //用户信息
        userInfo: getStore('userInfo') || {},
        //系统平台
        osType: getStore('osType') || '',
        //菜单信息
        home: getStore('home') || 'index',
        page: getStore('page') || 'anomaly',
        buttons: getStore('buttons') || {},
    }),
    getters: {
        getUserInfo: state => state.userInfo,
        getOsType: state => state.osType,
        getHome: state => state.home,
        getPage: state => state.page,
        getButtons: state => state.buttons,
    },
    actions: {
        //用户信息
        setUserInfo(value) {
            this.userInfo = value
            setStore('userInfo', value)
        },
        //系统平台
        setOsType(value) {
            this.osType = value
            setStore('osType', value)
        },
        //菜单信息
        setButtons(value) {
            this.buttons = value
            setStore('buttons', value)
        },
        setHomePage(value) {
            this.home = value
            setStore('home', value)
        },
        setPage(value) {
            this.page = value
            setStore('page', value)
        },
        getButtonsVal(value) {
            return this.buttons[value] || false
        },
        //清除缓存和token
        clearStoreData() {
            this.userInfo = null
            this.osType = ''
            this.home = 'index'
            this.page = 'anomaly'
            this.buttons = null
            //清除缓存
            clearStoreAll()
        },
    },
})

export default function useUserStoreWidthOut() {
    return useAppStore(pinia)
}
