var selectCompanyId = "";

//部门详情弹框
$(document).on('click', 'a.view-detail', function() {

    let dataId = $(this).data('id'); //单位ID
    companyId = dataId;

    layerDepartmentIndex = layer.open({
        type: 1,
        title: false,
        closeBtn: 0,
        shadeClose: true,
        skin: 'yourclass',
        area: ['700px', '400px'],
        content: $(".queryCompanyList"),
        success: function() {
            getDepartmentDataAjax({ "companyId": dataId })
                //获取部门列表 ajax请求
            $('.clsBtn,.cancel').click(function() {
                layer.close(layerDepartmentIndex);
            })
        }
    })
})

let scrollPageNo = 2;
let scrollPageHasMoreData = true;

var winH = $('.scroll').height(); //页面可视区域高度  
var p = 0,
    t = 0; // 顶部和底部的距离
$(function() {

    //鼠标滚动事件   
    $('.scroll').scroll(function() {
        var pageH = $('.result').height(); //当前文档总高度
        var scrollT = $('.scroll').scrollTop(); //滚动条top的值   
        var bottomH = (pageH - winH - scrollT) / winH; // 当前所滚动位置到底部距离

        p = $(this).scrollTop(); //顶部距离
        let dataId = $('a.view-detail').data('id');

        if (t <= p) { // 判断是否下滚  
            if (bottomH < 0.01) {
                if (scrollPageHasMoreData) {
                    getDepartmentDataAjax({ "companyId": dataId }, scrollPageNo)
                    scrollPageNo++
                }
            }
        }
        setTimeout(function() { t = p; }, 2000); //延时2秒   
    });
})

function getDepartmentDataAjax(queryParam = {}, pageNo = 1) {

    queryParam.pageNo = pageNo;
    queryParam.pageSize = 20;
    var items2 = ''
    ajaxRequest(DEPARTMENT_LIST, "POST", queryParam, function(result) {

        let data = result.pageList;
        if (data.length == 0) {
            scrollPageHasMoreData = false;
            $('#dataList2').html("<br/><span style='display:block;text-align:center'>暂无数据...</span>");

        } else {
            data.forEach(function(item, key) {
                items2 += "<tr>" +
                    "<td   data-user='" + JSON.stringify(item) + "' data-id=" + item.departmentId + " class='status' style='width:24px!important'></td>" +
                    "<td>" + item.departmentName + "</td>" +
                    "<td>" + item.departmentMan + "</td>" +
                    "<td>" + item.departmentPhone + "</td>" +
                    "<td>" + item.creatMan + "</td>" +
                    "<td>" + getFormatDate(item.creatTime.time) + "</td>" +
                    "</tr>"
            })
            $('#dataList2').html(items2);
        }
    }, function(errorMsg) {
        alert("请求失败!");
    })
}

/* 部门新增ajax请求 */
$('#addDataDepart').click(function() {

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

    //验证数据是否为空
    let res = validParamIsEmpty(allParam, {
        "departmentName": "请填写部门名称",
        "departmentMan": "请填写部门负责人",
        // "departmentPhone": "请填写负责人电话",

    });
    if (res.code == -1) {
        alert(res.msg);
        return;
    }
    var departmentPhone = checkPhoneFormat(allParam.departmentPhone);
    if (!departmentPhone.status) {
        alert(departmentPhone.message)
        return
    }

    //验证通过 请求ajax

    //将当前公司的ID赋值给提交数据
    allParam.companyId = companyId;

    ajaxRequest(DEPARTMENT_ADD, "POST", allParam, function(result) {
        let data = result.data;
        layer.close(layerCreateIndex2);
        layer.msg('添加成功!', { icon: 6 });
        getDepartmentDataAjax({ "companyId": companyId })
    }, function(errorMsg) {
        alert("添加失败!");

    })
})

/* 部门修改发送请求 */
$('#editDataDepart').click(function() {
    //获取表单的值 并转换成对象
    let allParam = serializeArrayToObj($("#editDepartmentForm").serializeArray());

    //验证数据是否为空
    let res = validParamIsEmpty(allParam, {
        "departmentName": "请填写部门名称",
        "departmentMan": "请填写部门负责人",
        // "departmentPhone": "请填写负责人电话",
    });

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

    var departmentPhone = checkPhoneFormat(allParam.departmentPhone);
    if (!departmentPhone.status) {
        alert(departmentPhone.message)
        return
    }

    //验证通过 请求ajax
    ajaxRequest(DEPARTMENT_UPDATE, "POST", allParam, function(result) {
        let data = result.data;
        layer.close(layerUpdateIndex2);
        layer.msg('修改成功!', { icon: 6 });
        getDepartmentDataAjax({ "companyId": companyId })
    }, function(errorMsg) {
        alert("修改失败!");
    })
})

//部门删除发送请求
$('.delete2').click(function(event) {
    event.stopPropagation();
    if (!$('.queryCompanyList .pure-table tr').has('.checked').length) {
        //layer.msg('请选择一条需要修改的信息');
        layer.msg('请选择一条需要删除的信息!', { icon: 5 });
    } else {
        let departmentId = $('.queryCompanyList .pure-table tr').find('.checked').data('id');

        ajaxRequest(DEPARTMENT_DELETE, "POST", { "departmentId": departmentId }, function(result) {

            $(".queryCompanyList .pure-table tbody tr.selected").remove()

            let data = result.data;
            // layer.close(layerDepartmentIndex);
            layer.msg('删除部门成功!', { icon: 6 });
            getDepartmentDataAjax({ "companyId": companyId })
        }, function(errorMsg) {
            alert("部门删除失败!");
        })
    }
})

//部门新增弹框
$('.add2').click(function() {
        layerCreateIndex2 = layer.open({
            type: 1,
            title: false,
            closeBtn: 0,
            shadeClose: true,
            skin: 'yourclass',
            area: ['410px', '300px'],
            content: $(".addDepartmentDataOut"),
            success: function() {
                $('.clsBtn2,.cancel2').click(function() {
                    //alert(1)
                    layer.close(layerCreateIndex2);
                })
            }
        })
    })
    //部门修改弹框
$('.edit2').click(function() {

    if (!$('.queryCompanyList .pure-table tr').has('.checked').length) {
        layer.msg('请选择一条需要修改的信息!', { icon: 5 });

    } else {
        // let dataId = $('.pure-table tr').find('.checked').data('id');
        let userInfo = $('.queryCompanyList .pure-table tr').find('.checked').data('user');
        $('.editDepartmentDataOut input[name=departmentName]').val(userInfo.departmentName)
        $('.editDepartmentDataOut input[name=departmentMan]').val(userInfo.departmentMan)
        $('.editDepartmentDataOut input[name=departmentPhone').val(userInfo.departmentPhone)
        $('.editDepartmentDataOut input[name=companyId').val(userInfo.companyId)
        $('.editDepartmentDataOut input[name=departmentId').val(userInfo.departmentId)

        layerUpdateIndex2 = layer.open({
            type: 1,
            title: false,
            closeBtn: 0,
            shadeClose: true,
            skin: 'yourclass',
            area: ['410px', '300px'],
            content: $(".editDepartmentDataOut"),
            success: function() {
                $('.clsBtn2,.cancel2').click(function() {
                    layer.close(layerUpdateIndex2);
                })
            }
        });
    }
})