import {defineStore} from 'pinia'
import pinia from "../store/init"
import config from '../config/index';
import {getStorage, setStorage, clearStorage} from "@/utils/storage";

export const useAppStore = defineStore('main', {
    state: () => ({
        //系统信息
        title: config.theme,
        //用户信息
        tenantId: config.tenantId,
        userInfo: getStorage('userInfo') ?? {},
        //项目合同段数据
        projectInfo: getStorage('projectInfo') ?? {},
        contractInfo: getStorage('contractInfo') ?? {},
        projectId: getStorage('projectId') ?? '',
        contractId: getStorage('contractId') ?? '',
        isAnimation: getStorage('isAnimation') ?? false,
        //消息数据
        msgCountData: getStorage('msgCountData') ?? {}
    }),
    actions: {
        //系统信息
        setTitle(value) {
            this.title = value
        },
        //用户信息
        setTenantId(value) {
            this.tenantId = value
            setStorage('tenantId', value)
        },
        setUserInfo(value) {
            this.userInfo = value
            setStorage('userInfo', value)
        },
        //项目合同段数据
        setProjectInfo(value) {
            this.projectInfo = value
            setStorage('projectInfo', value)
        },
        setContractInfo(value) {
            this.contractInfo = value
            setStorage('contractInfo', value)
        },
        setProjectId(value) {
            this.projectId = value
            setStorage('projectId', value)
        },
        setContractId(value) {
            this.contractId = value
            setStorage('contractId', value)
        },
        setAnimation(value) {
            this.isAnimation = value
            setStorage('isAnimation', value)
        },
        setMsgCountData(value) {
            this.msgCountData = value
            setStorage('msgCountData', value)
        },
        //清除缓存和token
        clearStoreData() {
            //清除状态
            this.title = config.theme
            //用户信息
            this.tenantId = config.tenantId
            this.userInfo = {}
            //项目合同段数据
            this.projectInfo = {}
            this.contractInfo = {}
            this.projectId = ''
            this.contractId = ''
            this.msgCountData = {}
            clearStorage()
        },
    }
})

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