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


require('../../common/foreach-polyfill');

// 添加.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) {
    var $checked = $('.reason .row .checked');
    var reason = $checked.next('.reason-text').text();
    var reasonId = $checked.parent().data('value');

    $.ajax({
        url: '/me/cancelOrder',
        data: {
            orderCode: code,
            reasonId: reasonId,
            reason: reason
        }
    }).done(function(result) {
        var tip;

        if (result && result.code === 200) {
            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();
        } else {
            new _alert('<h1>' + result.message + '</h1>').show();
        }

    }).fail(function(err) {
        console.log(err);
        new _alert('<h1>出错了!修改失败!</h1>').show();
    });
}

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
};