import { getArrangeWorkDetailApi } from 'mocp/api/assign-work'
import { defineStore } from 'pinia'

const useAssignWorkStore = defineStore('assignWork', {
    state: () => {
        return {
            id: '',
            details: undefined,
            arrangeWorkExtendList: [], //责任单位列表
            arrangeWorkExtend: undefined //当前选中的责任单位
        }
    },
    getters: {
        //获取分解计划
        getDecompose(state) {
            return JSON.parse(state.arrangeWorkExtend?.decompose || '[]') || []
        }
    },
    actions: {
        async getArrangeWorkDetail() {
            const res = await getArrangeWorkDetailApi({ id: this.id }, { loading: true })
            if (res.code == 200) {
                this.details = res.data
                this.arrangeWorkExtendList = res.data.arrangeWorkExtendList || []
                this.arrangeWorkExtend = res.data.arrangeWorkExtendList[0]
            } else {
                uni.$mocpMessage.showToast(res.message)
            }
        },
        setState(...args) {
            this.$patch({ [args[0]]: args[1] })
        }
    },
    // 配置持久化
    persist: {
        // 调整为兼容多端的API
        storage: {
            setItem(key, value) {
                uni.setStorageSync(key, value)
            },
            getItem(key) {
                return uni.getStorageSync(key)
            }
        }
    }
})
export default useAssignWorkStore
