

$.ajax({
    url: 'person_limit',
    type: 'post',
    data: {
        uname: "",
        page: 1
    },
    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('form', function(){
            var form = layui.form;
            form.render(); //更新全部
        //各种基于事件的操作，下面会有进一步介绍
        });
        // 分页初始化
        layui.use('laypage', function () {
            var laypage = layui.laypage;
            laypage.render({
                elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
                , curr: 1
                , count: $('#total_nums').val()
                , theme: '#2ca9f9'
                , layout: ['prev', 'page', 'next', 'skip']
                , jump: function (obj, first) {

                    if (!first) {
                        $.ajax({
                            url: 'person_limit',
                            type: 'post',
                            data: {
                                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();
                                }
                                layui.use('form', function(){
                                    var form = layui.form;
                                    form.render(); //更新全部
                                //各种基于事件的操作，下面会有进一步介绍
                                });
                            }
                        })
                    }
                }
            });
        });
    }
})

//用户筛选：
function sub_filter() {
    var uname = $('#uname').val();
    $.ajax({
        url: 'person_limit',
        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_limit', datas);
            $('#totalPage').val($('#total_nums').val())
            layui.use('form', function(){
                var form = layui.form;
                form.render(); //更新全部
            //各种基于事件的操作，下面会有进一步介绍
            });
        }
    })
}
//用户搜索分页
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();
                            }
                            layui.use('form', function(){
                                var form = layui.form;
                                form.render(); //更新全部
                            //各种基于事件的操作，下面会有进一步介绍
                            });
                        }
                    })
                }
            }
        });
    });
}

function allotEquipPage(uname) {
    $('#hiduname').val(uname);
    $.ajax({
        url: 'limit',
        type: 'post',
        data: {
            uname: uname
        },
        // dataType: "json",
        success: function (data) {
            if (data) {
                var newNavList = eval('(' + data + ')');
                layui.use(['tree'], function () {
                    var tree = layui.tree;

                    var topicId = []
                    for (var i = 0; i < newNavList.length; i++) {
                        if (newNavList[i].children.length) {
                            var currChildren = newNavList[i].children;
                            for (var j = 0; j < currChildren.length; j++) {
                                topicId.push(currChildren[j].id);
                                // topicId.push(parseInt(currChildren[j].id));
                            }
                        } else {
                            topicId.push(newNavList[i].id);
                            // topicId.push(parseInt(newNavList[i].id));
                        }
                        if (i == newNavList.length - 1) {
                            //基本演示
                            tree.render({
                                elem: '#test1'
                                , data: navList
                                , showCheckbox: true  //是否显示复选框
                                , id: 'demoId1'
                                , isJump: true //是否允许点击节点时弹出新窗口跳转
                                , click: function (obj) {
                                    var data = obj.data;  //获取当前点击的节点数据
                                    // layer.msg('状态：' + obj.state + '<br>节点数据：' + JSON.stringify(data));
                                }
                            });
                            tree.setChecked('demoId1', topicId); //勾选指定节点
                            console.log(topicId)

                        }
                    }


                })
            } else {
                layui.use(['tree'], function () {
                    var tree = layui.tree;
                    tree.render({
                        elem: '#test1'
                        , data: navList
                        , showCheckbox: true  //是否显示复选框
                        , id: 'demoId1'
                        , isJump: true //是否允许点击节点时弹出新窗口跳转
                        , click: function (obj) {
                            var data = obj.data;  //获取当前点击的节点数据
                            // layer.msg('状态：' + obj.state + '<br>节点数据：' + JSON.stringify(data));
                        }
                    });
                })
            }
        },
        error: function (type) {
            console.log(type)
        }
    })
    // var currPage = $('#page .layui-laypage-curr em').eq(1).html();
    // var f_name = $('#uname').val();
    // window.location.href = 'limit?uname=' + uname + '&currpage=' + currPage + '&f_name=' + f_name;
    layer.open({
        type: 1,
        id:"limitPopLayer",
        title:"用户"+uname+" 权限分配",
        area: ['60%', '80%'],
        btn: ['确定', '取消'],
        yes: function(index, layero){
            getLimit()
        },
        btn2: function(index, layero){
        //按钮【按钮二】的回调
        
        //return false 开启该代码可禁止点击该按钮关闭
        },
        scrollbar: false,
        // skin: 'layui-layer-rim', //加上边框
        content: $('#limitPop')
    });
}
function getLimit() {
    layui.use(['tree'], function () {
        var tree = layui.tree;
        var checkData = tree.getChecked('demoId1');
        console.log(checkData)
        if (checkData.length) {
            $.ajax({
                url: 'limit_view',
                type: 'post',
                data: {
                    uname: $('#uname').val(),
                    limit: JSON.stringify(checkData)
                },
                success: function (data) {
                    if (data) {
                        layui.use(['layer'], function () {
                            var layer = layui.layer;
                            layer.msg('设置成功！', { icon: 1, time: 500 }, function () {
                                layer.close(layer.index);
                            })
                        })
                    } else {
                        layui.use(['layer'], function () {
                            var layer = layui.layer;
                            layer.msg('设置失败！', { icon: 2 })
                        })
                    }
                },
                error: function (type) {
                    console.log(type)
                }
            })
        } else {
            layui.use(['layer'], function () {
                var layer = layui.layer;
                layer.msg('必须选择一项', { icon: 2 })
            })
        }

    })
}