<!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>
        <table class="layui-hide" id="table-data" lay-filter="table-data"></table>
    </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/query', function (data) {
                    if (data.code === 200) {
                        let dataMap = data.data
                        tree.render({
                            elem: '#test9'
                            ,data: dataMap
                            ,showLine: false  //是否开启连接线
                            ,onlyIconControl: true
                            ,edit: ['add', 'del'] //操作节点的图标
                            ,click: function(obj){
                                if(obj.data.companyId!=null){
                                    layer.open({
                                        title: '编辑信息',
                                        type: 2,
                                        shade: 0.2,
                                        maxmin: true,
                                        shadeClose: false,
                                        area: ['800px', '700px'],
                                        content: '/cloud/account/department/edit/' + obj.data.id,
                                        btn: ["确定", "取消"],
                                        yes: function(index, layero){
                                            let body = layer.getChildFrame('body',index);

                                            let departmentName = body.find("input[name='departmentName']").val();
                                            if (departmentName.length <= 0) {
                                                layer.msg('请输入部门名称！');
                                                return;
                                            }

                                            let dataSet = {
                                                "id" : obj.data.id,
                                                "departmentName" : departmentName
                                            }

                                            $.post("/cloud/account/department/editSave", dataSet, function (data) {
                                                if (data.code === 200) {
                                                    loadTree()
                                                    layer.msg(data.msg, {time: 3000, icon: 1});
                                                    layer.close(index);

                                                } else {
                                                    layer.msg(data.msg, {icon: 2});
                                                }
                                            });
                                        }
                                        ,btn2: function(index, layero){
                                            loadTree()
                                        }
                                        ,cancel: function(index, layero) {
                                            loadTree()
                                        },
                                    });
                                }

                            },
                            operate: function(obj){
                                let type = obj.type; // 操作类型：add、update、del
                                let data = obj.data; // 当前节点数据
                                if(type === 'add'){
                                    // 添加子部门
                                    layer.open({
                                        title: '添加信息',
                                        type: 2,
                                        shade: 0.2,
                                        maxmin: true,
                                        shadeClose: false,
                                        area: ['800px', '700px'],
                                        btn: ["确定", "取消"],
                                        content: '/cloud/account/department/add',
                                        yes: function(index, layero){
                                            let body = layer.getChildFrame('body',index);

                                            let departmentName = body.find("input[name='departmentName']").val();
                                            if (departmentName.length <= 0) {
                                                layer.msg('请输入部门名称！');
                                                return;
                                            }
                                            let dataSet = {
                                                "id":data.id,
                                                "companyId":data.companyId,
                                                "departmentName" : departmentName,

                                            }

                                            $.post("/cloud/account/department/addSave", dataSet, function (data) {
                                                if (data.code === 200) {
                                                    layer.open({
                                                        type: 0,
                                                        title: data.msg,
                                                        closeBtn: 0,
                                                        btn: ["继续添加", "关闭"],
                                                        content: '是否继续添加？',
                                                        btn1: function (index) {
                                                            layer.close(index)

                                                        },
                                                        btn2: function (index2) {
                                                            layer.close(index2);
                                                            layer.close(index);
                                                            loadTree()
                                                        }
                                                    });
                                                } else {
                                                    layer.msg(data.msg, {icon: 2});
                                                }
                                            });
                                        }
                                        ,btn2: function(index, layero){
                                            loadTree()
                                        }
                                        ,cancel: function(index, layero) {
                                            loadTree()
                                        },
                                    });
                                }else if(type === 'update'){
                                    // 编辑部门

                                }else if(type === 'del'){
                                    // 删除部门
                                    layer.confirm('确认要删除 <b>' + data.departmentName + ' </b>吗？', {
                                        title: false,
                                        btn: ['确定', '取消'],
                                        closeBtn: 0,        // 不显示右上角的关闭按钮
                                        // 按钮【确定】的回调
                                        btn1: function (index) {
                                            // 加载层
                                            let index_loading = layer.load(1, {
                                                shade: [0.2, '#fff'] //0.1透明度的白色背景
                                            });
                                            $.post("/cloud/account/department/delete", {id: data.id}, function (data2) {
                                                // 关闭加载层
                                                layer.close(index_loading)
                                                if (data2.code === 200) {
                                                    loadTree()
                                                    layer.msg(data2.msg, {time: 3000, icon: 1});
                                                } else {
                                                    layer.msg(data2.msg, {time: 3000, icon: 2});
                                                }
                                            });
                                        }
                                        ,cancel: function(index, layero) {
                                            loadTree()
                                        },
                                    });
                                }
                            }
                        });
                    }else {
                        layer.msg(data.msg, {offset: 't', icon: 2});
                    }
                });
            }
        })




    });

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