var test = true,
    webNotice = false,
    webNoticeId = "",
    oldOrderInfo = false,
    oldOrderInfo_malai = false;
window.onload = function (ev) {
    init();
}

init = function () {
    getProductInfo("", 1, 10);
	pagenotify();
}


pagenotify = function () {
    notifyMe();
    orderWheelSearch();
    orderWheelSearchMaLai();
}

notifyMe = function () {
    // 先检查浏览器是否支持
    if (!("Notification" in window)) {
        //alert("本浏览器不支持网页通知提示，如需要网页提示，请使用chrome浏览器打开此网站。");
    }
    else {
        Notification.requestPermission(function (permission) {
            if (permission === "granted") {
                webNotice = true;
            }
            else if (Notification.permission === "denied") {
                //alert("请打开网页通知权限，或者质询技术人员协助打开。");
            }
        });
    }
}

orderWheelSearch = function () {
    setInterval(function () {
        var parameter = urlEncode(["OrderWheelSearch"]);
        $.ajax({
            type: "post",
            url: "/new/php/index.php",
            data: {mode: parameter},
            dataType: "json",
            success: function (data) {
                if (oldOrderInfo == false) {
                    oldOrderInfo = data[0];
                }
                else {
                    if (oldOrderInfo.OrderId == data[0].OrderId) {

                    }
                    else {
                        oldOrderInfo = data[0];
                        orderNoticeBody(oldOrderInfo);
                        $("#audio")[0].play();
                    }
                }
                console.log(oldOrderInfo);
            }
        });
    }, 5000);
}

orderWheelSearchMaLai = function () {
    setInterval(function () {
        var parameter = urlEncode(["OrderWheelSearch"]);
        $.ajax({
            type: "post",
            url: "/malai/php/index.php",
            data: {mode: parameter},
            dataType: "json",
            success: function (data) {
                if (oldOrderInfo_malai == false) {
                    oldOrderInfo_malai = data[0];
                }
                else {
                    if (oldOrderInfo_malai.OrderId == data[0].OrderId) {

                    }
                    else {
                        oldOrderInfo_malai = data[0];
                        orderNoticeBodyMaLai(3, oldOrderInfo_malai);
                        $("#audio")[0].play();
                    }
                }
            }
        });
    }, 5000);
}

orderNoticeBody = function (type, data) {
    var title, obj;
    if (type == 1) {
        title = "泰国来订单啦";
    }
    else if (type == 2) {
        title = "台湾来订单啦";
    }
    else if (type == 3) {
        title = "马来西亚来订单啦";
    }
    webNoticeId = data.OrderId + '<>' + data.ProductId;
    obj = {
        dir: "auto",
        body: "订单号：" + data.OrderNum + "\n" +
        "产品编号：" + data.ProductRemarks,
        // tag: webNoticeId,
        icon: JSON.parse(data.ProductInfoClassId)[1].img,
        renotify: false
    }
    var notification = new Notification(title, obj);
    notification.onclick = function (event, data) {
        event.preventDefault(); // prevent the browser from focusing the Notification's tab
        var oid = webNoticeId.split("<>")[0],
            pid = webNoticeId.split("<>")[1];
        window.open('https://kjdspro.com/new/admin/orderinfo.html?oid=' + oid + '&pid=' + pid, '_blank');
    }
}

orderNoticeBodyMaLai = function (type, data) {
    var title, obj;
    if (type == 1) {
        title = "泰国来订单啦";
    }
    else if (type == 2) {
        title = "台湾来订单啦";
    }
    else if (type == 3) {
        title = "马来西亚来订单啦";
    }
    webNoticeId = data.OrderId + '<>' + data.ProductId;
    obj = {
        dir: "auto",
        body: "订单号：" + data.OrderNum + "\n" +
        "产品编号：" + data.ProductRemarks,
        // tag: webNoticeId,
        icon: JSON.parse(data.ProductInfoClassId)[1].img,
        renotify: false
    }
    var notification = new Notification(title, obj);
    notification.onclick = function (event, data) {
        event.preventDefault(); // prevent the browser from focusing the Notification's tab
        var oid = webNoticeId.split("<>")[0],
            pid = webNoticeId.split("<>")[1];
        window.open('https://kjdspro.com/malai/admin/orderinfo.html?oid=' + oid + '&pid=' + pid, '_blank');
    }
}




getProductInfo = function (judge, pageindex, pagesize) {
    var parameter = urlEncode(["SelectAllProduct", judge, pageindex, pagesize]);
    $.ajax({
        type: "post",
        url: "/malai/php/index.php",
        data: {mode: parameter},
        dataType: "json",
        success: function (data) {
            if (data.length == 0 || data == -1) {
                var th = '<th colspan="8" scope="col" class="text-c f-20" style="padding: 50px 0;">暂无数据</th>';
                $("#warp").html(th);
                $("#Page").html("");
                // $("#warp").append(th);
                return;
            }

             if (test) {
            test = false;
            var totalSize = data[0].allcount,
                totalPage = Math.ceil(totalSize / pagesize);
            $("#Page").html("");
            // $("#Page").whjPaging("setPage", {currPage: pageindex, totalPage: totalPage, totalSize: totalSize});
            $("#Page").whjPaging({
                currPage: pageindex,
                totalSize: totalSize,//可选，总条数
                totalPage: totalPage,//可选，总页数
                isShowRefresh: false,//可选，是否展示刷新，默认true
                isShowPageSizeOpt: false,//可选，是否展示每页条数下拉框，默认true，如果设置显示，总条数必须设置
                // isResetPage:true,
                callBack: function (currPage) {
                    $("#loading").css({display: "block"});
                    $("#warp").css({display: "none"});
                    getProductInfo(judge, currPage, pagesize);
                    // test = true;
                    // console.log('currPage:' + currPage + '     pageSize:' + pageSize);
                }
            });
             }

            printProductInfo(data);
            console.log(data);
        }
    });
}

printProductInfo = function (data) {
    var warp = $("#warp"),
        tr,
        href,
        title = "'查看商品信息'";
    warp.html("");
    for (var i = 0; i < data.length; i++) {
        href = "'product-add.html?pid=" + data[i].ProductId + "'";
        tr = $('<tr class="text-c">' +
            '<td>' + data[i].ProductId + '</td>' +
            '<td>' + data[i].ProductRemarks + '</td>' +
            '<td>' + data[i].ProductName + '</td>' +
            '<td>' + data[i].ProductPrimeCost + '</td>' +
            '<td>' + data[i].ProductPrice + '</td>' +
            '<td><a href="' + data[i].ProductIntroduce + '" target="_blank">' + data[i].ProductIntroduce + '</a></td>' +
            '<td>' + data[i].ProductAddTime + '</td>' +
            '<td>' +
            '<a class="btn btn-primary radius td-button" href="/malai/goods.html?pid=' + data[i].ProductId + '">查看H5页面</>' +
            '<a class="btn btn-primary radius td-button" onclick="product_add(' + title + ',' + href + ')">查看产品信息</a>' +
            '</td></tr>');
        warp.append(tr);
    }
    $("#loading").css({"display": "none"});
    $("#warp").css({"display": "table-row-group"});
}

keySearchProductInfo = function () {
    if (event.keyCode == 13) {
        searchProductInfo();
    }
}

searchProductInfo = function () {
    var _val = $("#SearchPlaceBtn").val().trim();
    if (_val == "") {
        layer.msg('请输入订单号、人名、地址、手机号其中一个', {icon: 1});
    }
    else {
        getProductInfo(_val, 1, 10);
    }
}


product_add = function (title, url) {
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}

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

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";
        }
    }
}

