/**
 * 变压器台账
 */
var resultData = null;
//修改变压器台账信息
function updateTransByIndex(index) {
    $("#tfIndex").val(index);
    var subIndex = $("#subIndex").val();
    layer.open({
        type: 2,
        title: '修改变压器信息',
        shadeClose: true,
        resize: false,
        shade: 0.3,
        area: ['400px', '350px'],
        content: 'updateTrans', //iframe的url
        scrollbar: false,
        end: function () {
            close();
        }
    });
}

//删除变压器台账信息
function deleteTransByIndex(index) {
    var tName = parent.tfName;
    var node = parent.nodIndex;
    var userIndex = index;
    layer.confirm('确定删除？', function (index) {
        $.ajax({
            type: "POST",
            url: "/pqs9900/trans/deleteTransByIndex",
            dataType: "text",
            data: {
                index: userIndex
            },
            success: function (data) {
                var info = $.parseJSON(data);
                if (info.code == 500) {
                    //删除用户异常、或者失败
                    layer.msg(info.message, {
                        icon: 5,
                        time: 1000
                    }, function () {
                        load();
                        getTree();
                    });
                } else {
                    //删除用户成功
                    layer.msg(info.message, {
                        icon: 6,
                        time: 1000
                    }, function () {
                        if (tName == undefined || node == undefined) {
                            load();
                            getTree();
                        } else {
                            parent.queryInfo(tName, node);
                        }
                    });
                }
            }

        });
        layer.close(index);
    })
}

//节点维护
function tfLgAssTransByIndex(index) {

    $("#tfIndex").val(index);
    layer.open({
        type: 2,
        title: '变压器节点维护',
        shadeClose: true,
        resize: false,
        shade: 0.3,
        area: ['500px', '200px'],
        content: 'tfLgAss', //iframe的url
        scrollbar: false,
        end: function () {
            close();
        }
    });
}

function load() {
    $.ajax({
        type: "POST",
        url: "/pqs9900/trans/selectAll",
        data: "",
        beforeSend: function () {
            i = ityzl_SHOW_LOAD_LAYER();
        },
        success: function (data) {
            //没有权限的请求
            if (data == 300) {
                location.href = '/pqs9900/auth/unauthorized';
                //没有登陆的请求
            } else if (data == 301) {
                location.href = '/pqs9900';
            } else {
                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 = $('#vdlist').DataTable({
        dom: 'rtip',
        scrollX: true,
        lengthChange: false,
        bDestroy: true,
        ordering: false,
        columnDefs: [{
            targets: [],
            visible: false
        }],
        data: data.body,
        destroy: true,
        lengthMenu: length,
        language: {
            url: '/pqs9900/json/chine.json',
            "sEmptyTable": "没有内容"
        },
        "createdRow": function (row, data, index) {
            if (data[0].length > 6) {
                var realValue = data[0];
                var s = data[0].substr(0, 6) + '...'
                $('td', row).eq(0).prevObject[2].innerHTML = "<span title='" + realValue + "'>" + s + "</span>";
            }
            if (data[1].length > 6) {
                var realValue = data[1];
                var s = data[1].substr(0, 6) + '...'
                $('td', row).eq(1).prevObject[1].innerHTML = "<span title='" + realValue + "'>" + s + "</span>";
            }
        },
        'fnDrawCallback': function (oTable) {
            $("#vdlist_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,3,4,5],//导出需要的列
                    format: {
                        body: function (data) {//对导出内容的筛选 data 表格内容 rid 行索引 cid 列索引
                            return data;
                        }
                    }
                }
            }
        ]
    });
    modifyCss(oTable);
}

$(document).ready(function () {
    //新增变压器台账
    $("#addTrans").click(function () {
        var subIndex = $("#subIndex").val();
        layer.open({
            type: 2,
            title: '新增变压器信息',
            shadeClose: true,
            resize: false,
            shade: 0.3,
            area: ['400px', '350px'],
            content: 'addTrans', //iframe的url
            scrollbar: false,
            end: function () {
                close();
            }
        });
    });
    load();
});

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 = $("#vdlist").width();
    if (width > 100) {
        initTable(resultData);
        window.clearInterval(it)
    }
}

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