if ($('#gobackpage').val()) {
    var gobackpage = $('#gobackpage').val();
} else {
    var gobackpage = 1;
}
var uname = $('#gobackuname').val();

$.ajax({
    url: 'person_list',
    type: 'post',
    data: {
        uname: uname,
        page: gobackpage
    },
    success: function (data) {
        $('#datatable tbody').html('');
        $('#datatable tbody').html(data);
        if ($('#totalPage').val() > 0) {
            $('.nullData').hide();
            $('#page').parent().show();
        } else {
            $('.nullData').show();
            $('#page').parent().hide();
        }

        // 分页初始化
        layui.use('laypage', function () {
            var laypage = layui.laypage;
            laypage.render({
                elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
                , curr: gobackpage
                , count: $('#total_nums').val()
                , theme: '#2ca9f9'
                , layout: ['prev', 'page', 'next', 'skip']
                , jump: function (obj, first) {

                    if (!first) {
                        $.ajax({
                            url: 'person_list',
                            type: 'post',
                            data: {
                                uname: uname,
                                page: obj.curr,
                            },
                            success: function (data) {
                                $('#datatable tbody').html('');
                                $('#datatable tbody').html(data);
                                if ($('#totalPage').val() > 0) {
                                    $('.nullData').hide();
                                    $('#page').parent().show();
                                } else {
                                    $('.nullData').show();
                                    $('#page').parent().hide();
                                }
           
                            }
                        })
                    }
                }
            });
        });
    }
})

//用户筛选：
function sub_filter() {
    var uname = $('#uname').val();
    $.ajax({
        url: 'person_list',
        type: 'post',
        data: {
            uname: uname,
            page: 1
        },
        success: function (data) {
            $('#datatable tbody').html('');
            $('#datatable tbody').html(data);
            if ($('#total_nums').val() > 0) {
                $('.nullData').hide();
                $('#page').parent().show();
            } else {
                $('.nullData').show();
                $('#page').parent().hide();
            }
            var datas = {
                uname: uname,
            }
            initSearchPage($('#total_nums').val(), 'person_list', datas);
            $('#totalPage').val($('#total_nums').val())

        }
    })
}
//用户搜索分页
function initSearchPage(page, url, datas) {
    layui.use('laypage', function () {
        var laypage = layui.laypage;
        laypage.render({
            elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
            // , count: 30
            , count: page
            , theme: '#428BCA'
            , layout: ['prev', 'page', 'next', 'skip']
            , jump: function (obj, first) {
                datas.page = obj.curr;
                if (!first) {
                    $.ajax({
                        url: url,
                        type: 'post',
                        data: datas,
                        success: function (data) {
                            $('#datatable tbody').html('');
                            $('#datatable tbody').html(data);
                            if ($('#total_nums').val() > 0) {
                                $('.nullData').hide();
                                $('#page').parent().show();
                            } else {
                                $('.nullData').show();
                                $('#page').parent().hide();
                            }

                        }
                    })
                }
            }
        });
    });
}

function allotEquipPage(uname) {
    var currPage = $('#page .layui-laypage-curr em').eq(1).html();
    var f_name = $('#uname').val();
    window.location.href = 'area_create?uname=' + uname + '&currpage=' + currPage + '&f_name=' + f_name;
}

// 创建新用户
function addUser() {
    var currPage = $('#page .layui-laypage-curr em').eq(1).html();
    var f_name = $('#uname').val();
    window.location.href = 'create_user?currpage=' + currPage + '&f_name=' + f_name;
}

//重置密码
function resetPwd(userName) {
    layui.use(['layer', 'form'], function () {
        var form = layui.form;
        form.val("resetPwdBoxForm", {
            "uname": userName
        })

        $('#userName').val(userName);
        $('#req').val('resetpwd')
        var layer = layui.layer;
        //执行一个layer实例
        layer.open({
            title: '重置密码',
            type: 1,
            area: ['400px', '285px'],
            btn: ['确定', '取消'],
            content: $('#resetPwdBox'), //指定元素
            closeBtn: 2,
            yes: function (index, layero) {
                sendResetPwd();
            },
            btn2: function (index, layero) {
                closeResetPwd();
            },
            cancel: function (index, layero) {
                closeResetPwd();
            }
        });
    });
}
// 密码重置
function sendResetPwd() {
    var userpassWord = $('#userpassWord').val();
    var userRePassWord = $('#userRePassWord').val();
    if (userpassWord == "") {
        layer.tips('请输入密码', '#userpassWord');
        return false;
    } else if (userRePassWord != userpassWord) {
        layer.tips('两次密码输入不一致', '#userRePassWord');
        return false;
    }
    $.ajax({
        url: 'change_pwd',
        type: 'post',
        data: $("#resetPwdBoxForm").serialize(),
        success: function (data) {
            if (data) {
                layer.msg("密码重置成功！");
                layer.closeAll();
                closeResetPwd()
                // setTimeout(function () {
                //     location.reload();
                // }, 1000)
            }
            else { layer.msg("密码重置失败！！！"); }
        }
    })
}

function closeResetPwd() {
    $("#resetPwdBoxForm input").val('');
    $('#resetPwdBox').hide();
}

// 删除用户
function deluser(userName) {

    layui.use(['layer'], function () {
        var layer = layui.layer;
        //询问框
        layer.confirm('删除用户' + userName + ' ？', {
            btn: ['确定', '取消'] //按钮
        }, function () {
            $.ajax({
                url: 'delete_user',
                type: 'post',
                data: {
                    uname: userName
                },
                success: function (data) {
                    if (data) {
                        layer.msg('删除成功', { icon: 1 });
                        var uname = $('#uname').val();
                        $.ajax({
                            url: 'person_list',
                            type: 'post',
                            data: {
                                uname: uname,
                                page: $('.layui-laypage-curr em').eq(1).html()
                            },
                            success: function (data) {
                                $('#datatable tbody').html('');
                                $('#datatable tbody').html(data);
                                if ($('#total_nums').val() > 0) {
                                    $('.nullData').hide();
                                    $('#page').parent().show();
                                } else {
                                    $('.nullData').show();
                                    $('#page').parent().hide();
                                }
    
                            }
                        })
                    }
                    else { layer.msg("删除失败"); }
                }
            })
        }, function () {

        });
    });
}