
import type { BasicTableProps } from '@/components/VxeTable';
import { api_approvalUserAdd, api_approvalUserDelete, api_approvalUserEdit, api_approvalUserList } from '@/api/approvalUser/approvalUser';
import { ApprovalUserAddParams, ApprovalUserParam } from '@/api/approvalUser/approvalUserModel';
import { resCodeSuccess, useReturnData } from '@/utils/useReturnData';
import { useEleFormValidate } from '@/hooks/useEleFormValidate';
import { useQueryList } from '@/hooks/useQueryList';
import { cloneDeep } from '@/utils/cloneDeep';
import { EditType } from '@/type/BaseEnum';
import { ref, reactive, unref, onMounted, computed } from 'vue';
import { useFeesetList } from '@/store/list_feeset';
import { useMessage } from '@/hooks/useMessage';

/**
 * 处理审批人员记录
 * @returns 
 */
const { resData } = useReturnData()
const { createConfirm } = useMessage()
function useApprovalUser(approvalUserOptions: BasicTableProps) {
    const storeFeesetList = useFeesetList()
    const storeFeeset = useFeesetList();
    const searchFormRef = ref()
    // 添加与编辑弹窗 ref
    const approvalUserAddEditRef = ref<{ show: Fn }>()
    // 查询参数
    const approvalUserParams = reactive<ApprovalUserParam>({
        approvalType: undefined,
        deleteFlag: undefined,
        pageNum: 1,
        pageSize: 10,
        total: 0
    })
    const approvalUserState = reactive({
        loading: false
    })

    // 初始化列表数据
    const initList = async () => {
        approvalUserState.loading = true
        try {
            const res = await api_approvalUserList(listParams)
            if (resCodeSuccess(res)) {
                approvalUserOptions.data = res.data.records
                approvalUserParams.total = res.data.total
            }
        } finally {
            approvalUserState.loading = false
        }
    }
    // 添加、编辑
    const onConfirmSave = async (params: ApprovalUserAddParams, onclose: Fn, setLoading: Fn) => {
        try {
            if(params.id) {
                const res = await api_approvalUserEdit(params)
                const editCode = resData(res)
                if(editCode !== 500) {
                    onclose()
                    initList()
                }
            } else {
                const res = await api_approvalUserAdd(params)
                const addCode = resData(res)
                if(addCode !== 500) {
                    onclose()
                    initList()
                }
            }
        } finally {
            setLoading(false)
        }
    }
    // 删除
    const deleteBtn = async (id) => {
        if(!id) return
        const res = await api_approvalUserDelete({ id})
        const deleteCode = resData(res, '删除成功')
        if(deleteCode !== 500) initList()
    }
    const { resetForm } = useEleFormValidate()
    // 重置
    const resetParams = () => {
        resetForm(searchFormRef.value)
    }
    // 初始化列表、分页数据
    const { sizeChange, currentChange, searchBtn, resetBtn, listParams } = useQueryList<ApprovalUserParam>(initList, resetParams, approvalUserParams)
    // 添加
    const addBtn = () => {
        unref(approvalUserAddEditRef)?.show()
    }
    // 操作
    const handleClickChange = (row) => {
        const data = cloneDeep(row)
        switch (data.types) {
            case EditType.EDIT:
                unref(approvalUserAddEditRef)?.show(data)
                break;
            case EditType.DELETE:
                createConfirm({
                    type: "warning",
                    message: "系统提示",
                    cancelButtonText: "取消",
                    confirmButtonText: "确定",
                    beforeClose: (action, instance, done) => {
                        if (action === "confirm") {
                            deleteBtn(data.id)
                        }
                        done()
                    },
                })
                break;
        }
    }
    onMounted(() => {
        storeFeesetList.addApprovalTypeList()
        storeFeeset.addSysUserList()
    })
    // 审批类型
    const approvalTypeList = computed(() => storeFeesetList.getApprovalTypeList)
    // 审批人员
    const sysUserList = computed(() => storeFeeset.getSysUserList)
    
    return {
        searchFormRef,
        approvalUserState,
        approvalUserParams,
        approvalUserAddEditRef,
        sizeChange, 
        currentChange, 
        searchBtn, 
        resetBtn,
        handleClickChange,
        addBtn,
        approvalTypeList,
        sysUserList,
        onConfirmSave
    }
}

export {
    useApprovalUser
}