var oTable=null;
var resultData = null;
function load() {
    $.ajax({
        type: "POST",
        url: "/pqs9900/manage/getManage",
        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;
    // }
    oTable = $("#manageId").DataTable();
    oTable.clear().draw();
    oTable = $('#manageId').DataTable({
        scrollX: true,
        lengthChange: false,
        bDestroy: true,
        dom: 'rtip',
        ordering: false,
        columnDefs: [{
            targets: [],
            visible: false
        }],
        columns: [
            {"width": "15%"},
            {"width": "10%"},
            {"width": "15%"},
            {"width": "15%"},
            {"width": "10%"}
        ],
        data: data.body,
        destroy: true,
        pageLength: length,
        language: {
            url: '/pqs9900/json/chine.json',
            "sEmptyTable": "没有内容"
        },
        'fnDrawCallback': function (oTable) {
            $("#manageId_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 = $("#manageId").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],//导出需要的列
                    format: {
                        body: function (data) {//对导出内容的筛选 data 表格内容 rid 行索引 cid 列索引
                            return data;
                        }
                    }
                }
            }
        ]
    });
    modifyCss(oTable);
}

$(document).ready(function () {
    //上传资源到指定路径
    $("#uploadManageList").click(function () {
        layer.open({
            type: 2,
            title: '上传资源',
            shadeClose: true,
            resize: false,
            shade: 0.3,
            area: ['20%', '50%'],
            content: 'uploadManageList', //iframe的url
            scrollbar: false,
            end: function () {
                close();
            }
        });
    });
    load();
})

//修改
function updateManageByIndex(index) {
    var username = $("#username").val();
    confirmIndentity(username, function () {
        $("#ResIndex").val(index);
        layer.open({
            type: 2,
            title: '修改资源信息',
            shadeClose: true,
            resize: false,
            shade: 0.3,
            area: ['25%', '50%'],
            content: 'updateManage', //iframe的url
            scrollbar: false,
            end: function () {
                close();
            }
        });
    })
}

//删除
function deleteManageByIndex(index) {
    var username = $("#username").val();
    confirmIndentity(username, function () {
    var res_Index = index;
    layer.confirm('确定删除？', function (index) {
            $.ajax({
                type: "POST",
                url: "/pqs9900/manage/deleteManageByIndex",

                dataType: "text",
                data: {
                    res_Index: res_Index
                },
                success: function (data) {
                    if (data == 0) {
                        layer.msg("该资源的目录不存在或目录已被删除", {icon: 5, time: 2000}, function () {
                            load();
                        });

                    } else {
                        //删除资源成功
                        layer.msg("删除资源成功", {icon: 1, time: 2000}, function () {
                            load();
                        });

                    }

                }

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

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

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

}

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

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

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]);
}