var pageNo = 1;
var totalPages = 0;

//列表
getListDataAjax();

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

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

    ajaxRequest(BUILDING_LIST, "POST", queryParam, function(result) {
        let data = result.pageList;
        let items = '';
        data.forEach(function(item, key) {

            items += "<tr>" +
                "<td class='status' data-id=" + item.id + " data-user='" + JSON.stringify(item) + "'></td>" +
                "<td>" + item.buildName + "</td>" +
                "<td>" + item.posistion + "</td>" +
                "<td>" + item.addr1 + "</td>" +
                "<td>" + item.addr2 + "</td>" +
                "<td>" + item.addr3 + "</td>" +
                "<td>" + item.address + "</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("请求数据失败!");
    })
}

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

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

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

//重置表单
$('.reset').click(resetForm);
//分页刷新按钮
$('.pg-refresh').click(resetForm);

//重置表单
function resetForm() {
    pageNo = 1;
    $("#buildName").val("");
    getListDataAjax(getSearchParamObj());
}

/*新增 修改 关闭 弹框*/
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);
                    })
                }
            })
        }),
        //修改弹框信息 
        $('.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=buildName]').val(userInfo.buildName)
                $('.editDataOut input[name=posistion]').val(userInfo.posistion)
                $('.editDataOut select[name=addr1]').val(userInfo.addr1)
                provinceText = userInfo.addr1;
                $.each(provinceList, function(i, item) {
                    if (provinceText == item.name) {
                        cityItem = i;
                        return cityItem
                    }
                });
                removeEle(city);
                removeEle(town);
                $.each(provinceList[cityItem].cityList, function(i, item) {
                    addEle(city, item.name)
                })
                $('.editDataOut select[name=addr2]').val(userInfo.addr2)
                cityText = userInfo.addr2;
                removeEle(town);
                $.each(provinceList, function(i, item) {
                    if (provinceText == item.name) {
                        cityItem = i;
                        return cityItem
                    }
                });
                $.each(provinceList[cityItem].cityList, function(i, item) {
                    if (cityText == item.name) {
                        for (var n = 0; n < item.areaList.length; n++) {
                            addEle(town, item.areaList[n])
                        }
                    }
                });

                $('.editDataOut select[name=addr3]').val(userInfo.addr3)
                $('.editDataOut input[name=id]').val(userInfo.id)

                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('请选择一条需要修改的信息');
            layer.msg('请选择一条需要删除的信息!', { icon: 5 });
        } else {
            let dataId = $('.pure-table tr').find('.checked').data('id');
            ajaxRequest(BUILD_MANAGE_DELETE, "POST", { "buildId": dataId }, function(result) {
                if (result.flag) {
                    $(".pure-table tbody tr.selected").remove()
                    layer.close(layer.layerCreateIndex);
                    layer.msg(result.msg, { icon: 6 });
                } else {
                    //服务端返回报错
                    alert(result.msg);
                }
                getListDataAjax()
            }, function(errorMsg) {
                alert("删除失败!");
            })
        }
    })
})

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

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

    //验证数据是否为空
    let res = validParamIsEmpty(allParam, {
        "buildName": "请输入建筑名称",
        "posistion": "请输入建筑坐标",
        "addr1": "请选择省份",
        "addr2": "请选择市",
        "addr3": "选择区、县",
        "address": "请输入详细地址",
    });

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

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

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

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

    //验证数据是否为空
    let res = validParamIsEmpty(allParam, {
        "buildName": "请输入建筑名称",
        "posistion": "请输入建筑坐标",
        "addr1": "请选择省份",
        "addr2": "请选择市",
        "addr3": "选择区、县",
    });
    if (res.code == -1) {
        alert(res.msg);
        return;
    }
    ajaxRequest(BUILD_MANAGE_UPDATE, "POST", allParam, function(result) {
        layer.close(layerUpdateIndex);
        layer.msg('修改成功!', { icon: 6 });
        getListDataAjax();
    }, function(errorMsg) {
        alert("数据修改失败!");
    })
})