import { api_addcustomerFeeSetAdministrator, api_customerFeeSetAdministratorList, api_deleteCustomerFeeSetAdministrator, api_editCustomerFeeSetAdministrator } from '@/api/customerfee/customerfeeset';
import { CustomerFeeSetAdministratorDto, CustomerFeeSetAdministratorParam } from '@/api/customerfee/CustomerFeeSetModel';
import type { BasicTableProps } from '@/components/VxeTable';
import { useEleFormValidate } from '@/hooks/useEleFormValidate';
import { useQueryList } from '@/hooks/useQueryList';
import { resCodeSuccess, useReturnData } from '@/utils/useReturnData';
import { ref, reactive, unref, computed, onMounted } from 'vue';
import { useMessage } from '@/hooks/useMessage';
import { useFeesetList } from '@/store/list_feeset';
import { cloneDeep } from '@/utils/cloneDeep';
import { EditType } from '@/type/BaseEnum';

const { createConfirm } = useMessage()
const { resData } = useReturnData()
/**
 * 处理客户费接管
 * @returns 
 */
function useAdministratorList(administratorListOptions: BasicTableProps) {
    // 搜索 ref
    const administratorListFormRef = ref()
    // 新增或编辑 ref
    const addEditAdministratorRef = ref<{ show: Fn }>()
    const administratorListState = reactive({
        loading: false
    })
    // 参数
    const administratorListParams = reactive<CustomerFeeSetAdministratorParam>({
        feeSetName: undefined,
        notifyUserType: undefined,
        pageNum: 1,
        pageSize: 10,
        total: 0
    })
    const storeFeeset = useFeesetList();
    const notifyUserTypeList = computed(() => storeFeeset.getNotifyUserTypeList)
    const sysUserList = computed(() => storeFeeset.getSysUserList)
    onMounted(() => {
        storeFeeset.addCustomerFeeSetNameList();
        storeFeeset.addNotifyUserTypeList();
        storeFeeset.addSysUserList()
    })

    // 初始化数据
    const initList = async () => {
        try {
            administratorListState.loading = true
            const res = await api_customerFeeSetAdministratorList(listParams)
            if (resCodeSuccess(res)) {
                administratorListOptions.data = res.data.records
                administratorListParams.total = res.data.total
            }
        } catch (error) {
        } finally {
            administratorListState.loading = false
        }
    }
    // 校验表单
    const { resetForm } = useEleFormValidate();
    const resetParams = () => {
        resetForm(unref(administratorListFormRef))
    }
    const { sizeChange, currentChange, searchBtn, resetBtn, listParams } = useQueryList<CustomerFeeSetAdministratorParam>(initList, resetParams, administratorListParams)
    // 添加、编辑、删除
    const addBtn = () => unref(addEditAdministratorRef)?.show()
    const getDeleteAdministrator = async (id) => {
        if(!id) return
        const res = await api_deleteCustomerFeeSetAdministrator({ id })
        const deleteCode = resData(res, '删除成功')
        if(deleteCode !== 500) initList()
    }
    const deleteBtn = (id) => {
        createConfirm({
            type: "warning",
            message: "系统提示",
            cancelButtonText: "取消",
            confirmButtonText: "确定",
            beforeClose: (action, instance, done) => {
                if (action === "confirm") {
                    getDeleteAdministrator(id)
                }
                done()
            },
        })
    }
    // 操作
    const handleClickChange = (data) => {
        const row = cloneDeep(data)
        switch (row.types) {
            case EditType.EDIT:
                unref(addEditAdministratorRef)?.show(row)
                break;
            case EditType.DELETE:
                deleteBtn(row.id)
                break;
        }
    }
    // 新增
    const hanleConfirmAdministrator = async (params: Partial<CustomerFeeSetAdministratorDto>, onclose: Fn) => {
        try {
            // 编辑
            if(params.id) {
                const res = await api_editCustomerFeeSetAdministrator(params)
                const codeData = resData(res)
                if (codeData !== 500) initList();
            } else {
                // 新增
                const res = await api_addcustomerFeeSetAdministrator(params)
                const codeData = resData(res)
                if (codeData !== 500) initList();
            }
        } finally {
            onclose()
        }
    }

    return {
        administratorListState,
        administratorListParams,
        administratorListFormRef,
        addEditAdministratorRef,
        sizeChange,
        currentChange,
        searchBtn,
        resetBtn,
        addBtn,
        sysUserList,
        notifyUserTypeList,
        hanleConfirmAdministrator,
        handleClickChange
    }
}

export {
    useAdministratorList
}