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

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

            var 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() {
    var text = $(this).text().trim();
    var fade = text.match(/已退款|已结清|已取消/) || [];
    var expire = text.match(/已逾期/) || [];

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

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

    selection.forEach(function(sel) {
        var 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
    });

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

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