returns-detail.js 4.04 KB
/**
 * 退换货详情
 * @author: yyqing<yanqing.yang@yoho.cn>
 * @date: 2016/2/26
 */

var $ = require('yoho.jquery'),
    Handlebars = require('yoho.handlebars'),
    dialog = require('../common/dialog');

var $returnState = $('.visual-return-state li'),
    $detail = $('.detail-container'),
    $courierCode = $('.courier-code'),
    $expressCompany = $('#express-company'),
    $expressNumber = $('#express-number'),
    $expressViewBox = $('.express-view-box > span');

var returnId = $('#return-id').val(),
    expId = $('#express-old-id').val(),
    expNumber = $expressNumber.val(),
    expCompany = '',
    expList = {},
    isChange = false;

var Dialog = dialog.Dialog,
    active,
    cancelApplyDialog = '<p class="message-title"><i class="{{messageIcon}}"></i>{{messageTitle}}</p><p class="message-summary">{{messageSummary}}</p>';;

function syncExpressInfo() {
    expCompany = expList[expId] ? expList[expId] : '';
    $expressViewBox.html(expCompany + ' 快递单号:' + expNumber);
    $expressCompany.val(expId);
    $expressNumber.val(expNumber);
}

function validateExpress() {
    var send = {};

    send.number = $expressNumber.val();
    send.id = $expressCompany.val() * 1;

    if (!send.id) {
        alert('请选择快递公司');
    } else if (!send.number) {
        alert('请填入快递单号');
    } else {
        return send;
    }

    return false;
}

function sendExpressCode() {
    var sendParm = validateExpress();

    if (!sendParm) {
        return;
    }

    $.ajax({
        type: 'POST',
        url: '/home/returns/setExpressNumber',
        data: {
            id: returnId,
            isChange: isChange,
            expressId: sendParm.id,
            expressNumber: sendParm.number,
            expressCompany: expList[sendParm.id]
        }
    }).then(function(jsonData) {
        if (jsonData.code === 200) {
            expId = sendParm.id;
            expNumber = sendParm.number;
            $returnState.eq(2).addClass('act');
        } else {
            alert(jsonData.message);
        }
        syncExpressInfo();
        $courierCode.removeClass('in-edit');
    });
}

function cancelApply(id, url) {
    var template = Handlebars.compile(cancelApplyDialog);
    var html = template({messageIcon: 'icon-warn', messageTitle: '取消申请', messageSummary: '您确定要请取消申请吗?'});
    var options = {
        mask: true,
        btns: [
            {
                id: 'cancel-sure',
                name: '确定',
                btnClass: ['cancel-sure'],
                cb: function() {
                    $.ajax({
                        type: 'POST',
                        url: url,
                        data: {
                            id: id
                        }
                    }).then(function(data) {
                        active.close();
                        if (data.code === 200) {
                            location.reload();
                        } else {
                            new dialog.Alert(data.message).show();
                        }
                    });
                }
            },
            {
                id: 'cancel-no',
                name: '取消',
                btnClass: ['cancel-no'],
                cb: function() {
                    active.close();
                }
            }
        ],
        content: html,
        className: 'cancel-dialog'
    };
    return new Dialog(options);
}


if ($detail.parent().hasClass('exchange-detail')) {
    isChange = true;
}

$expressCompany.val(expId);

$detail.find('.company-list').each(function() {
    var data = $(this).data();

    expList[data.id] = data.name;
});

$('#modify').click(function() {
    $courierCode.addClass('in-edit');
});

$('#submit-btn').click(function() {
    sendExpressCode();
});

$('.cancel-return').on('click', '.cancel-trigger', function() {
    active = cancelApply($(this).data('id'), $(this).data('url'));
    active.show();
})