/**
 * 终端更改日志
 */
var dataTable = null;
var resultData = null;
var resultStatus = "";
var endTime = getEndTime();
var startTime = getStartTime("周", endTime);
$(document).ready(function () {
    $("#interval option[value='年份']").remove();
    $("#interval option[value='季度']").remove();
    load();
});
$("#query").click(function () {
    startTime = $("#startTime").val();
    endTime = $("#endTime").val();
    load();
});

function load() {
    resultStatus = $("#resultStatus option:selected").val();
    var i;
    $.ajax({
        type: "post",
        url: "/pqs9900/device/getEventPushLogsDetail",
        data: {
            resultStatus: resultStatus,
            startTime: startTime,
            endTime: endTime
        },
        beforeSend: function () {
            i = ityzl_SHOW_LOAD_LAYER();
        },
        error: function (data) {
            layer.msg("暂无数据", {
                icon: 5,
                time: 3000
            });
            //***********关闭loading
            ityzl_CLOSE_LOAD_LAYER(i);
        },
        success: function (data) {
            if (data.code == 500) {
                resultData = null;
                //没有数据
                initTable(null);
            } else {
                var info = data.body;
                resultData = info;
                initTable(info);
            }
            startQuery();
            //***********关闭loading
            ityzl_CLOSE_LOAD_LAYER(i);
        }
    });
}

function initTable(data) {
    var height = $(window).height() - 160;
    var length = Math.floor(height / 28);
    if (length < 1) {
        length = 1;
    }
    dataTable = $("#eventPushLogid").DataTable();
    dataTable.clear().draw();
    dataTable = $('#eventPushLogid').DataTable({
        // order: [[10, 'asc']],
        columnDefs: [
            {
                orderable: false,
                targets: [2, 3, 5]
            }
        ],
        columns: [
            {width: "5%", data: null},
            {
                width: "10%", data: "timtId",
                render: function (data, type, full, meta) {
                    return format(full.timeId);
                }
            },
            {
                width: "8%", data: "gdName",
                render: function (data, type, full, meta) {
                    return full.gdName;
                }
            },
            {
                width: "8%", data: "subName",
                render: function (data, type, full, meta) {
                    return full.subName;
                }
            },
            {
                width: "8%", data: "lineName",
                render: function (data, type, full, meta) {
                    return full.lineName;
                }
            },
            {
                width: "6%", data: "eventValue",
                render: function (data, type, full, meta) {
                    return full.eventValue;
                }
            },
            {
                width: "10%", data: "persistTime",
                render: function (data, type, full, meta) {
                    return full.persistTime;
                }
            },
            {
                width: "10%", data: "creatTime",
                render: function (data, type, full, meta) {
                    return format(full.creatTime);
                }
            },
            {
                width: "10%", data: "",
                render: function (data, type, full, meta) {
                    if (full.result == 1) {
                        return format(full.updateTime);
                    } else {
                        return "/";
                    }

                }
            },
            {
                width: "8%", data: "",
                render: function (data, type, full, meta) {
                    if (full.result == 1) {
                        return "/";
                    } else {
                        return full.pushfailed;
                    }

                }
            },
            {
                width: "5%", data: "",
                render: function (data, type, full, meta) {
                    if (full.result == 1) {
                        return "成功";
                    } else {
                        return "失败";
                    }

                }
            },
            {
                width: "10%", data: "",
                render: function (data, type, full, meta) {
                    if (full.userUpdateTime == null) {
                        return "/";
                    } else {
                        return format(full.userUpdateTime);
                    }
                }
            },
            {
                width: "10%", data: "",
                render: function (data, type, full, meta) {
                    if (full.result == 1) {
                        return "<button type='button' class='btn themeBGDisableColor' style='margin-right:10px;' href='javascript:void(0);'  disabled>已推送</button>";
                    } else {
                        return "<button type='button' class='btn themeBGColor'" +
                            " style='margin-right:10px;' href='javascript:void(0);' onclick='sendEventMsg(\"" + full.lineId + "\",\"" + full.eventDetaileIndex + "\")' >人工推送</button>";
                    }
                }
            },
        ],
        autoWidth: false,
        serverSide: false,
        destroy: true,
        data: data,
        lengthChange: false,
        scrollX: true,
        searching: true,
        dom: 'rtip',
        pageLength: length,
        language: {
            url: '/pqs9900/json/chine.json'
        },
        "createdRow": function (row, data, index) {
            if (data.subName.length > 10) {
                var realValue = data.subName;
                var s = data.subName.substr(0, 10) + '...'
                $('td', row).eq(3).prevObject[3].innerHTML = "<span title='" + realValue + "'>" + s + "</span>";
            }
            if (data.lineName.length > 10) {
                var lineValue = data.lineName;
                var lineS = data.lineName.substr(0, 10) + '...'
                $('td', row).eq(4).prevObject[4].innerHTML = "<span title='" + lineValue + "'>" + lineS + "</span>";
            }
        },
        fnDrawCallback: function (oTable) {
            $("#eventPushLogid_info").addClass("fl");
            $("#eventPushLogid_paginate").addClass("fr");
            $("#eventPushLogid_paginate").append("<div style='float:right;padding-top:4px;padding-left:10px'>到第 <input type='text' id='changePage' class='input-text' style='width:50px;height:28px;'> 页  <button class='btn btn-primary' id='dataTable-btn'>确定</button></div>");
            var table = $("#eventPushLogid").dataTable();
            $('#dataTable-btn').click(function (e) {
                if ($("#changePage").val() && $("#changePage").val() > 0) {
                    var redirectpage = $("#changePage").val() - 1;
                } else {
                    var redirectpage = 0;
                }
                table.fnPageChange(redirectpage);
            });
        }
    });
    dataTable.on('order.dt search.dt',
        function () {
            dataTable.column(0, {
                search: 'applied',
                order: 'applied'
            }).nodes().each(function (cell, i) {
                cell.innerHTML = i + 1;
            });
        }).draw();
    new $.fn.dataTable.Buttons(dataTable, {
        buttons: [
            {
                extend: 'csv',//导出格式
                title: '暂降推送日志列表',//导出csv名称
                exportOptions: {
                    columns: [1, 2, 3, 4],//导出需要的列
                    format: {
                        body: function (data) {//对导出内容的筛选 data 表格内容 rid 行索引 cid 列索引
                            return data;
                        }
                    }
                }
            }
        ]
    });
    modifyCss(dataTable);

}

