import { api_UserRecharge } from '@/api/usermoneysum/apiUserMoneySum';
import { RechargeParams } from '@/api/usermoneysum/apiUserMoneySumModel';
import { useEleFormValidate } from '@/hooks/useEleFormValidate';
import { useReturnData } from '@/utils/useReturnData';
import { ref, reactive } from 'vue';
/**
 * 客户充值
 */
function useRechargeAdd(onClose: Fn, onShow: Fn, setLoading: Fn, emit: Fn) {
    const { resData } = useReturnData()
    // 表单
    const addrechargeRef = ref()
    const { formValidate, resetForm } = useEleFormValidate();
    const rechargeForm = reactive<RechargeParams>({
        customerId: undefined,//客户编号	
        currencyCode: '',//币种
        feeAmount: 0,//金额
        customerType: 0,//费用类型	
        orgId: '',//分公司编号	
        bankAccount: undefined,//银行账号	
        transactionNumber: undefined,//交易号	
        receiveTime: undefined,//付款时间
        paymentType: undefined,//付款方式
        remark: undefined//备注
    })
    const rechargeFormRules = reactive({
        customerId: [{ required: true, message: '请选择客户编号', trigger: 'change' }],
        currencyCode: [{ required: true, message: '请选择币种', trigger: 'change' }],
        feeAmount: [{ required: true, message: '请输入金额', trigger: 'blur' }],
        receiveTime: [{ required: true, message: '请选择付款时间', trigger: 'change' }],
        paymentType: [{ required: true, message: '请选择付款方式', trigger: 'change' }],
    })
    // 显示
    const show = () => {
        resetForm(addrechargeRef.value)
        onShow()
    }
    // 隐藏
    const onclose = () => {
        onClose()
    }
    // 确定
    const confirm = async () => {
        const isValidate = await formValidate(addrechargeRef.value)
        if (!isValidate) return
        try {
            setLoading(true)
            const res = await api_UserRecharge(rechargeForm)
            resData(res, '充值成功！')
            emit("onUpdateRecharge")
            onclose()
        } finally {
            setLoading(false)
        }
    }

    return {
        rechargeForm,
        rechargeFormRules,
        addrechargeRef,
        onclose,
        show,
        confirm
    }
}

export {
    useRechargeAdd
}