installment.order-detail.page.js 2.03 KB
const $ = require('yoho-jquery');
const Repayment = require('./repayment');
const CHECKBOX_SELECTOR = 'input.installment-term';
const tip = require('../plugin/tip');

require('./overdue-notice');


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

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

const repayment = new Repayment({
    onGetSelection: function() {
        const ret = [];

        $(`${CHECKBOX_SELECTOR}:checked`).each(function() {
            const input = $(this);

            const 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();
    }
});

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


$(`${CHECKBOX_SELECTOR}:checkbox`).click(function() {
    const selection = repayment.getSelection();
    let isSkipped = false;
    let lastIndex = 0;

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

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

    repayment.update();
});