repayment.js 4.35 KB
let Repayment = function(options) {
    let self = this;

    this.settings = $.extend({}, {

        onSelectAll: $.noop,

        onDeselectAll: $.noop,

        /**
         * 返回可选择数目
         *
         * @returns {number}
         */
        onGetSelectableCount: function() {
            return 0;
        },

        /**
         * 获取选择
         *
         * @returns {Array}
         */
        onGetSelection: function() {
            return [];
        }
    }, options);

    this.currAmt = $('.repayment-bottom').find('.curr-amt');
    this.currFee = $('.repayment-bottom').find('.curr-fee');
    this.total = 0;
    this.fee = 0;


    $('.repayment-btn').on('click', function() {
        let path = location.pathname;

        let params = {
            action: 'go.instalmentRepayment',
            params: {
                list: self.setParams(),
                amount: self.total
            }
        };

        $(this).attr('href', path + '?openby:yohobuy=' + encodeURIComponent(JSON.stringify(params)));
    });

    $('#repayment-total').click(function() {
        let isChecked = $(this).data('checked');

        if (!isChecked) {
            self.settings.onSelectAll.bind(self)();
        } else {
            self.settings.onDeselectAll.bind(self)();
        }

        self.update();
        $(this).data('checked', $(this).data('checked'));
    });
};

/**
 * 格式化金额
 *
 * @param amount
 * @returns {string}
 * @private
 */
Repayment.prototype._formatCurrency = function(amount) {
    let m = ('' + amount).match(/(\d+)\.?(\d+)?/);
    let fixedPart = m[1], floatPart = m[2];

    let ret = '0.00';

    if (m && m.length === 3) {
        ret = fixedPart + '.' + (floatPart || '00');
    }

    return ret;
};

/**
 * 设置总金额
 *
 * @param total
 */
Repayment.prototype.setTotal = function(total) {
    this.total = total;

    this.currAmt.text(this._formatCurrency(this.total));
};


/**
 * 设置费用
 *
 * @param fee
 */
Repayment.prototype.setFee = function(fee) {
    this.fee = fee;

    this.currFee.text(this._formatCurrency(this.fee));
};

// 获取立即付款的传参
Repayment.prototype.setParams = function() {
    let values = this.getSelection();
    let formatValues = [];

    if (values && values.length) {
        values.forEach(function(value) {
            formatValues.push({
                index: value.index,
                orderCode: value.orderCode,
                termNo: value.termNo
            });
        });
    }

    return formatValues;
};

Repayment.prototype.getSelection = function() {
    return this.settings.onGetSelection();
};

Repayment.prototype._updateUI = function() {
    this.setTotal(this.total);
    this.setFee(this.fee);

    if (this.fee > 0) {
        $('.serve-price').show();
    } else {
        $('.serve-price').hide();
    }
};

/**
 * 更新组件并计算金额
 */
Repayment.prototype.update = function() {
    let values = this.getSelection();
    let $repaymentBottom = $('.repayment-bottom');
    let self = this;
    let total = [], fee = [];

    // 是否选择全部
    let totalCheck = this.settings.onGetSelectableCount() === values.length;

    this.total = 0;
    this.fee = 0;

    if (values && values.length) {

        values.forEach(function(value) {
            // 计算还款总额和费用
            // self.total += value.amount + value.fee;
            // self.fee += value.fee;

            total.push(value.amount);
            fee.push(value.fee);
        });

        $.get('/home/installment/total-amount.json?prices=' + total.join(',')).then(function(result) {
            // 计算总金额
            if (result.code === 200) {
                self.total = result.data;
            }
        }).then(function() {
            return $.get('/home/installment/total-amount.json?prices=' + fee.join(','));
        }).then(function(result) {
            // 计算手续费
            if (result.code === 200) {
                self.fee = result.data;
                self._updateUI();
            }
        });
    }

    if (values.length === 0) {
        $repaymentBottom.slideUp();
    } else {
        $repaymentBottom.slideDown();
    }

    $('#repayment-total').prop('checked', totalCheck).data('checked', totalCheck);
};

// 跳转到还款详情
window.jumpDetail = function(id) {
    location.href = '/home/installment/repay/detail?id=' + id;
};

module.exports = Repayment;