<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>部门列表</title>
    <link rel="stylesheet" th:href="@{/lib/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
    <link rel="stylesheet" th:href="@{/lib/font-awesome-4.7.0/css/font-awesome.min.css}" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm data-add-btn" lay-event="add">
                    <i class="layui-icon layui-icon-addition" style="font-size: 10px;"></i>新增
                </button>
                <button class="layui-btn layui-btn-sm data-add-btn" lay-event="expand">
                    <i class="layui-icon layui-icon-down" style="font-size: 10px;"></i>展开
                </button>
                <button class="layui-btn layui-btn-sm data-add-btn" lay-event="fold">
                    <i class="layui-icon layui-icon-up" style="font-size: 10px;"></i>折叠
                </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-update" lay-event="update">
                <i class="layui-icon layui-icon-edit" style="font-size: 10px;"></i>编辑
            </a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-view" lay-event="view">
                <i class="layui-icon layui-icon-file" style="font-size: 10px;"></i>查看
            </a>
            <a class="layui-btn layui-btn-danger layui-btn-xs data-count-delete" lay-event="delFlag">
                <i class="layui-icon layui-icon-delete" style="font-size: 10px;"></i>删除
            </a>
        </script>
    </div>
</div>
<script th:src="@{/lib/jquery-3.4.1/jquery-3.4.1.min.js}" charset="utf-8"></script>
<script th:src="@{/lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>
<script th:src="@{/lib/coco-message/coco-message.js}" charset="utf-8"></script>
<script th:src="@{/js/lay-config.js}" charset="utf-8"></script>
<script type="text/javascript" th:inline="javascript">
    AjaxUtil.ctx = /*[[@{/}]]*/'';
    var updateflag = 0;
    layui.use(['form', 'table', 'treeTable'], function () {
        var form = layui.form,
            treeTable = layui.treeTable;
        var isShow = true;
        var deptId = 0;

        var insTb = treeTable.render({
            elem: '#currentTableId',
            url: AjaxUtil.ctx + 'deptinfo/list',
            skin: 'grid',
            tree: {
                treeColIndex: 1,
                treeSpid: "1",
                iconIndex: 1,           // 折叠图标显示在第几列
                isPidData: true,        // 是否是id、pid形式数据
                idName: 'deptId',  // id字段名称
                pidName: 'parentId', // pid字段名称
                arrowType: 'arrow2',
                getIcon: 'ew-tree-icon-style2'
            },
            treeDefaultClose: isShow,
            method: 'GET',
            page: false,
            toolbar: '#toolbarDemo',
            cols: [
                [
                    {type: 'numbers', fixed: 'left'},
                    {field: 'deptName', width: '22%', title: '部门名称'},
                    {field: 'leader', title: '负责人', width: '8%'},
                    {field: 'phone', title: '联系电话', width: '10%'},
                    {field: 'email', title: '邮箱', width: '12%'},
                    {field: 'orderNum', title: '排序', width: '4%', align: 'center', edit: 'number'},
                    {
                        field: 'status', title: '状态', align: 'center', width: '6%', templet: function (d) {
                            if (0 == d.status) {
                                return '<span class="layui-badge layui-bg-blue">正常</span>';
                            } else {
                                return '<span class="layui-badge layui-bg-orange">停用</span>';
                            }
                        }
                    },
                    {field: 'cjrxm', width: '8%', title: '创建人'},
                    {field: 'createTime', width: '8%', title: '创建时间', sort: true, align: 'center'},
                    {title: '操作', width: '18%', toolbar: '#currentTableBar', align: "center"}
                ]
            ]
        });

        // 监听单元格编辑
        treeTable.on('edit(currentTableId)', function (obj) {
            var value = obj.value
                , data = obj.data;
            if (!/^\+?[0-9][0-9]*$/.test(value)) {
                Message.warning('请输入数字，且大于0!', 1000);
                return false;
            }
            var index = layer.load(2, {shade: 0.1});
            AjaxUtil.get({
                url: AjaxUtil.ctx + 'deptinfo/updateSort/' + data.deptId + '/' + value,
                success: function (res) {
                    layer.close(index);
                    if (0 === res.code) {
                        layer.closeAll('dialog');
                        Message.success(500, res.message, function () {
                            // 重载表格
                            insTb.refresh();
                        });
                    } else {
                        Message.error(res.message, 1000);
                    }
                },
                error: function (error) {
                    layer.close(index);
                    Message.error('服务器异常！', 1000);
                }
            });
        });

        treeTable.on('toolbar(currentTableId)', function (obj) {
            if (obj.event === 'add') {
                var index = layer.open({
                    title: '新增部门',
                    type: 2,
                    maxmin: true,
                    shade: 0.2,
                    area: [layerwidth + 'px', layerheight + 'px'],
                    content: AjaxUtil.ctx + 'deptinfo/forAdd/' + deptId,
                    end: function (index) {
                        if (updateflag == 1) {
                            insTb.refresh();
                            insTb.expandAll();// 展开全部
                        }
                        updateflag = 0;
                    }
                });
            } else if (obj.event === 'expand') {
                isShow = true;
                insTb.expandAll();// 展开全部
            } else if (obj.event === 'fold') {
                isShow = false;
                insTb.foldAll();// 折叠全部
            }
        });

        treeTable.on('tool(currentTableId)', function (obj) {
            var data = obj.data;
            if (obj.event === 'update') {
                layer.open({
                    type: 2,
                    shade: 0.2,
                    title: '修改部门',
                    maxmin: true,
                    area: [layerwidth + 'px', layerheight + 'px'],
                    content: AjaxUtil.ctx + 'deptinfo/forUpdate/' + data.deptId,
                    end: function (index) {
                        if (updateflag == 1) {
                            insTb.refresh();
                            insTb.expandAll();// 展开全部
                        }
                        updateflag = 0;
                    }
                });
                return false;
            } else if (obj.event === 'view') {
                var index = layer.open({
                    type: 2,
                    shade: 0.2,
                    title: '查看部门',
                    maxmin: true,
                    area: [layerwidth + 'px', layerheight + 'px'],
                    content: AjaxUtil.ctx + 'deptinfo/forView/' + data.deptId,
                    end: function () {
                        layer.close(index);
                    }
                });
                return false;
            } else if (obj.event === 'delFlag') {
                if (data.children != undefined) {
                    Message.error('该节点下存在子节点，不能直接删除', 2000);
                } else {
                    var index = layer.load(0, {shade: 0.1});
                    AjaxUtil.get({
                        url: AjaxUtil.ctx + 'deptinfo/doDelete/' + data.deptId,
                        success: function (res) {
                            layer.close(index);
                            if (0 === res.code) {
                                // layer.closeAll('dialog');
                                Message.success(500, res.message, function () {
                                    // 重载表格
                                    insTb.refresh();
                                    insTb.expandAll();// 展开全部
                                });
                            } else {
                                Message.error(res.message, 1000);
                            }
                        },
                        fail: function (res) {
                            layer.close(index);
                            Message.error(res.message, 1000);
                        },
                        error: function (error) {
                            layer.close(index);
                            Message.error("服务器异常！", 1000);
                        }
                    });
                    return false;
                }
            }
        });

        //监听行选中
        treeTable.on('row(currentTableId)', function (obj) {
            deptId = obj.data.deptId;
            obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
        });
    });

    function setUpdateFlag() {
        updateflag = 1;
    }
</script>
</body>
</html>