function modifyCss(dataTable) {
    dataTable.buttons().container().css("display", "inline-block");
    dataTable.buttons().container().css("float", "right");
    dataTable.buttons().container().css("top", "4px");
    dataTable.buttons().container().find("a").prepend("<i class='glyphicon glyphicon-share'></i>");
    dataTable.buttons().container().appendTo('.header');
    dataTable.buttons().container().after($("#fliteData")[0]);
}

//搜索表中的数据
function searchResult(value) {
    dataTable.search(value).draw();
}

//特殊处理，表头紧缩问题--start
var it = null;

function startQuery() {
    //实时刷新时间单位为毫秒
    it = setInterval('refreshQuery()', 100);

}

/* 刷新查询 */
function refreshQuery() {
    var width = $("#eventPushLogid").width();
    if (width > 100) {
        initTable(resultData);
        window.clearInterval(it)
    }
}

//特殊处理，表头紧缩问题--end

function add0(m) {
    return m < 10 ? '0' + m : m
}

function format(shijianchuo) {
    var time = new Date(shijianchuo);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
}

//人工推送消息
function sendEventMsg(lineId, eventDetailIndex) {
    $.ajax({
        type: "post",
        url: "/pqs9900/device/sendUserEventMsg",
        data: {
            lineId: lineId,
            eventDetailIndex: eventDetailIndex
        },
        beforeSend: function () {
            i = ityzl_SHOW_LOAD_LAYER();
        },
        success: function (data) {
            if (data.resultCode == 10000) {
                layer.msg(data.msg, {
                    icon: 6,
                    time: 1000
                }, function () {
                    load();
                });
            } else {
                layer.msg(data.msg, {
                    icon: 5,
                    time: 1000
                }, function () {
                    load();
                });
            }
        }
    })
}