/**
 * 审计权限
 */
var options = $("#typeSelect option:selected").val();
var option = $("#detailedSelect option:selected").val();
var username = $("#username option:selected").val();
var endTime = getEndTime();
var startTime = getStartTime("年份", endTime);
var ii;
var obj = document.getElementById('detailedSelect');
obj.options.add(new Option("全部", "")); // 全部
function load(username, options, option, startTime, endTime) {
    var i;
    $.ajax({
        type: "POST",
        url: "/pqs9900/log/getUserLogId",
        data: {
            username: username,
            type: options,
            name: option,
            startTime: startTime,
            endTime: endTime
        },
        beforeSend: function () {
            i = ityzl_SHOW_LOAD_LAYER();
        },
        success: function (data) {
            initTable(data.body);
            //***********关闭loading
            ityzl_CLOSE_LOAD_LAYER(i);
        }
    });
}

function loadWarning() {
//	var type = 0;
//	$.ajax({
//        type: "POST",
//        url: "/pqs9900/log/getWarnInfo",
//        data: {
//        	type:type
//        },
//        success: function (data) {
//            //没有权限的请求
//            if (data == 300) {
//                location.href = '/pqs9900/auth/unauthorized';
//                //没有登陆的请求
//            } else if (data == 301) {
//                location.href = '/pqs9900';
//            } else {
//            	if(data.code == 500){
//            		
//            	}else{
//	            	if(data.body.length > 0){
//	            		layer.open({
//	                        type: 2,
//	                        title: false,
//	                        anim: 2,
//	                        area: ['800px','200px'],
//	                        offset: 'rb',
//	                        content: 'warnwin',
//	                        shadeClose: true,
//	                        yes: function () {
//	                            layer.closeAll();
//	                        },
//	                        success: function (layero, index) {
//	                            var t=setTimeout(function(){
//	                            	layer.close(index);
//	                            },10000);
//	                        }
//	                    });
//	            	}
//            	}
//            }
//        }
//    });

    $.ajax({
        type: "POST",
        url: "/pqs9900/log/getMemoInfo",
        data: {},
        success: function (data) {
            if (data.code == 500) {

            } else {
//	            		layer.msg("日志表存储空间已超过1G,请及时备份清理!", {
//							icon : 2,
//							time : 1000
//						});
                layer.close(ii);
                ii = layer.open({
                    type: 2,
                    title: false,
                    anim: 2,
                    area: ['400px', '50px'],
                    offset: 'lb',
                    content: 'seriouswin?size=' + data.body,
                    shadeClose: true,
                    yes: function () {
                        layer.closeAll();
                    },
                    success: function (layero, index) {
                    }
                });
            }
        }
    });
}

var type;
var detailed;
var oTable;
var inter;

function getIndex(index) {
    $.ajax({
        type: 'POST',
        dataType: "json",
        cache: false,
        url: '/pqs9900/dic/selAllType',
        data: {
            typeId: JSON.stringify(index)
        },
        traditional: true,
        error: function () {
        },
        success: function (data) {
            detailed = data;
            // 插入信息到combox中
            for (var i = 0; i < data.length; i++) {
                obj.options.add(new Option(data[i].dicName, data[i].dicIndex));
            }
        }
    });
}

function geteventtype() {
    $.ajax({
        type: 'POST',
        dataType: "json",
        url: '/pqs9900/type/getLog',
        error: function () {
        },
        success: function (data) {
            // 插入信息到combox中
            type = data;
            var obj = document.getElementById('typeSelect');
            obj.options.add(new Option("全部", "")); // 全部
            var aoData = new Array();
            var tmptype = "";

            for (var i = 0; i < data.length; i++) {
                obj.options.add(new Option(data[i].dicTypeName, data[i].dicTypeIndex));

                var list = new Object();
                if (i == data.length - 1) {
                    tmptype += data[i].dicTypeIndex;
                } else {
                    tmptype += data[i].dicTypeIndex + ",";
                }
                list.type = tmptype;
            }
            aoData.push(list);
            getIndex(aoData);
        }
    });
}

function getUsername() {
    $.ajax({
        type: 'POST',
        dataType: "json",
        cache: false,
        url: '/pqs9900/log/getUsername',
        data: {},
        error: function () {
        },
        success: function (data) {
            // 插入信息到combox中
            var obj = document.getElementById('username');
            obj.options.add(new Option("全部", "")); // 全部
            for (var i = 0; i < data.length; i++) {
                obj.options.add(new Option(data[i], data[i]));
            }
        }
    });
}

