var resultData =null;
$(function () {
    getData();
});
function getData() {
    //获取模板所有信息
    $.post('/pqs9900/report/getPloyData', function (data) {
        var tableData = [];
        for (var i = 0; i < data.body.length; i++) {
            var temp=[];
            temp.push(data.body[i].rpName);
            temp.push(data.body[i].rpDescribe);
            temp.push(data.body[i].rpIndex);
            temp.push("");
            tableData.push(temp);
        }
        initTable(tableData);
        resultData = tableData;
        startQuery();
    });
}

//点击编辑按钮
$('#excelList').on("click", ".updateData", function () {
    if (objTable != null) {
        var item = objTable.row($(this).closest('tr')).data();
        showPloy(item[0], item[1], item[2]);
    }
});

//点击删除按钮
$('#excelList').on("click", ".btn-danger", function () {
    if (objTable != null) {
        var item = objTable.row($(this).closest('tr')).data();

        $.post('/pqs9900/report/deleteexcelploy', {
            'rpIndex': item[2]
        }, function (data) {
            layer.msg(data.body);
            close();
            getData();//删除成功更新表格数据
        });
    }
});

var objTable = null;

function initTable(data) {
    objTable = $('#excelList').DataTable({
        data: data,
        lengthChange: false,
        bDestroy: true,
        ordering: false,
        columnDefs: [{
            targets: [2],
            visible:
                false
        }],
        dom: 'lrt<<"myStyle"i><"myStyle"p>>',
        language:
            {
                url: '/pqs9900/json/chine.json'
            }
        ,
        createdRow: function (row, data, index) {
            var $btnView = $('<button type="button" class="btn btn-danger ml10"><i class="glyphicon glyphicon-trash"></i>删除</button><button type="button" class="btn themeBGColor updateData ml10"><i class="glyphicon glyphicon-edit"></i>编辑</button>');
            $('td', row).eq(2).append($btnView);
        }
        ,
        fnDrawCallback: function (objTable) {
            $("#excelList_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 = $("#excelList").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(objTable, {
        buttons: [
            {
                extend: 'csv',//导出格式
                title: '监测点报表列表',//导出csv名称
                exportOptions: {
                    columns: [0, 1],//导出需要的列
                    format: {
                        body: function (data) {//对导出内容的筛选 data 表格内容 rid 行索引 cid 列索引
                            return data;
                        }
                    }
                }
            }
        ]
    });
    modifyCss(objTable);
}

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 showPloy(name, describe, rpIndex) {
    layer.open({
        type: 2,
        area: ['40%', '98%'],
        resize: false,
        title: '编辑模板',
        content: ['addexcelploy', 'no'],
        success: function (layero, index) {
            $(layero).find("iframe")[0].contentWindow.setUpdate();
            $(layero).find("iframe")[0].contentWindow.setValue(name, describe, rpIndex);
        }
    });
}


//跳转新建模板页面
function addPloy() {
    layer.open({
        type: 2,
        area: ['40%', '98%'],
        resize: false,
        title: '新建模板',
        content: ['addexcelploy', 'no']
    });
}


//跳转已有模板页面
function showNode() {
    layer.open({
        type: 2,
        resize: false,
        area: ['100%', '100%'],
        title: '字典管理',
        content: ['addexceldata', 'no']
    });
}

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

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

}

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

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