var pageNo = 1;
var totalPages = 0;

//列表渲染
getListDataAjax();

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

    queryParam.pageNo = pageNo;
    // queryParam.pageSize = 4;

    ajaxRequest(EVENT_MANAGE_LIST, "POST", queryParam, function(result) {
        let data = result.RESULT.pageList;
        let items = '';
        if (result.RESULT.totalCount > 0) {
            data.forEach(function(item, key) {
                var clzt = item.clzt == 1 ? '已处理' : '未处理';
                var clztClassName = item.clzt == 1 ? 'processed' : 'unprocess';
                var cl_tiem = item.cl_tiem ? getFormatDate(item.cl_tiem) : '';
                items += "<tr>" +
                    "<td class='status' data-user='" + JSON.stringify(item) + "' data-id=" + item.id + "></td>" +
                    "<td>" + item.xh + "</td>" +
                    "<td>" + item.company_name + "</td>" +
                    "<td>" + getFormatDate(item.reporting_time.time) + "</td>" +
                    "<td>" + item.event_name + "</td>" +
                    "<td>" + item.event_type_name + "</td>" +
                    "<td class='" + clztClassName + "'>" + clzt + "</td>" +
                    "<td>" + item.cl_people + "</td>" +
                    "<td>" + cl_tiem + "</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 event_name = $('#event_name').val();
    let event_type = $('#event_type').val()
    queryParam.event_name = event_name;
    queryParam.event_type = event_type;
    return queryParam;
}

//重置表单
$('.reset').click(resetForm)

//重置表单
function resetForm() {
    pageNo = 1;
    $("#event_name").val("");
    $("#event_type").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);
})

//单位下拉
getNameList()

//单位下拉 ajax请求
function getNameList() {
    ajaxRequest(DEVICE_TYPE_COMPANYLIST, "POST", {}, function(result) {
        let data = result.RESULT;
        let items = '';

        data.forEach(function(item, key) {
            items += `<option value="${item.owner_id}">${item.owner_name}</option>`
        })
        $('#getNameList').append(items);
        $('#getNameList2').append(items);
    }, function(errorMsg) {
        alert("请求数据失败!");
    })
}

/*新增 修改 关闭 弹框*/
var layer = layui.layer;
var layerCreateIndex = '';
var layerUpdateIndex = ''
layui.use('layer', function() {
    //新增弹框
    $('.add').click(function() {
            layerCreateIndex = layer.open({
                type: 1,
                title: false,
                closeBtn: 0,
                shadeClose: true,
                skin: 'yourclass',
                area: ['400px', '400px'],
                content: $(".addDataOut"),
                success: function() {
                    $('.clsBtn,.cancel').click(function() {
                        layer.close(layerCreateIndex);
                    })
                }
            })
        }),
        //处置弹框信息 
        $('.handle').click(function() {
            if (!$('.pure-table tr').has('.checked').length) {
                layer.msg('请选择一条需要修改的信息!', { icon: 5 });
            } else {
                let userInfo = $('.pure-table tr').find('.checked').data('user');
                $('.handleDataOut input[name=company_name]').val(userInfo.company_name)
                $('.handleDataOut input[name=event_type]').val(userInfo.event_type_name)
                $('.handleDataOut input[name=event_name]').val(userInfo.event_name)
                $('.handleDataOut input[name=event_content]').val(userInfo.event_content)
                $('.handleDataOut textarea[name=cl_content]').val(userInfo.cl_content)
                $('.handleDataOut input[name=id').val(userInfo.id)
                if ($('.handleDataOut textarea[name=cl_content]').val()) {
                    $('.handleDataOut textarea[name=cl_content]').attr({ readonly: 'true' })
                    $('#handleUpdate').addClass('disabled')
                } else {
                    $('.handleDataOut textarea[name=cl_content]').removeAttr("readonly")
                    $('#handleUpdate').removeClass('disabled')
                }
                layerUpdateIndex = layer.open({
                    type: 1,
                    title: false,
                    closeBtn: 0,
                    shadeClose: true,
                    skin: 'yourclass',
                    area: ['410px', '400px'],
                    content: $(".handleDataOut"),
                    success: function() {
                        $('.clsBtn,.cancel').click(function() {
                            layer.close(layerUpdateIndex);
                        })
                    }
                });
            }
        })

    //删除信息
    $('.delete').click(function() {
        if (!$('.pure-table tr').has('.checked').length) {
            //layer.msg('请选择一条需要修改的信息');
            layer.msg('请选择一条需要删除的信息!', { icon: 5 });
        } else {
            let dataId = $('.pure-table tr').find('.checked').data('id');

            ajaxRequest(DEVICE_CHECKCHANNEL_DELETE, "POST", { "ID_LIST": [{ "id": dataId }] }, function(result) {

                $(".pure-table tbody tr.selected").remove()
                layer.close(layer.layerCreateIndex);
                layer.msg('删除成功!', { icon: 6 });
                getListDataAjax()
            }, function(errorMsg) {
                alert("用户删除失败!");
            }, 2)
        }
    })
})


/* 新增发送请求 */
$('#addData').click(function() {

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

    //验证数据是否为空
    let res = validParamIsEmpty(allParam, {
        "event_name": "请输入事件名称",
        "event_content": "请输入事件内容",
        "event_type": "请选择事件类型",
        "company_code": "请选择所属单位",
    });

    if (res.code == -1) {
        alert(res.msg);
        return;
    }

    //验证通过 请求ajax
    ajaxRequest(EVENT_MANAGE_ADD, "POST", allParam, function(result) {
        layer.close(layerCreateIndex);
        layer.msg('添加成功!', { icon: 6 });
        getListDataAjax();
    }, function(errorMsg) {
        alert("异常错误!");
    })
})

/* 处置发送请求 */
$('#handleUpdate').click(function() {

    //获取表单的值 并转换成对象
    let allParam = serializeArrayToObj($("#handleDataForm").serializeArray());
    //验证数据是否为空
    let res = validParamIsEmpty(allParam, {
        "cl_content": "请输入处理内容",
    });

    if (res.code == -1) {
        alert(res.msg);
        return;
    }
    ajaxRequest(EVENT_MANAGE_HANDLE, "POST", allParam, function(result) {
        layer.close(layerUpdateIndex);
        layer.msg('处置成功!', { icon: 6 });
        getListDataAjax();
    }, function(errorMsg) {
        alert("数据修改失败!");
    })
})