import paymentService from "@/query-actions/payments/PaymentConstant";
import PaymentConstant from "@/query-actions/payments/PaymentConstant";

/**
 * 线上批量支付动作
 * batchPayment
 */
export default {
    execute(context, param) {
        console.log('s1 batchPayment')
        if (context.selects.length === 0) {
            context.dom.$Message.info(paymentService.paymentNotSelectMessage);
            return;
        }
        debugger
        try {
            context.selects.forEach(e => paymentService.checkSingletonSelectData(e))
        } catch (e) {
            context.dom.$Message.info(e.message);
            return;
        }
        let warnInfo = PaymentConstant.paymentConfirmMessage
        try {
            PaymentConstant.checkMultiSelectData(context.selects);
        } catch (e) {
            if (e.message === PaymentConstant.PAYMENT_DATA_CHECK_FAIL_COMMON) {
                context.dom.$Message.info(paymentService.PAYMENT_DATA_CHECK_FAIL_COMMON);
                return;
            }
            warnInfo = e.message;
        }
        context.dom.$Modal.confirm({
            title: '信息提示',
            content: warnInfo,
            onOk: () => {
                let param = {
                    "payIds": [],
                    "tableName": PaymentConstant.tableName
                }
                context.selects.forEach(e => {
                    param.payIds.push(e.id)
                })
                paymentService.postPaymentOnlineRequest(param, (data) => {
                    if (data.code === 200 && data.data.extErrorFlag) {
                        setTimeout(() => {
                            context.dom.$Modal.error({
                                title: "错误",
                                content: data.data.errorMessage
                            });
                        }, 400)
                    } else {
                        let allDataLength = param.payIds.length;
                        let failureLength = data.data.failureItems.length;
                        let info = '提交待支付数据完成，本次共提交' + allDataLength + '条记录。\n失败：' + failureLength + '条。\n'
                        if (failureLength > 0) {
                            let msg = ''
                            data.data.failureItems.forEach(e => {
                                msg += e.message + '\n'
                            })
                            info = info + '详细信息：\n' + msg
                        }
                        setTimeout(() => {
                            context.dom.$Modal.info({
                                title: "支付结果",
                                content: info.replace(/\n/g, '<br/>'),
                                onOk: () => {
                                    GMS.vbus.$emit('custom-query-refresh');
                                    GMS.vbus.$emit('tab-count-refresh');
                                }
                            })
                        }, 400)
                    }
                }, (resp) => {
                })
            },
            onCancel() {
            }
        });
    },
}
