order-block.js 3.29 KB
/**
 * 订单
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2016/2/17
 */

var $ = require('yoho.jquery');

var dialog = require('../common/dialog');

var Dialog = dialog.Dialog;
var Confirm = dialog.Confirm;

var $tpl = $('#cancel-dialog-tpl');

var html = $tpl.html();

var active;

function cancelFactory(id) {
    var options = {
        mask: false,
        btns: [
            {
                id: 'cancel-sure',
                name: '确定并取消订单',
                btnClass: ['cancel-sure'],
                cb: function() {
                    var $checked = $('.cancel-dialog :checked');
                    var $tip = $('.cancel-dialog .cancel-tip');
                    var text = '';
                    if ($checked.val() === '10') {
                        text = $checked.parent().find('#reason_other').val();
                    }else{
                        text = $checked.siblings('label').text();
                    }
                    if ($checked.length === 0) {
                        $tip.html('请选择要取消订单的原因');
                        return;
                    }
                    $.ajax({
                        type: 'POST',
                        url: '/home/orders/cancelorder',
                        data: {
                            orderCode: id,
                            reasonId: $checked.val(),
                            reason: text
                        }
                    }).then(function(data) {
                        if (data.code === 200) {
                            active.close();
                            history.go(0);
                        } else {
                            $tip.html(data.message);
                        }
                    });
                }
            },
            {
                id: 'cancel-no',
                name: '暂不取消',
                btnClass: ['cancel-no'],
                cb: function() {
                    active.close();
                }
            }
        ],
        content: html,
        className: 'cancel-dialog'
    };

    return new Dialog(options);
}

$tpl.remove();

//查看物流
$('.check-logistics').click(function() {
    $(this).siblings('.logistics').removeClass('hide');
});

$('.close-logistics').click(function() {
    $(this).closest('.logistics').addClass('hide');
});


//订单列表&订单详情【取消订单和确认收货】
$('.me-orders, .order-detail').on('click', '.cancel-order', function(e) {

    //取消订单
    active = cancelFactory($(this).closest('.order, .order-detail').data('id'));
    active.show();
}).on('click', '.confirm-received', function(e) {
    var id = $(this).closest('.order, .order-detail').data('id');

    //确认收货
    active = new Confirm({
        cb: function() {
            $.ajax({
                type: 'POST',
                url: '/home/orders/confirmorder',
                data: {
                    orderCode: id
                }
            }).then(function(data) {
                if (data.code === 200) {
                    active.close();
                    history.go(0);
                }
            });
        },
        content: '您确定要确认收货吗?'
    });
    active.show();
});