var pageNo = 1;
var totalPages = 0;

//设备类型列表
getListDataAjax();

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

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

    ajaxRequest(DEVICE_TYPE_LIST, "POST", queryParam, function(result) {
            let data = result.RESULT.pageList;

            let items = '';
            if (result.RESULT.totalCount > 0) {

                data.forEach(function(item, key) {
                    let transmissionModel = "";
                    if (item.transmission_model == 1) {
                        transmissionModel = "NB-IOT"
                    } else if (item.transmission_model == 2) {
                        transmissionModel = "Lora-IOT"
                    } else {
                        transmissionModel = "2G/4G/5G"
                    }
                    items += "<tr>" +
                        "<td class='status' data-user='" + JSON.stringify(item) + "' data-id=" + item.id + "></td>" +
                        "<td>" + item.xh + "</td>" +
                        "<td>" + item.type_name + "</td>" +
                        "<td>" + item.type_value + "</td>" +
                        "<td>" + transmissionModel + "</td>" +
                        "<td>" + item.business + "</td>" +
                        "<td>" + item.company_name + "</td>" +
                        "<td>" + item.add_people + "</td>" +
                        "<td>" + getFormatDate(item.add_time.time) + "</td>" +
                        "</tr>"
                })
            }
            $('#dataList').html(items);

            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("请求数据失败!");
        })
}

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

//拼接搜索条件
function getSearchParamObj() {
    let queryParam = {};
    let business = $('#business').val();
    let type_value = $('#type_value').val()
    queryParam.business = business;
    queryParam.type_value = type_value;
    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);
})

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

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

//单位下拉
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: $(".addDeviceTypeOut"),
                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');

                $('.editDeviceTypeOut input[name=type_name]').val(userInfo.type_name)
                $('.editDeviceTypeOut input[name=type_value]').val(userInfo.type_value)
                $('.editDeviceTypeOut select[name=transmission_model]').val(userInfo.transmission_model)
                $('.editDeviceTypeOut input[name=business').val(userInfo.business)
                $('.editDeviceTypeOut select[name=company_code]').val(userInfo.company_code)
                $('.editDeviceTypeOut input[name=id').val(userInfo.id)

                layerUpdateIndex = layer.open({
                    type: 1,
                    title: false,
                    closeBtn: 0,
                    shadeClose: true,
                    skin: 'yourclass',
                    area: ['410px', '400px'],
                    content: $(".editDeviceTypeOut"),
                    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_TYPE_DELETE, "POST", { "ID_LIST": [{ "id": dataId }] }, function(result) {
                $(".pure-table tbody tr.selected").remove()
                let data = result
                layer.close(layer.layerCreateIndex);
                layer.msg('删除成功!', { icon: 6 });
                getListDataAjax()

            }, function(errorMsg) {
                alert("用户删除失败!");
            }, 2)
        }
    })
})

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

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

    //验证数据是否为空
    let res = validParamIsEmpty(allParam, {
        "type_name": "请输入类型名称",
        "type_value": "请输入类型参数",
        "transmission_model": "请选择传输类型",
        "business": "请输入厂家名称",
        "company_code": "请选择所属单位",
    });

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

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

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

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

    //验证数据是否为空
    let res = validParamIsEmpty(allParam, {
        "type_name": "请输入类型名称",
        "type_value": "请输入类型参数",
        "transmission_model": "请选择传输方式",
        "business": "请输入厂家名称",
    });

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

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