installment.order-detail.page.js 2.04 KB
var $ = require('yoho-jquery');
var Repayment = require('./repayment');
var CHECKBOX_SELECTOR = 'input.installment-term';
var tip = require('../plugin/tip');
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('amount')),
                fee: parseFloat(input.data('fee')) + parseFloat(input.data('delayFee'))
            };

            ret.push(data);
        });

        return ret;
    },
    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 lastIndex = 0;
    var self = this;

    selection.forEach(function(sel) {
        if (sel.index - lastIndex > 1) {
            isSkipped = true;
        } else {
            lastIndex = sel.index;
        }
    });

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

    repayment.update();
});