
import { ReturnMoneyParam } from '@/api/usermoneysum/apiUserMoneySumModel';
import { useEleFormValidate } from '@/hooks/useEleFormValidate';
import { useMessage } from '@/hooks/useMessage';
import { ref, reactive } from 'vue';

const { createConfirm } = useMessage()
/**
 * 客户退款
 * @returns 
 */
function useRdfund(onClose: Fn, onShow: Fn, setLoading: Fn, emit: Fn) {
    const refundRef = ref()
    const refundForm = reactive<ReturnMoneyParam>({
        customerId: 0,
        currencyCode: "CNY",
        feeAmount: undefined,
        remark: ''
    })
    const refundState = reactive({
        customerName: '',
        leftTotal: 0
    })
    const refundFormRules = reactive({
        feeAmount: [{ required: true, message: '请输入金额', trigger: 'blur' }],
    })

    // 显示
    const show = (data) => {
        if (data) {
            refundForm.feeAmount = data.leftTotal > 0 ? data.leftTotal : 0
            refundForm.customerId = data.customerId
            refundForm.currencyCode = data.currencyCode
            refundState.customerName = data.customerName
            refundState.leftTotal = data.leftTotal
        }
        onShow()
    }
    // 隐藏
    const onclose = () => {
        refundForm.feeAmount = 0
        onClose()
    }
    const { formValidate } = useEleFormValidate();
    // 确定
    const confirm = async () => {
        const isValidate = await formValidate(refundRef.value)
        if (!isValidate) return
        createConfirm({
            type: "warning",
            message: "请确定是否要退还金额？",
            cancelButtonText: "取消",
            confirmButtonText: "确定",
            beforeClose: (action, instance, done) => {
                if (action === "confirm") {
                    setLoading(true)
                    emit("onRefund", refundForm, setLoading, onclose)
                }
                done()
            },
        })
    }

    return {
        refundRef,
        refundState,
        refundForm,
        refundFormRules,
        onclose,
        show,
        confirm
    }
}

export {
    useRdfund
}