window.onload = function (ev) {
    init();
}


init = function () {
    getOrderInfo();
}

getOrderInfo = function () {
    var oid = getUrlParam("oid");
    var parameter = urlEncode(["OrderInfoSearch", oid]);
    $.ajax({
        type: "post",
        url: "/new/php/index.php",
        data: {mode: parameter},
        dataType: "json",
        success: function (data) {
            printOrderInfo(data);
        }
    });
}

updateOrderInfo=function(){
    var oid = getUrlParam("oid");
    var name=$("#UserReallyName")[0].value;
    var phone=$("#UserPhoneNum")[0].value;
    var email=$("#UserEMali")[0].value;
    var pcode=$("#PostalCode")[0].value;
    var province=$("#Province")[0].value;
    var city=$("#City")[0].value;
    var area=$("#Area")[0].value;
    var address=$("#Address")[0].value;
    var remarks=$("#Remarks")[0].value;
    var close=top.$(".layui-layer-close1")[0];
    var parameter = urlEncode(["UpdateOrderInfo", oid,name,phone,email,pcode,province,city,area,address,remarks]);
    $.ajax({
        type: "post",
        url: "/malai/php/index.php",
        data: {mode: parameter},
        dataType: "json",
        success: function (data) {
            alert('修改成功！');
            close.click();
        }
    });
}

printOrderInfo = function (data) {
    var input = $('input[type="text"]'),
        id,
        value = data[0];
    for (var i = 0; i < input.length; i++) {
        id = input.eq(i).attr("id");
        input.eq(i).attr("value", value[id]);
    }
    $("#Remarks").val(value.Remarks);


    $(".filelist").html("");


    if (value.ProductInfoClassId == "") {
        var color = value.ProductAttributeId.split(",")[1],
            colorimg = value.ProductAttributeId.split(",")[0],
            size = value.ProductAttributeId.split(",")[2];
        $("#Color").val(color);
        $("#Size").val(size);
        $(".filelist").append($('<li><p class="imgWrap"><img src="' + colorimg + '"></p></li>'));
    }
    else {
        $("#Color").parents().eq(1).hide();
        $("#Size").parents().eq(1).hide();
        $(".uploader-list-container").parents().eq(1).children().eq(0).text("订单数据：");
        var obj = JSON.parse(unescape(value.ProductInfoClassId));
        for (var i in obj) {
            if (i == 0) {

                $("#ProductNumber").val(obj[i].Number);
                $("#ProductPrice").val(obj[i].Money);
                $("#Payment").val(parseInt(obj[i].Money) * parseInt(obj[i].Number));
            }
            if (i != 0) {
                var li = '<li>' +
                    '<p>颜色分类：' + obj[i].color + '</p>' +
                    (obj[i].size ? '<p>尺寸：' + obj[i].size + '</p>' : '') +
                    '<p class="imgWrap"><img src="' + obj[i].img + '"></p></li>';
                $(".filelist").append(li);
            }
        }
    }

    $("#loading").css({"display": "none"});
    $(".form").css({"display": "block"});
}


getUrlParam = function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]);
    return null; //返回参数值
}

urlEncode = function (arr) {
    var i; //循环初始化
    for (i = 0; i < arr.length; i++) {
        arr[i] = encodeURIComponent(encodeURIComponent(arr[i]));
    }
    return arr.join();
}