var pageNo = 1;
var totalPages = 0;
//数据列表
getListDataAjax();

//获取数据列表 ajax请求
function getListDataAjax(queryParam = {}, pageNo = 1) {

    queryParam.pageNo = pageNo;
    // queryParam.pageSize = 20;

    ajaxRequest(LOG_MANAGE_LIST, "POST", queryParam, function(result) {
        let data = result.RESULT.pageList;
        let items = '';
        data.forEach(function(item, key) {
            items += "<tr>" +
                "<td class='status' data-user='" + JSON.stringify(item) + "' data-id=" + item.id + "></td>" +
                "<td>" + item.xh + "</td>" +
                "<td>" + item.user_name + "</td>" +
                "<td>" + getFormatDate(item.creat_date.time) + "</td>" +
                "<td>" + item.operation + "</td>" +
                "<td>" + item.time + "</td>" +
                "<td>" + item.ip + "</td>" +
                "</tr>"
        })
        $('#dataList').html(items);
        $('#totalCount').html(result.totalCount)
        $('#handled').html(result.yclCount)
        $('#unhandle').html(result.wclCount)
        $('#handleRate').html(result.cll)

        totalPages = result.RESULT.totalPages;
        $('#totalPage').html(totalPages); //总共多少页
        $('#dataTotal').html(result.RESULT.totalCount); //总共多少条数据
        $('#currentPage').val(pageNo); //当前页面

        let pageFrom = (pageNo - 1) * result.RESULT.pageSize + 1; //开始
        let pageTo = result.RESULT.pageNo * result.RESULT.pageSize; //结束
        pageTo = pageTo > result.RESULT.totalCount ? result.RESULT.totalCount : pageTo;

        $('#pageFrom').html(pageFrom);
        $('#pageTo').html(pageTo);

        // 无数据时
        if (!data.length) {
            $('.pager.has-data').hide()
            $('.pager.no-data').show()
        } else {
            $('.pager.has-data').show()
            $('.pager.no-data').hide()
        }
        if (pageNo < totalPages) {
            $('#nextPageButton,#lastPageButton').removeClass('disabled');
        } else {
            $('#nextPageButton,#lastPageButton').addClass('disabled');
        }
        if (pageNo === 1) {
            $('#firstPageButton,#prevPageButton').addClass('disabled');
        } else {
            $('#firstPageButton,#prevPageButton').removeClass('disabled');
        }
    }, function(errorMsg) {
        alert("请求数据失败!");
    })
}

//按钮搜索
$('#buttonSearch').on('click', function() {
    pageNo = 1
    getListDataAjax(getSearchParamObj());
})

//拼接搜索条件
function getSearchParamObj() {
    let queryParam = {};
    let start_creat_date = $('#start_creat_date').val();
    let end_creat_date = $('#end_creat_date').val()
    let user_name = $('#user_name').val()
    queryParam.start_creat_date = start_creat_date;
    queryParam.end_creat_date = end_creat_date;
    queryParam.user_name = user_name;
    return queryParam;
}

//重置表单
$('.reset').click(resetForm)
    //重置表单
function resetForm() {
    pageNo = 1
    $("#user_name").val("");
    $("#start_creat_date").val("");
    $("#end_creat_date").val("");
    getListDataAjax(getSearchParamObj());
}

//分页操作
$('#firstPageButton').on('click', function() {
    pageNo = 1;
    getListDataAjax(getSearchParamObj(), 1);
})

$('#lastPageButton').on('click', function() {
    pageNo = totalPages;
    getListDataAjax(getSearchParamObj(), pageNo);
})

$('#prevPageButton').on('click', function() {
    pageNo -= 1;
    getListDataAjax(getSearchParamObj(), pageNo);
})

$('#nextPageButton').on('click', function() {
    pageNo += 1;
    getListDataAjax(getSearchParamObj(), pageNo);
})


// 导出 start
$('.export').click(getDataExport)

function getDataExport() {
    // 输出base64编码
    const base64 = s => window.btoa(unescape(encodeURIComponent(s)));

    ajaxRequest(LOG_EXPORT, "POST", {}, function(result) {
        let data = result.RESULT.data;
        let newData = [];

        data.forEach(function(item, index) {
            newData.push({ xh: item.xh, user_name: item.user_name, creat_date: getFormatDate(item.creat_date.time), operation: item.operation, time: item.time, ip: item.ip })
        });
        let str = '<tr style="text-align:center"><th>序号</th><th>操作人员</th><th>操作时间</th><th>操作内容</th><th>执行时间</th><th>请求IP</th></tr>';
        // 循环遍历，每行加入tr标签，每个单元格加td标签
        for (let i = 0; i < newData.length; i++) {
            str += '<tr style="text-align:center">';
            for (const key in newData[i]) {
                // 增加\t为了不让表格显示科学计数法或者其他格式
                str += `<td>${ newData[i][key] + '\t'}</td>`;
            }
            str += '</tr>';
        }
        // Worksheet名
        const worksheet = 'Sheet1'
        const uri = 'data:application/vnd.ms-excel;base64,';

        // 下载的表格模板数据
        const template = `<html xmlns:o="urn:schemas-microsoft-com:office:office" 
        xmlns:x="urn:schemas-microsoft-com:office:excel" 
        xmlns="http://www.w3.org/TR/REC-html40">
        <head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>
        <x:Name>${worksheet}</x:Name>
        <x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet>
        </x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]-->
        </head><body><table>${str}</table></body></html>`;

        // 通过创建a标签实现
        const link = document.createElement("a");
        link.href = uri + base64(template);
        // 对下载的文件命名
        link.download = "日志管理数据表.xls";
        link.click();

    }, function(errorMsg) {
        alert("请求数据失败!");
    })
}
// 导出  end