var pageNo = 1;
var totalPages = 0;

//列表渲染
getListDataAjax();

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

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

    ajaxRequest(EVENT_DANGER_MANAGE_LIST, "POST", queryParam, function(result) {
        let data = result.RESULT.pageList;
        let items = '';
        if (result.RESULT.totalCount > 0) {
            data.forEach(function(item, key) {

                var time = item.time.time ? getFormatDate(item.time.time) : '';
                var clsj = item.clsj.time ? getFormatDate(item.clsj.time) : '';
                var clztClassName = item.clzt == '已处理' ? 'processed' : 'unprocess';

                items += "<tr>" +
                    "<td class='status' data-user='" + JSON.stringify(item) + "' data-id=" + item.id + "></td>" +
                    "<td>" + item.orderIdx + "</td>" +
                    "<td>" + item.company_name + "</td>" +
                    "<td>" + time + "</td>" +
                    "<td>" + item.data + "</td>" +
                    "<td>" + item.data4 + "</td>" +
                    "<td>" + item.fullname + "</td>" +
                    "<td class='" + clztClassName + "'>" + item.clzt + "</td>" +
                    "<td>" + item.clr + "</td>" +
                    "<td>" + clsj + "</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 time_start = $('#time_start').val();
    let time_end = $('#time_end').val()
    let clzt = $('#clzt').val()
    queryParam.time_start = time_start;
    queryParam.time_end = time_end;
    queryParam.clzt = clzt;
    return queryParam;
}

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

//重置表单
function resetForm() {
    pageNo = 1;
    $("#clzt").val("");
    $("#time_start").val("");
    $("#time_end").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() {
    //处置弹框信息 
    $('.handle').click(function() {
        if (!$('.pure-table tr').has('.checked').length) {
            layer.msg('请选择一条需要修改的信息!', { icon: 5 });
        } else {
            //重新渲染表单
            $('#handleDataForm')[0].reset()
            layui.use(['form'], function() {
                var form = layui.form;
                form.render('radio');
            });
            let userInfo = $('.pure-table tr').find('.checked').data('user');
            $('.handleDataOut input[name=company_name]').val(userInfo.company_name)
            $('.handleDataOut input[name=name]').val(userInfo.name)
            $('.handleDataOut input[name=data]').val(userInfo.data)
            $('.handleDataOut input[name=id').val(userInfo.id)
            $('.handleDataOut input[name=dwtype]').val(userInfo.dwtype)
            $('.handleDataOut input[name=status]').val(userInfo.status)
            $('.handleDataOut input[name=device_code]').val(userInfo.device_code)
            $('.handleDataOut textarea[name=clnr]').val(userInfo.clnr)
            if (userInfo.clzt == "已处理") {
                $('.handleDataOut textarea[name=clnr]').attr({ readonly: 'true' })
                $('#handleUpdate').addClass('disabled')
                $('.handle-range').hide()
                $('.handleDataOut input[name=device_code]').val(userInfo.device_code)
                $('.handleDataOut input[name=clwb]').val(userInfo.clwb);
                $("input[type=radio]").attr("disabled", "true")
                if (userInfo.clwb == 0) {
                    $("#no").prop("checked", true);
                } else {
                    $("#yes").prop("checked", true);
                }
                layui.use(['form'], function() {
                    var form = layui.form;
                    form.render('radio');
                });
            } else {
                $('.handleDataOut textarea[name=clnr]').removeAttr("readonly")
                $("input[type=radio]").removeAttr("disabled")
                $('#handleUpdate').removeClass('disabled')
                $('.handle-range').show()
                layui.use(['form'], function() {
                    var form = layui.form;
                    form.render('radio');
                });
            }

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

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

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

    //验证数据是否为空
    let res = validParamIsEmpty(allParam, {
        "clnr": "请输入处理内容",
    });

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

    ajaxRequest(EVENT_DANGER_MANAGE_HANDLE, "POST", allParam, function(result) {
        layer.close(layerUpdateIndex);
        layer.msg('处置成功!', { icon: 6 });
        getListDataAjax();
    }, function(errorMsg) {
        alert("数据修改失败!");
    })
})