cancel-order.js 3.33 KB
var dialog = require('../../plugins/dialog');
var _dialog = dialog.Dialog;

// 添加.check方法
require('../../plugins/check');

// 将一维数组转化成子数组长度为l的二维数组
function getDimensions(arr, l) {
    var a = arr.slice(0, arr.length);
    var newArr = [];
    var subArr = [];

    do {
        subArr = a.splice(0, l);
        newArr.push(subArr);
    } while (subArr.length >= l);

    return newArr;
}

function getTpl(data) {
    var cancelOrderTpl = require('../../../tpl/me/cancel-order.hbs');

    return cancelOrderTpl(data);
}

// 取消订单
function cancelOrder(code, onCancel) {
    $.ajax({
        url: '/me/cancelOrder',
        data: {
            orderCode: code
        }
    }).done(function() {
        var tip = new _dialog({
            className: 'order-dialog alert',
            content: '<h1>订单修改</h1>' +
                     '<span>您已成功取消了该订单</span>',
            closeCb: onCancel,
            btns: [
                {
                    id: 'cancel-complete',
                    btnClass: ['confirm'],
                    name: '确定',
                    cb: function() {
                        tip.close(onCancel);
                    }
                }
            ]
        }).show();

    }).fail(function(err) {
        console.log(err);
    });
}

function showDiaglog(tpl, code, onCancel) {
    var tip = new _dialog({
        className: 'order-dialog cancel-dialog',
        content: tpl,
        keep: true,
        btns: [
            {
                id: 'cancel-confirm',
                btnClass: ['confirm'],
                name: '确定并取消订单',
                cb: function() {
                    cancelOrder(code, onCancel);
                    tip.close();
                }
            },
            {
                id: 'cancel-stop',
                btnClass: ['cancel-btn'],
                name: '暂不取消',
                cb: function() {
                    tip.close();
                }
            }
        ]
    }).show();

    $('.cancel-dialog .reason p').check({
        type: 'radio',
        group: 'reason',
        onChange: function(ele, checked) {
            var $ele = $(ele);
            var $input = $('.cancel-dialog .reason input');

            if (checked && $ele.parent('.other').length) {
                $input.prop('disabled', false);
                $input.focus();
            } else {
                $input.val('');
                $input.prop('disabled', true);
            }
        }
    });
}

// 获取取消订单的理由
function getCancelReason(code, onCancel) {
    $.ajax({
        url: '/me/getCancelOrderReason'
    }).done(function(result) {
        var tpl;
        var data;
        var reasons = [];

        if (result.code) {
            data = getDimensions(result.data, 2);

            data.forEach(function(d, idx) {
                if (idx === 0) {
                    d[0].checked = true;
                }
                reasons.push({
                    subReasons: d
                });
            });

            tpl = getTpl({
                reasons: reasons
            });

            showDiaglog(tpl, code, onCancel);
        }
    }) .fail(function(err) {
        console.log(err);
    });
}

function start(code, onCancel) {
    getCancelReason(code, onCancel);
}

module.exports = {
    start: start
};