var pageNo = 1;
var totalPages = 0;

//列表
getListDataAjax();

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

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

    ajaxRequest(BUILDING_ROOM_LIST, "POST", queryParam, function(result) {
        let data = result.pageList;
        let items = '';
        data.forEach(function(item, key) {
            items += "<tr>" +
                "<td class='status' data-id=" + item.roomId + " data-user='" + JSON.stringify(item) + "'></td>" +
                "<td>" + item.roomName + "</td>" +
                "<td>" + item.creatMan + "</td>" +
                "<td>" + getFormatDate(item.creatTime) + "</td>" +
                "<td>" + item.remark + "</td>" +
                "</tr>"
        })
        $('#dataList').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("请求数据失败!");
    })
}


getBuilding()
    //获取楼层下拉
function getBuilding(queryParam = {}) {
    ajaxRequest(BUILDING_LIST, "POST", queryParam, function(result) {
        let data = result.pageList;
        let items = '';
        data.forEach(function(item, key) {
            items += `<option value="${item.id}">${item.buildName}</option>`
        })
        $('#buildId').append(items);
    }, function(errorMsg) {
        alert("请求数据失败!");
    })
}


getFloor()
    //获取楼层下拉
function getFloor(queryParam = {}) {
    ajaxRequest(BUILDING_FLOOR_LIST, "POST", queryParam, function(result) {
        let data = result.pageList;
        let items = '';
        data.forEach(function(item, key) {
            items += `<option value="${item.floorId}">${item.floorName}</option>`
        })
        $('#floorId').append(items);
        $('#floorId2').append(items);
        $('#floorIdSearch').append(items);
    }, function(errorMsg) {
        alert("请求数据失败!");
    })
}


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

//拼接搜索条件
function getSearchParamObj() {
    let queryParam = {};
    let roomName = $('#roomName').val();
    let floorIdSearch = $('#floorIdSearch').val();
    queryParam.roomName = roomName;
    queryParam.floorId = floorIdSearch;
    return queryParam;
}

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

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



/*新增 修改 关闭 弹框*/
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() {
                        //alert(1)
                        layer.close(layerCreateIndex);
                    })
                }
            })
        }),
        //修改弹框信息 
        $('.edit').click(function() {
            if (!$('.pure-table tr').has('.checked').length) {
                layer.msg('请选择一条需要修改的信息!', { icon: 5 });
            } else {
                let userInfo = $('.pure-table tr').find('.checked').data('user');
                $('.editDataOut input[name=roomName]').val(userInfo.roomName)
                $('.editDataOut select[name=floorId]').val(userInfo.floorId)
                $('.editDataOut input[name=remark').val(userInfo.remark)
                $('.editDataOut select[name=status]').val(userInfo.status)
                $('.editDataOut input[name=roomId').val(userInfo.roomId)

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

    //删除信息
    $('.delete').click(function() {
        if (!$('.pure-table tr').has('.checked').length) {
            layer.msg('请选择一条需要删除的信息!', { icon: 5 });
        } else {
            let dataId = $('.pure-table tr').find('.checked').data('id');
            ajaxRequest(BUILD_ROOM_DELETE, "POST", { "roomId": dataId }, function(result) {
                if (result.flag) {
                    $(".pure-table tbody tr.selected").remove()
                    layer.close(layer.layerCreateIndex);
                    layer.msg(result.msg, { icon: 6 });
                    getListDataAjax()
                } else {
                    //服务端返回报错
                    layer.msg(result.msg, { icon: 6 });
                }
                getListDataAjax()
            }, function(errorMsg) {
                alert("删除失败!");
            })
        }
    })
})

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

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

    //验证数据是否为空
    let res = validParamIsEmpty(allParam, {
        "roomName": "请输入房间名称",
        "floorId": "请选择所属楼层",
        "remark": "请输入备注",

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

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


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

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

    //验证数据是否为空
    let res = validParamIsEmpty(allParam, {
        "roomName": "请输入房间名称",
        "floorId": "请选择所属楼层",
        "remark": "请输入备注",
    });

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

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