import { defineStore } from 'pinia'
import { DICT_DATA } from '@/stores/constant/cacheKey'
import type { DictData } from '@/stores/interface/index'
import { reactive } from 'vue'

export const useDictData = defineStore(
    'dictData',
    () => {
        const state: DictData = reactive({
            basic: [],
            area: [],
            areaTree: [],
            userList: []
            // 其他接口获取的字典，比如区域
        })
        const getBasicData = (code: string, arr?: string[]) => {
            let list = []
            list = state.basic.filter(item => item.code === code)[0]?.children || []
            if (arr) {
                list = list.filter(item => !arr.includes(item.code))
            }
            return list
        }
        const areaSelect = () => {
            let list = state.areaTree.filter(item => item.id == state.area[0]?.area)

            return list.length == 0 ? state.areaTree : list
        }

        const statusSelect = () => {
            return [
                { name: '待提交审批', id: 0 },
                { name: '审批中', id: 1 },
                { name: '审批通过', id: 2 },
                { name: '审批不通过', id: 3 },
                { name: '已取消', id: 4 }
            ]
        }

        return {
            state,
            getBasicData,
            areaSelect,
            statusSelect
        }
    },
    {
        persist: {
            key: DICT_DATA
        }
    }
)
