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

const arrangeWorkExtendRouter = [
    { name: '工作反馈', path: '/panel/assign-work/edit-work' },
    { name: '领导批示', path: '/panel/assign-work/edit-approval' },
    { name: '跟踪反馈', path: '/panel/assign-work/edit-gzFeedback' },
    { name: '跟踪批示', path: '/panel/assign-work/edit-gzApproval' },
    { name: '后续措施反馈', path: '/panel/assign-work/edit-followUpFeedback' },
    { name: '工作验证', path: '/panel/assign-work/edit-validate' }
]
const useAssignWorkStore = defineStore('assignWork', {
    state: () => {
        return {
            id: '',
            details: undefined,
            arrangeWorkExtendList: [], //责任单位列表
            arrangeWorkExtend: undefined //当前选中的责任单位
        }
    },
    getters: {
        //获取当前选中的责任单位的index
        getArrangeWorkExtendIndex(state) {
            return state.arrangeWorkExtendList.findIndex((item) => item.id == state.arrangeWorkExtend.id)
        },
        //是否创建人
        isCreateUser(state) {
            const userStore = useUserStore()
            return state.details.createUserId == userStore.userInfo.id
        },
        //是否回复人
        isReplyUser(state) {
            const userStore = useUserStore()
            const replyIds = JSON.parse(state.details.reply)[this.getArrangeWorkExtendIndex].map((item) => item.userId)
            return replyIds.includes(userStore.userInfo.id)
        },
        //是否领导
        isPresenterUser(state) {
            const userStore = useUserStore()
            return state.details.presenterId == userStore.userInfo.id
        },
        //获取分解计划
        getDecompose(state) {
            return JSON.parse(state.arrangeWorkExtend?.decompose || '[]') || []
        },
        //获取详情页底部按钮的内容
        getDetailsFooterBtn(state) {
            if (!state.arrangeWorkExtend) return false
            if (state.arrangeWorkExtend.feedbackTime == '0') {
                if (this.isReplyUser) {
                    return arrangeWorkExtendRouter[0]
                }
            } else {
                if (state.arrangeWorkExtend.approvalTime == '0') {
                    if (this.isPresenterUser) {
                        return arrangeWorkExtendRouter[1]
                    }
                } else {
                    if (state.arrangeWorkExtend.gzFeedbackTime == '0') {
                        if (this.isReplyUser && state.arrangeWorkExtend.leaderState != '' && state.arrangeWorkExtend.leaderState != '1') {
                            return arrangeWorkExtendRouter[2]
                        }
                    } else {
                        if (state.arrangeWorkExtend.gzApprovalTime == '0') {
                            if (this.isPresenterUser && state.arrangeWorkExtend.leaderState != '' && state.arrangeWorkExtend.leaderState != '1') {
                                return arrangeWorkExtendRouter[3]
                            }
                        } else {
                            if (state.arrangeWorkExtend.followUpFeedbackTime == '0') {
                                if (this.isReplyUser && state.arrangeWorkExtend.followUpFeedback == '1') {
                                    return arrangeWorkExtendRouter[4]
                                }
                            } else {
                                if (state.arrangeWorkExtend.eventType == '') {
                                    if (this.isCreateUser) {
                                        return arrangeWorkExtendRouter[5]
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return false
        }
    },
    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
