//#import Util.js
//#import DateUtil.js
//#import pigeon.js
//#import $owlService:services/cart/CartService.jsx
//#import $owlService:services/cart/OrderService.jsx
//#import $owl_mall_o:services/modelService.jsx

var OrderUtil = (function () {
    var f = {
        getAliasCode: function (jOrder) {
            var createTime = jOrder._create_time;
            if(typeof createTime === 'string'){
                createTime = new Date(createTime).getTime();
            }
            var aliasCode = DateUtil.getStringDate(createTime, "yyyyMMddHHmmss");
            var number = OrderService.getOrderAliasCodeNumber() + "";
            number = number.substring(number.length - 3, number.length);
            var ran = f.getRanString(10);
            ran = ran.substring(5, 7);
            return aliasCode + ran + number;
        },
        getPayRecordCode: function () {
            var number = OrderService.getPayRecordCodeNumber() + "";
            var numberString = "" + number;
            var outerId = "880" + numberString;
            var now = "" + new Date().getTime();
            var paddingNumber = 16 - outerId.length;
            return outerId + now.substring(now.length - paddingNumber);
        },
        getRanString: function (len) {
            var str = "";
            var arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
            for (var i = 0; i < len; i++) {
                var pos = Math.round(Math.random() * (arr.length - 1));
                str += arr[pos];
            }
            return str;
        },
        getItems: function (smallCart) {
            var items = [];
            for (var i = 0; i < smallCart.items.length; i++) {
                var jItem = smallCart.items[i];
                if (!jItem.checked) {
                    continue;
                }

                var smallItem = {};
                smallItem.shop_id = jItem.shopId;
                smallItem.product_id = jItem.productId;
                smallItem.sku_id = jItem.skuId;
                smallItem.sku_code = jItem.skuCode;
                smallItem.barcode = jItem.barcode;
                smallItem.attrs = jItem.attrs;
                smallItem.name = jItem.name;
                smallItem.amount = jItem.amount;
                smallItem.ori_price = jItem.unitPrice;
                smallItem.unit_price = jItem.unitPrice;
                smallItem.total_price = jItem.totalPrice;
                smallItem.unit_name = jItem.unitName;

                items.push(smallItem);
            }
            return items;
        },
        getProcessState: function (order) {
            if (!order || !order.process_state) {
                return "";
            }
            var state = order.process_state;
            var process_state = ["待审核", "已确认", "已出库", "已签收", "已取消"];
            return process_state[state];
        },
        getPayState: function (order) {
            if (!order || !order.pay_state) {
                return "";
            }
            var state = order.pay_state;
            var pay_state = ["未支付", "已支付"];
            return pay_state[state];
        },
        getDeliveryTime: function (order) {
            if (!order || !order.delivery_info || !order.delivery_info.delivery_time) {
                return "";
            }
            var deliveryTime = order.delivery_info.delivery_time;
            if (deliveryTime == "everyday") {
                return "工作日、周末及假日均可送货";
            } else if (deliveryTime == "m2f") {
                return "仅工作日送货";
            } else if (deliveryTime == "s2s") {
                return "仅周末或假日送货";
            } else {
                return "未知";
            }
        },
        getOrderStateName: function (jOrder) {
            // $.log("getOrderStateName");
            var payState = jOrder.pay_state;
            var processState = jOrder.process_state;
            if (processState == "4") {
                return "已取消";
            }
            if (payState == "0") {
                return "待支付";
            }
            if (processState == "3") {
                return "已签收";
            }
            if (processState == "2") {
                return "已出库";
            }
            if (processState == "1") {
                return "准备发货";
            }
            if (processState == "0") {
                return "订单到仓库";
            }
            return "待支付";
        }
    };
    return f;
})();