var pageNo = 1;
var totalPages = 1;

//安全机制列表
getListDataAjax();

//获取安全机制列表 ajax请求
function getListDataAjax(queryParam = {}, pageNo = 1) {

    queryParam.pageNo = pageNo;
    // queryParam.pageSize = 1;

    ajaxRequest(SECURITY_LIST, "POST", queryParam, function(result) {

        let data = result.pageList;
        let items = '';
        data.forEach(function(item, key) {
            let status = item.status ? '启动' : '未启动';
            items += "<tr>" +
                "<td class='status' data-user='" + JSON.stringify(item) + "' data-id=" + item.id + "></td>" +
                "<td>" + item.times + "</td>" +
                "<td>" + item.restartTime + "</td>" +
                "<td>" + item.id + "</td>" +
                "<td>" + item.intervalTime + "</td>" +
                "<td>" + status + "</td>" +
                "</tr>"
        })
        $('#securityList').html(items);

        totalPages = result.totalPages;
        $('#totalPage').html(totalPages); //总共多少页
        $('#dataTotal').html(result.totalCount); //总共多少条数据
        $('#currentPage').val(pageNo); //当前页面
        let pageFrom = (pageNo - 1) * result.pageSize + 1; //开始
        let pageTo = result.pageNo * result.pageSize; //结束

        pageTo = pageTo > result.totalCount ? 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("请求数据失败!");
    })
}

//拼接搜索条件
function getSearchParamObj() {
    let queryParam = {};
    return queryParam;
}

//分页操作
$('#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);
})

/*新增 修改 关闭 弹框*/
var layer = layui.layer;
var layerUpdateIndex = ''

layui.use('layer', function() {

    //修改弹框信息
    $('.edit').click(function() {

        if (!$('.pure-table tr').has('.checked').length) {
            layer.msg('请选择一条需要修改的信息!', { icon: 5 });

        } else {
            let dataId = $('.pure-table tr').find('.checked').data('id');
            let secyrityInfo = $('.pure-table tr').find('.checked').data('user');

            $('.editSecurityOut input[name=times]').val(secyrityInfo.times)
            $('.editSecurityOut input[name=restartTime]').val(secyrityInfo.restartTime)
            $('.editSecurityOut input[name=intervalTime]').val(secyrityInfo.intervalTime)
            $('.editSecurityOut select[name=status').val(secyrityInfo.status)
            $('.editSecurityOut input[name=id').val(secyrityInfo.id)

            layerUpdateIndex = layer.open({
                type: 1,
                title: false,
                closeBtn: 0,
                shadeClose: true,
                skin: 'yourclass',
                area: ['400px', '300px'],
                content: $(".editSecurityOut"),
                success: function() {
                    $('.clsBtn,.cancel').click(function() {
                        layer.close(layerUpdateIndex);
                    })
                }
            });
        }
    })
})

/* 修改安全机制发送请求 */
$('#dataUpdate').click(function() {

    //获取表单的值 并转换成对象
    let allParam = serializeArrayToObj($("#updateSecurityForm").serializeArray());

    // 验证数据是否为空
    let res = validParamIsEmpty(allParam, {
        "times": "请填写错误次数",
        "restartTime": "请填写重置时间",
        "intervalTime": "请填写重试间隔时间",
        "status": "请填写机制状态"
    });
    if (res.code == -1) {
        alert(res.msg);
        return;
    }

    //验证通过 请求ajax
    ajaxRequest(SECURITY_UPDATE, "POST", allParam, function(result) {
        layer.close(layerUpdateIndex);
        layer.msg('修改成功!', { icon: 6 });
        getListDataAjax()

    }, function(errorMsg) {
        alert("数据修改失败!");

    })

})