//#import $owlService:services/common/CommonUtil.jsx
//#import $owl_mall_order_package:services/modelService.jsx

var OrderUtil = (function () {
    var f = {
        getOrderOrgIds: function (jOrder) {
            var orgIds = [];
            var items = jOrder.items;
            for (var i = 0; i < items.length; i++) {
                var jItem = items[i];
                if (!CommonUtil.isExist(orgIds, jItem._orgId)) {
                    orgIds.push(jItem._orgId);
                }
            }
            return orgIds;
        },
        getOrderStateValue: function (order) {
            var processState = f.getProcessStateName(order);
            var payState = f.getPayStateName(order);

            return payState + "," + processState;
        },
        getProcessStateName: function (order) {
            var processState = order.process_state;
            if (processState == "submitted") {
                if (order.group_purchase_info && order.group_purchase_info.is_group_purchase == "y") {
                    return "待审核";
                }
                return "提交订单";
            }
            if (processState == "confirm") {
                return "审核通过";
            }
            if (processState == "paid") {
                return "付款成功";
            }
            if (processState == "undelivered") {
                return "未发仓库";
            }
            if (processState == "received") {
                return "仓库已接收";
            }
            if (processState == "outbound") {
                return "已出库";
            }
            if (processState == "signed") {
                return "已签收";
            }
            if (processState == "canceled") {
                if (order.group_purchase_info &&
                    order.group_purchase_info.is_group_purchase == "y" &&
                    order.group_purchase_info.certify_state == "2") {
                    return "审核不通过";
                }
                return "已取消";
            }
            if (processState == "refunded") {
                return "已退款";
            }
            return "";
        },
        getPayStateName: function (order) {
            var payState = order.pay_state;
            if (payState == "unpaid") {
                return "未支付";
            }
            if (payState == "paid") {
                return "已支付";
            }
            return "";
        },
        getRefundStateName: function (order) {
            //refunding_退款中/partrefund
            var refund_state = order.refund_state;
            if (refund_state == "unrefund") {
                return "未退款";
            }
            if (refund_state == "refunding") {
                return "退款中";
            }
            if (refund_state == "partrefund") {
                return "部分退款";
            }
            if (refund_state == "refunded") {
                return "已退款";
            }
            return "";
        },
        getOrderTypeName: function (order) {
            var ext_info = order.ext_info;
            if (!ext_info) {
                return "";
            }
            if (ext_info.order_type == "common") {
                return "普通订单";
            } else if (ext_info.order_type == "pieceBuy") {
                return "拼团订单";
            } else if (ext_info.order_type == "wxJielong") {
                return "微信接龙订单";
            } else if (ext_info.order_type == "presale") {
                return "预售订单";
            } else if (ext_info.order_type == "crossDirectMail") {
                return "跨境直邮";
            } else if (ext_info.order_type == "crossBonded") {
                return "跨境保税";
            }
            return "";
        },
        isCanCancelForUCenter: function (order) {
            var processState = order.process_state;
            if (order.ext_info && order.ext_info.is_givegift == "y" && order.givegift_state == "2") {
                //已领取的送礼订单，不支持取消
                return false;
            }
            if (order.isSubOrder == "y") {
                if (processState == "submitted" || processState == "confirm" || processState == "paid") {
                    return true;
                }
            } else {
                if (processState == "submitted") {
                    return true;
                }
            }
            return false;
        },
        getOrderPackageList: function (orderId) {
            var searchArgs = {
                order_id: orderId
            };
            var searchResult = owl_mall_order_packageService.search(null, searchArgs, null, 0, 100, [{"owl_modifyTime": {order: "desc"}}]);
            if (searchResult.state === 'ok') {
                return searchResult.list;
            }
            return [];
        }
    };
    return f;

})();