var resultData = null;
$("#addPloy").click(function () {
    layer.open({
        type: 2,
        title: '新增策略信息',
        shadeClose: true,
        resize: false,
        shade: 0.3,
        area: ['648px', '500px'],
        maxmin: false,
        content: 'addtflgploy',
        scrollbar: false,
        end: function () {
            close();
        }
    });
});
$('#transformer').click(function () {
    layer.open({
        type: 2,
        title: '变压器详情',
        maxmin: false,
        resize: false,
        shadeClose: true, //点击遮罩关闭层
        area: ['100%', '100%'],
        content: 'transformer',
        scrollbar: false
    });
});
$("#selfcheck").click(function () {
    $.ajax({
        type: 'POST',
        dataType: "json",
        url: '/pqs9900/tflp/selfcheck',
        data: "",
        async: false,
        success: function (data) {
            if (data == 0) {
                layer.msg("变压器不存在！自检失败", {icon: 2, time: 2000});
            } else if (data == 1) {
                layer.msg("变压器策略表与变压器不对应，自检失败！", {icon: 2, time: 2000});
            } else if (data == 2) {
                layer.msg("自检成功，已保存在redis中！", {icon: 1, time: 2000});
            }
        }
    })
})

$(document).ready(function () {
    queryPloy();
});

function queryPloy() {
    jQuery.ajax({
        type: 'POST',
        dataType: "json",
        url: '/pqs9900/tflp/selectAll',
        data: {},
        beforeSend: function () {
            i = ityzl_SHOW_LOAD_LAYER();
        },
        async: false,
        success: function (data) {
            initTable(data);
            resultData = data;
            startQuery();
            //***********关闭loading
            ityzl_CLOSE_LOAD_LAYER(i);
        }
    })
}

function initTable(data) {
    var height = $(window).height() - 155;
    var length = Math.floor(height / 28);
    if (length < 1) {
        length = 1;
    }
    // var heightTemp = screen.height;
    // var length;
    // if (heightTemp === 768) {
    //     length = 10;
    // } else if (heightTemp === 1024) {
    //     length = 20;
    // } else if (heightTemp === 960) {
    //     length = 15;
    // } else if (heightTemp === 1050) {
    //     length = 18;
    // } else {
    //     length = 15;
    // }
    var oTable = $('#example').DataTable({
        scrollX: true,
        lengthChange: false,
        bDestroy: true,
        ordering: false,
        columnDefs: [{
            targets: [],
            visible: false
        }],
        data: data.body,
        destroy: true,
        lengthMenu: [12],
        language: {
            url: '/pqs9900/json/chine.json'
        },
        'fnDrawCallback': function (oTable) {
            $("#example_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 = $("#vdlist").dataTable();
            $('#dataTable-btn').click(function (e) {
                if ($("#changePage").val() && $("#changePage").val() > 0) {
                    var redirectpage = $("#changePage").val() - 1;
                } else {
                    var redirectpage = 0;
                }
                table.fnPageChange(redirectpage);
            });
        }
    });
    new $.fn.dataTable.Buttons(oTable, {
        buttons: [
            {
                extend: 'csv',//导出格式
                title: '变压器策略列表',//导出csv名称
                exportOptions: {
                    columns: [0, 1, 2],//导出需要的列
                    format: {
                        body: function (data) {//对导出内容的筛选 data 表格内容 rid 行索引 cid 列索引
                            return data;
                        }
                    }
                }
            }
        ]
    });
    modifyCss(oTable);
}

function updatetpolyByIndex(index) {
    layer.open({
        type: 2,
        title: '修改策略信息',
        shadeClose: true,
        area: ['648px', '500px'],
        offset: '0px',
        resize: false,
        maxmin: false,
        content: 'updatePloy?rpIndex=' + index,
        scrollbar: false,
        end: function () {
            close();
        }
    });
}

function deletetpolyByIndex(index) {
    layer.confirm('是否确认删除？', function () {
        $.ajax({
            method: "post",
            url: "/pqs9900/tflp/deltePloy",
            data: {
                rpIndex: index
            },
            async: false,
            success: function (data) {
                if (data.code == 200) {
                    layer.msg('删除变压器策略成功!', {icon: 1, time: 1000});
                    var tmp = 0;
                    queryPloy();
                } else {
                    layer.msg('删除变压器策略失败！', {icon: 2, time: 1000});
                }
            }
        })
    })
}

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

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

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

}

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

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