//#import $owl_mall_order_source:services/modelService.jsx

var OrderAddHelper = (function () {
    var f = {
        getOrderSource: function (source_id) {
            if (source_id.indexOf("owl_mall_order_source") < 0) {
                source_id = "owl_mall_order_source_" + source_id;
            }
            return owl_mall_order_sourceService.get(source_id);
        },
        /**
         * 订单号前缀含义说明：
         * 8-母订单
         * 9-子订单
         * 7-买中台券订单
         * 6-积分兑券订单
         * 5-预存款充值订单
         * 4-AIGC智绘豆充值订单
         * @param orderType
         * @returns {string}
         */
        getOrderCode: function (orderType) {
            var now = new Date();
            var year = "" + now.getFullYear();
            var month = now.getMonth() + 1;
            var date = now.getDate();
            var hour = now.getHours();
            var minute = "" + now.getMinutes();
            var second = "" + now.getSeconds();

            month = month < 10 ? "0" + month : "" + month;
            date = date < 10 ? "0" + date : "" + date;
            hour = hour < 10 ? "0" + hour : "" + hour;
            minute = minute < 10 ? "0" + minute : "" + minute;
            second = second < 10 ? "0" + second : "" + second;

            var num = parseInt(Math.random() * 90000 + 10000) + "";
            var orderCode = year + month + date + hour + minute + second + num;
            if (orderType == "subOrder") {
                orderCode = "9" + orderCode;
            } else if (orderType == "parentOrder") {
                orderCode = "8" + orderCode;
            } else if (orderType == "buyCouponOrder") {
                orderCode = "7" + orderCode;
            } else if (orderType == "enjoyBuyCouponOrder") {
                orderCode = "71" + orderCode;
            } else if (orderType == "pointCouponOrder") {
                orderCode = "6" + orderCode;
            } else if (orderType == "recharge") {
                orderCode = "5" + orderCode;
            } else if (orderType == "aigc_recharge") {
                orderCode = "4" + orderCode;
            } else {
                orderCode = orderCode;
            }

            return orderCode;
        },
        getTotalPrice: function (orderItems) {
            var totalPrice = 0;
            for (var i = 0; i < orderItems.length; i++) {
                var orderItem = orderItems[i];
                totalPrice += orderItem.total_price;
            }
            return totalPrice;
        },

        getTotalDeliveryPrice: function (orderItems) {
            //TODO:计算运费
            return 0;
        },

        getDiscountPrice: function (orderItems) {
            //TODO:计算优惠
            return 0;
        },
        getTotalDealPrice: function (orderItems) {
            return f.getTotalPrice(orderItems) + f.getTotalDeliveryPrice(orderItems) - f.getDiscountPrice(orderItems);
        },

        getTotalPayPrice: function (orderItems) {
            //TODO:计算实际支付金额,应该等于总成交价 - 用券支付 - 用积分支付等等
            return f.getTotalDealPrice(orderItems);
        },


        getSkuIcon: function (sku, product) {
            if (sku && sku.sku_images) {
                var images = sku.sku_images;
                if (images.preview_images) {
                    return images.preview_images
                } else if (images.main_images) {
                    return images.main_images
                }
            } else {
                //如果sku没有图片，那么就用product的图片
                if (product && product.images_info) {
                    var images = product.images_info;
                    if (images.preview_images) {
                        return images.preview_images
                    } else if (images.main_images) {
                        return images.main_images
                    }
                }
            }
            return null;
        }
    }
    return f;
})();

