installment.order-detail.page.js 3.09 KB
let $ = require('yoho-jquery');
let Repayment = require('./repayment');
let CHECKBOX_SELECTOR = 'input.installment-term';
let tip = require('js/plugin/tip');
let bp = require('./burying-point');
let orderCode = ($('.info-box h4').html() || '').replace('订单编号: ', '');
let repayment = new Repayment({
    onGetSelection: function() {
        let ret = [];

        $(CHECKBOX_SELECTOR + ':checked').each(function() {
            let input = $(this);

            let data = {
                index: input.data('sortId'),
                orderCode: input.data('orderCode'),
                termNo: input.data('sortId'),
                amount: parseFloat(input.data('currAmt')),
                fee: parseFloat(input.data('delayFee'))
            };

            ret.push(data);
        });

        return ret;
    },
    onGetSelectableCount: function() {
        return $(CHECKBOX_SELECTOR).length;
    },
    onDeselectAll: function() {
        $(CHECKBOX_SELECTOR + ':checked').prop('checked', false);
        return [];
    },
    onSelectAll: function() {
        $(CHECKBOX_SELECTOR + ':not(:checked)').prop('checked', true);
        return this.getSelection();
    }
});

require('./overdue-notice');

repayment.setTotal(0);
repayment.setFee(0);

/**
 * 订单状态样式
 */
$('.status').each(function() {
    let text = $(this).text().trim();
    let fade = text.match(/已退款|已结清|已取消/) || [];
    let expire = text.match(/已逾期/) || [];

    if (fade.length > 0) {
        $(this).addClass('faded');
    } else if (expire.length > 0) {
        $(this).addClass('expired');
    }
});

$(CHECKBOX_SELECTOR + ':checkbox').click(function() {
    let selection = repayment.getSelection();
    let isSkipped = false;
    let isChecked = $(this).is(':checked');
    let lastIndex = $('input:first').data('sortId');
    let curSel = $(this).data('sortId');
    let self = this;

    selection.forEach(function(sel) {
        let index = sel.index + 1;

        if (index - lastIndex > 1) {
            isSkipped = true;
        } else {
            lastIndex = index;
        }
    });

    // 取消选择时候判断下标,只能先取消大的
    if (selection && selection.length > 0 && !isChecked && curSel < selection[0].index) {
        isSkipped = true;
    }

    if (isSkipped) {
        setTimeout(function() {
            tip.show($(self).is(':checked') ? '请按时间顺序取消选择,不可以跨期' : '请按时间顺序添加还款,不可以跨期');
        }, 0);
        return false;
    }

    repayment.update();
});

// 默认选择第一个
$(CHECKBOX_SELECTOR + ':checkbox:first').click();

$(window).load(function() {

    // 统计:进入订单详情页时
    bp.setContYas('YB_INST_ORDER_INFO', {
        ORDER_CODE: orderCode
    }, true);

    $('.repayment-btn').on('click', function() {

        // 统计:点击立即还款时
        bp.setContYas({
            op: 'YB_INST_ORDER_TOPAY',
            appop: 'YB_H5_INST_ORDER_TOPAY_C'
        }, {
            ORDER_CODE: orderCode,
            PERIODS: $(CHECKBOX_SELECTOR + ':checked').last().data('sortId')
        }, true);
    });
});