$(document).ready(function () {
    $("#tj").click(function () {
        layer.open({
            type: 2,
            title: '统计',
            resize: false,
            shadeClose: true,
            shade: 0.3,
            area: ['100%', '100%'],
            content: 'sjtj', //iframe的url
            scrollbar: false
        });
    })
    getUsername();
    geteventtype();
    load(username, options, option, startTime, endTime);
    loadWarning();
    inter = setInterval('loadWarning()', 60000);
    $("#typeSelect").change(
        function () {
            var options = $("#typeSelect option:selected");
            var aoData = new Array();
            var tmptype = "";
            if (options.val() == "") {
                $("#typeSelect option").each(function () {
                    tmptype += $(this).val() + ",";
                })
                tmptype = tmptype.substring(0, tmptype.lastIndexOf(','));
            } else {
                tmptype += options.val();
            }
            var list = new Object();
            list.type = tmptype;
            aoData.push(list);
            $("#detailedSelect").find("option").remove();
            $.ajax({
                type: 'POST',
                dataType: "json",
                cache: false,
                url: '/pqs9900/dic/selAllType',
                data: {
                    typeId: JSON.stringify(aoData)
                },
                traditional: true,
                error: function () {
                },
                success: function (data) {
                    detailed = data;
                    // 插入信息到combox中
                    var obj = document.getElementById('detailedSelect');
                    obj.options.add(new Option("全部", "")); // 全部
                    if (data.length == 0) {
                        geteventtype();
                    } else {
                        for (var i = 0; i < data.length; i++) {
                            obj.options.add(new Option(data[i].dicName, data[i].dicIndex));
                        }
                    }
                }
            });
        });
    $("#query").click(function () {
        var options = $("#typeSelect option:selected").val();
        var option = $("#detailedSelect option:selected").val();
        var username = $("#username option:selected").val();
        var startTime = $("#startTime").val();
        var endTime = $("#endTime").val();
        var datatable = $('#example').DataTable();
        load(username, options, option, startTime, endTime);
    })
});


function shutdownsc() {
    window.clearInterval(inter);
}

function initTable(data) {
    $('#example').dataTable().fnClearTable();
    oTable = $("#example").DataTable();
    oTable.clear().draw();
    oTable = $('#example').DataTable({
        destroy: true,
        "aaSorting": [[1, "desc"]],
        data: data,
        "columnDefs": [{
            "targets": 0,
            "visible": false,
            "searchable": false
        }],
        columns: [
            {"width": "5%"},
            {"width": "11%"},
            {"width": "8%"},
            {"width": "7%"},
            {"width": "40%"},
            {"width": "10%"},
            {"width": "7%"},
            {"width": "10%"},
            {"width": "7%"}
        ],
        lengthMenu: [12],
        language: {
            url: '/pqs9900/json/chine.json'
        },
        "createdRow": function (row, data, index) {
            if (data[4].length > 8) {
                var realValue = data[4];
                var s = data[4].substr(0, 8) + '...'
                $('td', row).eq(4).prevObject[4].innerHTML = "<span title='" + realValue + "'>" + s + "</span>";
            }
        },
        '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 = $("#example").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: [1, 2, 3, 4, 5, 6, 7, 8],//导出需要的列
                    format: {
                        body: function (data) {//对导出内容的筛选 data 表格内容 rid 行索引 cid 列索引
                            return data;
                        }
                    }
                }
            }
        ]
    });
    modifyCss(oTable);
}

$("body").on("click", ".buttons-csv", function () {
    expcsv();
})

function expcsv() {
    var message = "导出日志列表";
    $.ajax({
        type: 'POST',
        dataType: "text",
        data: {
            message: message
        },
        cache: false,
        url: '/pqs9900/log/expcsv',
        error: function () {
        },
        success: function (data) {

        }
    })
}

function modifyCss(table) {
    table.buttons().container().css("display", "inline-block");
    table.buttons().container().css("float", "right");
    table.buttons().container().css("right", "4px");
    table.buttons().container().find("a").prepend("<i class='glyphicon glyphicon-share'></i>");
    table.buttons().container().appendTo('.header');
}