var test = true,
    orderState = "";
window.onload = function (ev) {
    init();
    //日历控件
    laydate.render({
        elem: '#startTime', //指定元素
        type: 'datetime',
        // value: getFormatDate(false),
        max: getFormatDate(true),
        done: function (value, date, endDate) {
            $("#startTime").next().show();
        }
    });
    //日历控件
    laydate.render({
        elem: '#endTime', //指定元素
        type: 'datetime',
        // value: getFormatDate(true),
        max: getFormatDate(true),
        done: function (value, date, endDate) {
            $("#endTime").next().show();
        }
    });
    excel();

}


init = function () {
    var today = getFormatDate(false),
        // var today = '2018-10-10 00:00:00',
        now = getFormatDate(true),
        val = "",
        state = orderState;
    $("#startTime").val(today);
    $("#endTime").val(now);
    $("#startTime").next().show();
    $("#endTime").next().show();
}


//搜索-打印表格
searchExcel = function () {
    $("#loading").css({"display": "block"});
    $("#warp").css({"display": "none"});
    var starttime = $("#startTime")[0].value,
        endtime = $("#endTime")[0].value;
    var parameter = urlEncode(["SelectExcel", starttime, endtime]);
    $.ajax({
        type: "post",
        url: "/malai/php/index.php",
        data: {mode: parameter},
        dataType: "json",
        success: function (data) {
            if (data.length == 0 || data == -1) {
                $("#loading").css({"display": "none"});
                $("#warp").css({"display": "table-row-group"});
                $("#warp").html("");
                var th = '<th colspan="8" scope="col" class="text-c f-20" style="padding: 50px 0;">暂无数据</th>';
                $("#warp").html(th);
                return;
            }
            printExcel(data);
        }
    });
}

printExcel = function (data) {
    var warp = $("#warp"),
        tr,
        state = {1: "已付款,<p style='color:red'>等待发货</p>", 2: "已发货<p style='color:red'>等待收货</p>", 3: "已收货"},
        title = "'订单详情'";
    var ordernum,
        product_remarks,
        order_class, order_class_color, order_class_div,
        product_price,
        order_number,
        order_price,
        order_time,
        order_state,
        order_operation, order_operation_delivergoods, order_operation_buygood, order_operation_href, odrer_address;

    warp.html("");
    for (var i = 0; i < data.length; i++) {
        order_class_div = "";
        ordernum = '<td><label><input type="checkbox" value=' + data[i].OrderNum + ' >' + data[i].OrderNum + '</label></td>';
        product_remarks = '<td>' + data[i].ProductRemarks + '</td>';
        product_price = '<td>' + Math.ceil(data[i].ProductPrice) + '</td>';
        if (data[i].ProductInfoClassId == "") {
            order_class_color = data[i].ProductAttributeId.split(",")[1];
            order_class = '<td><div>' +
                order_class_color;
            order_number = '<td>' + data[i].ProductNumber + '</td>';
            order_price = '</td>' + '<td>' + data[i].Payment + '</td>';
        }
        else {
            data[i].ProductInfoClassId = JSON.parse(unescape(data[i].ProductInfoClassId));
            for (var j in data[i].ProductInfoClassId) {
                if (j == 0) {
                    order_number = '<td>' + data[i].ProductInfoClassId[0].Number + '</td>';
                    order_price = '<td>' + parseInt(data[i].ProductInfoClassId[0].Money) * parseInt(data[i].ProductInfoClassId[0].Number) + '</td>';
                }
                else {
                    order_class_color = data[i].ProductInfoClassId[j].color;
                    order_class_div += '<div>' +
                        order_class_color +
                        '</div>';
                    order_class = '<td>' + order_class_div + '</td>';
                }
            }
        }
        order_time = '<td><p>' + data[i].OrderAddtime.split(" ")[0] + '</p><p>' + data[i].OrderAddtime.split(" ")[1] + '</p></td>';
        order_state = '<td class="state">' + state[data[i].OrderState] + '</td>';
        if (data[i].OrderState == 1) {
            order_operation_delivergoods = '<a class="btn btn-primary radius td-button" onclick="deliverGoods(\'' + data[i].OrderId + '\',this)">发货</a>';
        }
        else if (data[i].OrderState == 2) {
            order_operation_delivergoods = '<a class="btn btn-primary radius td-button" onclick="collectGoods(\'' + data[i].OrderId + '\',this)">确认买家已收货</a>';
        }
        else if (data[i].OrderState == 3) {
            order_operation_delivergoods = "";
        }

        if (data[i].BuyGoods == 0 || !data[i].BuyGoods) {
            order_operation_buygood = '<a class="btn btn-primary radius td-button" onclick="buygood(\'' + data[i].OrderId + '\',this)">采购</a>';
        }
        else {
            order_operation_buygood = '<a class="btn btn-primary radius td-button">已采购</a>';
        }
        order_operation_href = "'orderinfo.html?oid=" + data[i].OrderId + "&pid=" + data[i].ProductId + "'";
        order_operation = '<td>' + order_operation_delivergoods + order_operation_buygood +
            '<a class="btn btn-primary radius td-button" onclick="product_add(' + title + ',' + order_operation_href + ')">查看订单信息</a>' +
            '<a class="btn btn-primary radius td-button" onclick="deleteProduct(\'' + data[i].OrderId + '\',this)">删除</a>' +
            '</td>';
        //        odrer_address = '<td>' + data[i].ProductIntroduce + '</td>';
        odrer_address = '<td><a href="' + data[i].ProductIntroduce + '">' + data[i].ProductIntroduce + '</a></td>';
        tr = $('<tr class="text-c">' + ordernum + product_remarks + order_class + product_price + order_number + order_price + order_time + order_state + '</tr>');
        warp.append(tr);
    }
    $("#loading").css({"display": "none"});
    $("#warp").css({"display": "table-row-group"});
}

excel = function () {
    $("#getValue").click(function () {
        var valArr = new Array;
        $("#warp :checkbox").each(function (i) {
            if (this.checked === true) {
                valArr.push($(this).val());
            }
        });
        var vals = valArr.join(',');
        var valsJ = JSON.stringify(vals);
        window.open("../php/excel.php?" + "vals=" + valsJ);
        // alert(vals);
    });
}


getJsonObjLength = function (jsonObj) {
    var Length = 0;
    for (var item in jsonObj) {
        Length++;
    }
    return Length;
}

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

getFormatDate = function (flag) {
    var nowDate = new Date();
    var year = nowDate.getFullYear();
    var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1;
    var date = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate.getDate();
    var hour = nowDate.getHours() < 10 ? "0" + nowDate.getHours() : nowDate.getHours();
    var minute = nowDate.getMinutes() < 10 ? "0" + nowDate.getMinutes() : nowDate.getMinutes();
    var second = nowDate.getSeconds() < 10 ? "0" + nowDate.getSeconds() : nowDate.getSeconds();
    var val;
    if (flag == true) {
        val = year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
    }
    else if (flag == false) {
        val = year + "-" + month + "-" + date + " 00:00:00";
    }
    else if (flag == -1) {
        val = year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
    }
    return val;
}

hideSelectClose = function (el) {
    $(el).hide();
    $("#" + $(el).prev().attr("id")).val("");
}

toogle = function (cs) {
    var child = $(cs);
    for (var i = 0; i < child.length; i++) {
        if (child[i].style.display == "none") {
            child[i].style.display = "block";
        } else {
            child[i].style.display = "none";
        }
    }
}
