var pageNo = 1;
var totalPages = 0;

//列表渲染
getListDataAjax();

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

    ajaxRequest(DEVICE_CHECKCHANNEL_LIST, "POST", queryParam, function(result) {
        let data = result.RESULT.pageList;
        let items = '';
        if (result.RESULT.totalCount > 0) {
            data.forEach(function(item, key) {
                items += "<tr>" +
                    "<td class='status' data-user='" + JSON.stringify(item) + "' data-id=" + item.id + "></td>" +
                    "<td>" + item.xh + "</td>" +
                    "<td>" + item.passageway_name + "</td>" +
                    "<td>" + item.passageway_type_name + "</td>" +
                    "<td>" + item.dwtype_name + "</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("请求数据失败!");
    })
}

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

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

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

//重置表单
function resetForm() {
    pageNo = 1;
    $("#passageway_name").val("");
    $("#passageway_type_name").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);
                        })
                        // 下拉联动
                    $('.dwtype').on('change', function() {
                        let index = $(this).find('option:selected').val();
                        mingTest(index, 'passageway_type_add')
                    })
                    mingTest(1, 'passageway_type_add')
                }
            })
        }),
        //修改弹框信息 
        $('.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=passageway_name]').val(userInfo.passageway_name)
                $('.editDataOut input[name=device_code]').val(userInfo.device_code)
                $('.editDataOut input[name=company_code').val(userInfo.company_code)
                $('.editDataOut input[name=id').val(userInfo.id)
                $('.editDataOut select[name=dwtype]').val(userInfo.dwtype)
                $('.editDataOut select[name=passageway_type]').val(userInfo.passageway_type)

                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);
                        })
                        $('.dwtype').on('change', function() {
                            let dwtype = $(this).find('option:selected').val();
                            mingTest(dwtype, 'passageway_type_edit')
                        })
                        mingTest(userInfo.dwtype, 'passageway_type_edit', userInfo.passageway_type, userInfo.passageway_type)
                    }
                });
            }
        })

    //删除信息
    $('.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(DEVICE_CHECKCHANNEL_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)
        }
    })
})


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

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

    //验证数据是否为空
    let res = validParamIsEmpty(allParam, {
        "passageway_name": "请输入通道名称",
        "device_code": "请输入设备编号",
        "dwtype": "请选择设备类型",
        "passageway_type": "请选择通道类型",
        "company_code": "请选择所属单位",
    });

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

    //验证通过 请求ajax
    ajaxRequest(DEVICE_CHECKCHANNEL_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, {
        "passageway_name": "请输入通道名称",
        "device_code": "请输入设备编号",
        "dwtype": "请选择设备类型",
        "passageway_type": "请选择通道类型",
    });

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

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

function mingTest(dwTypeValue, elementId, passagewayTypeFirstIndex = 0) {
    if (dwTypeValue == 1) {
        $('#' + elementId + ' .dwtype').eq(dwTypeValue - 1).prop("selected", true); //
        $('#' + elementId + ' .passagewayTypeFirst').show();
        $('#' + elementId + ' .passagewayTypeSecond').hide();
        $('#' + elementId + ' .passagewayTypeThird').hide();
        $('#' + elementId + ' .passagewayTypeFirst').eq(passagewayTypeFirstIndex).prop("selected", true); //dwtype
    } else if (dwTypeValue == 2) {
        $('#' + elementId + ' .dwtype').eq(dwTypeValue - 1).prop("selected", true); //
        $('#' + elementId + ' .passagewayTypeFirst').hide();
        $('#' + elementId + ' .passagewayTypeSecond').show();
        $('#' + elementId + ' .passagewayTypeThird').hide();
        $('#' + elementId + ' .passagewayTypeSecond').eq(passagewayTypeFirstIndex - 1).prop("selected", true);
    } else if (dwTypeValue == 3) {
        $('#' + elementId + ' .dwtype').eq(dwTypeValue - 1).prop("selected", true); //
        $('#' + elementId + ' .passagewayTypeFirst').hide();
        $('#' + elementId + ' .passagewayTypeSecond').hide();
        $('#' + elementId + ' .passagewayTypeThird').show();
        $('#' + elementId + ' .passagewayTypeThird').eq(passagewayTypeFirstIndex - 1).prop("selected", true);
    }
}