<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>部门管理</title>
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <link rel="stylesheet" href="/lib/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
    <style>
        .site-dropdown-demo .layui-menu li:hover{background-color: #333;}
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <!-- 上方搜索栏 -->

        <!-- 表格 -->
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;"> <legend>企业架构</legend>
        </fieldset>
        <div id="test9" class="demo-tree demo-tree-box"></div>
    </div>
</div>


<!--引入JS文件-->
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="/js/jquery-1.10.2.min.js" charset="utf-8"></script>
<script>


    layui.use(['table','dropdown', 'util', 'layer','form','tree'], function () {
        let dropdown = layui.dropdown,
            util = layui.util,
            layer = layui.layer,
            $ = layui.jquery,
            form = layui.form,
            tree = layui.tree,
            table = layui.table;




        $(document).ready(function () {
            loadTree()
            function loadTree(){
                $.get('/cloud/account/department/query1', function (data) {
                    if (data.code === 200) {
                        let dataMap = data.data
                        tree.render({
                            elem: '#test9'
                            ,data: dataMap
                            ,showLine: false  //是否开启连接线
                            ,showCheckbox: true
                            ,id: 'deptTree'
                            ,oncheck: function(data) {
                                // 点击复选框时触发
                                let checkedData = tree.getChecked('deptTree');

                                // 递归查找所有 type 为 "user" 的节点（包括子节点中的）
                                function findAllUserNodes(nodes) {
                                    let userNodes = [];

                                    nodes.forEach(function(node) {
                                        // 如果当前节点是 user 类型，添加到结果
                                        if (node.type === 'user') {
                                            userNodes.push({
                                                id: node.id,
                                                title: node.title,
                                                path: node.path || '' // 如果有路径信息
                                            });
                                        }

                                        // 如果有子节点，递归查找
                                        if (node.children && node.children.length > 0) {
                                            var childUserNodes = findAllUserNodes(node.children);
                                            userNodes = userNodes.concat(childUserNodes);
                                        }
                                    });

                                    return userNodes;
                                }

                                // 查找所有 user 节点
                                let allUserNodes = findAllUserNodes(checkedData);
                                let userCount = allUserNodes.length;

                                if (userCount > 0) {
                                    let userIds = "";
                                    let names = "";
                                    allUserNodes.forEach(function(user, index) {
                                        names += (user.title + ",")
                                        userIds += (user.id + ",")
                                    });
                                    userIds = userIds.substring(0, userIds.lastIndexOf(','));
                                    names = names.substring(0, names.lastIndexOf(','));
                                    window.parent.parentData(names,userIds);
                                }

                            }
                        });

                    }else {
                        layer.msg(data.msg, {offset: 't', icon: 2});
                    }
                });



            }
        })
    });

</script>
</body>
</html>
