import { getPageListData } from '@/api/main/system/system'
import { addDateRange } from '@/utils/ruoyi'
const useSelectStore = defineStore('common', {
    state: () => ({
        questionnaireData:[],
        // templateListData:[],
        userData:[],
        userListData: [],
        deptListData: [],
        deptUserTree:[],
        orderTypeTree: [],
        knowledgeCatalogTree:[],//工单目录1
        knowledgeLabeTree:[],
        knowledgeTypeTree:[],
        workordertypeListData: [],
        categoryListData: [],
        roleList: [],//角色
        postList: [],//岗位
        dictList: [],//字典
        customerLabelList: [],
        dictCarTypeList:[],//字典-车辆类型
        dictSourceList:[],//字典-来源
        dictProjectList:[],//字典-项目
        dictOrderTypeList:[],//字典-类别
		workorderUserList : [], //工单受理人搜索数据，与转派数据
    }),
    actions: {
        async getSelectData(payload) {
            const questionnairResult = await getPageListData('/survey/survey/getGoingList')    //  /common/user
            this.questionnaireData = getUserSelcet(questionnairResult.data, 'title', 'surveyId')
            // const templateResult = await getPageListData('/sms/template')    //  /common/user
            // this.templateListData = getUserSelcet(templateResult.data, 'templateCode', 'templateId')
            // this.templateNameListData = getUserSelcet(templateResult.data, 'templateName', 'templateId', ['content'])
            const deptResult = await getPageListData('/common/dept') // /common/dept
            this.deptListData = arrayToTree(deptResult.data, 100)   
            const userResult = await getPageListData('/common/user')    //  /common/user
            this.userData = userResult.data
            this.userListData = getUserSelcet(userResult.data, 'nickName', 'userName')
            this.workorderUserList = workorderGlvUserList(userResult.data)

            const deptUserResult = await getPageListData('/common/work/type/treeselect')  //  /common/worker/type/treeselect    
            this.deptUserTree = filterTreeData(deptUserResult.data) ;//filterTreeData(workordertypeResult.data)
            const workordertypeResult = await getPageListData('/common/work/type/treeselect')  //  /common/worker/type/treeselect    
            this.orderTypeTree = workordertypeResult.data;//filterTreeData(workordertypeResult.data)
            const knowledgeCatalogResult = await getPageListData('/common/work/type/treeselect',{category:1})
            this.knowledgeCatalogTree = filterTreeData(knowledgeCatalogResult.data) 
            const knowledgeLabelResult = await getPageListData('/common/work/type/treeselect',{category:2})
            this.knowledgeLabeTree = getUserSelcet(knowledgeLabelResult.data,'label','id') ;
            const knowledgeTypeResult = await getPageListData('/common/work/type/treeselect',{category:3})
            this.knowledgeTypeTree =  getUserSelcet(knowledgeTypeResult.data,'label','id');

            const roleResult = await getPageListData('/common/role')    //  /common/role
            this.roleList = getUserSelcet(roleResult.data,'roleName','roleId')
            const postResult = await getPageListData('/common/post')    // /common/post
            this.postList = getUserSelcet(postResult.data,'postName','postId')
            const dictResult = await getPageListData('/common/dict/type')  // /common/dict/type
            this.dictList = getUserSelcet(dictResult.data,'dictName','dictId')
            const dictSourceResult = await getPageListData('/common/dict/data/work_sorce') // /common/dict/data/{type}
            this.dictSourceList = getUserSelcet(dictSourceResult.data,'dictLabel','dictLabel')

            const dictCarTypeResult = await getPageListData('/common/dict/data/cllx')
            this.dictCarTypeList = getUserSelcet(dictCarTypeResult.data,'dictLabel','dictLabel')

            const dictProjectResult = await getPageListData('/common/dict/data/work_project')
            this.dictProjectList = getUserSelcet(dictProjectResult.data,'dictLabel','dictLabel')
            const customerLabelResult = await getPageListData('/common/dict/data/customer_labels')
            this.customerLabelList = getUserSelcet(customerLabelResult.data,'dictLabel','dataId')
            const dictOrderTypeResult = await getPageListData('/common/work/type/treeselect')
            // const trees = proxy.handleTree(data, "id");
            this.dictOrderTypeList = dictOrderTypeResult.data ?? []
        },
        async getWorkProjectData() {
            const dictProjectResult = await getPageListData('/common/dict/data/work_project')
            this.dictProjectList = getUserSelcet(dictProjectResult.data,'dictLabel','dataId')
        },
        async getSelectCascade(deptId) {
            const params = { deptId }
            const selectResult = await getPageListData('/common/user', params)
            const userSelectData = getUserSelcet(selectResult.data, 'nickName', 'userName')
            return userSelectData
        },
        async getSelectflowableModel() {
            const selectCategoryResult = await getPageListData('/flowable/category')
            this.categoryListData = getUserSelcet(selectCategoryResult.data, 'categoryName', 'categoryName')

        }
    }
})
function arrayToTree(list, root) {
    return list
        .filter(item => item.parentId === root)
        .map(item => ({ label: item.deptName, value: item.deptId, children: arrayToTree(list, item.deptId) }))
}


function filterTreeData(data) {
    let res = []
    data.forEach((item) => {
        res.push({
            label: item.label,
            value: item.id,
            children: (item.children && item.children.length > 0) ? filterTreeData(item.children) : []
        })
    })
    return res
}

function getUserSelcet(data, titleValue, valueValue, other) {
    return data.map((item) => {
        let result = { title: item[titleValue], value: item[valueValue] };

        if (other && other.length > 0) {
            other.forEach((o) => {
                result[`${o}`] = item[`${o}`]
            })
        }

        return result
    }) 
}

function workorderGlvUserList( data ) {
    return  (data || []).map((val)=>{
		return {
			title : `${val.nickName}-${val.userName}`,
			value : val.userName
		}
	})
}

export default useSelectStore
