<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">

<head>
    <meta charset="UTF-8">
    <title>部门管理</title>
    <link rel="stylesheet" th:href="@{/lib/layui/css/layui.css}"/>
    <link rel="stylesheet" th:href="@{/css/common.css}">
    <link rel="stylesheet" th:href="@{/css/animate.min.css}">
    <link rel="stylesheet" th:href="@{/iconfont/iconfont.css}">
</head>

<style>
    html, body {
        height: 100%;
        margin: 0;
        padding: 0;
        font-size: 12px;
    }

    div {
        -moz-box-sizing: border-box; /*Firefox3.5+*/
        -webkit-box-sizing: border-box; /*Safari3.2+*/
        -o-box-sizing: border-box; /*Opera9.6*/
        -ms-box-sizing: border-box; /*IE8*/
        box-sizing: border-box; /*W3C标准(IE9+，Safari5.1+,Chrome10.0+,Opera10.6+都符合box-sizing的w3c标准语法)*/
    }

    .layui-table-view .layui-table {
        width: 100%
    }
</style>

<body class="animated fadeIn">

<div class="page-loading">
    <div class="rubik-loader"></div>
</div>

<div style="height: 100%">
    <div style="height: 100%">
        <div style="padding: 20px; background-color: #F2F2F2;height:calc(100% - 0px);">
            <div class="layui-row layui-col-space15" style="height: 100%;">
                <div class="layui-col-md3" style="height: 100%;">
                    <div class="layui-card" style="height: 100%;">
                        <div class="layui-card-header">部门树</div>
                        <div class="layui-card-body" id="toolbarDiv" style="height: calc(100% - 10px);">
                            <ul id="deptTree" class="dtree" data-id="0"
                                style="width: 100% !important;height: calc(100% - 49px);overflow-y: auto;"></ul>
                        </div>
                    </div>
                </div>
                <div class="layui-col-md9">
                    <div class="layui-card">
                        <div class="layui-card-header" id="card-header">部门列表</div>
                        <div class="layui-card-body">
                            <table class="layui-hide" id="dept-table"></table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!--<script type="text/html" id="toolbar">-->
    <!--<shiro:hasPermission name="dept:add">-->
        <!--<button type="button" class="layui-btn layui-btn-xs layui-btn-normal" lay-event="add">-->
            <!--<i class="zadmin-icon zadmin-icon-xinzeng zadmin-oper-area"></i>-->
            <!--新增-->
        <!--</button>-->
    <!--</shiro:hasPermission>-->
<!--</script>-->


<script type="text/html" id="column-toolbar">

    <shiro:hasPermission name="dept:update">
        <a lay-event="edit">
            <i class="zadmin-icon zadmin-icon-edit-square zadmin-oper-area zadmin-blue" title="编辑"></i>
        </a>
    </shiro:hasPermission>

    <shiro:hasPermission name="dept:delete">
        <a lay-event="del">
            <i class="zadmin-icon zadmin-icon-delete zadmin-oper-area zadmin-red" title="删除"></i>
        </a>
    </shiro:hasPermission>

    <shiro:lacksPermission name="dept:update, dept:delete">
        <i class="zadmin-icon zadmin-icon-wuquanxian zadmin-oper-area zadmin-red"></i>无权限
    </shiro:lacksPermission>

</script>

<script type="text/html" id="column-toolbar-sort">
    <i class="zadmin-icon zadmin-icon-shangyimian zadmin-oper-area zadmin-blue" lay-event="up" title="上移"></i>

    <i class="zadmin-icon zadmin-icon-xiayimian zadmin-oper-area zadmin-blue" lay-event="down" title="下移"></i>

    <i class="zadmin-icon zadmin-icon-zhidingmian zadmin-oper-area zadmin-blue" lay-event="top" title="置顶"></i>

    <i class="zadmin-icon zadmin-icon-zhidimian zadmin-oper-area zadmin-blue" lay-event="bottom" title="置底"></i>
</script>

<script th:src="@{/lib/jquery/jquery.min.js}"></script>
<script th:src="@{/lib/layui/layui.js}"></script>
<script th:src="@{/js/common.js}"></script>

<script>

    var parentDeptId = 0;

    // 获取有没有删除, 更新, 和新增的权限.
    var hasDeptDelete = false;
    var hasDeptUpdate = false;
    var hasDeptAdd = false;

    $.get('/security/hasPermission/depttree:update', function (data) {
        hasDeptUpdate = data.data;
    });
    $.get('/security/hasPermission/depttree:delete', function (data) {
        hasDeptDelete = data.data;
    });
    $.get('/security/hasPermission/depttree:add', function (data) {
        hasDeptAdd = data.data;
    });

    layui.config({
        base: '/lib/layui/extend/'
    }).use(['jquery', 'dtree', 'layer', 'table', 'element', 'tablePlug'], function () {
        var dtree = layui.dtree;
        var layer = layui.layer;
        var table = layui.table;
        var tablePlug = layui.tablePlug;

        tablePlug.smartReload.enable(true);

        var DTree = dtree.render({
            elem: "#deptTree",
            url: "/depttree/tree/root",
            dataStyle: "layuiStyle",
            initLevel: 5,   // 初始打开节点级别
            method: "GET",
            dot: false,     // 圆点是否显示
            toolbar: false,  // 右键工具栏
            menubar: true,  // 树上方工具栏, 展开、收缩、刷新、搜索等
            toolbarShow: [],
            toolbarScroll: "#toolbarDiv",
            toolbarExt: [
                {
                    toolbarId: "add",
                    icon: "dtree-icon-roundadd",
                    title: "添加子节点",
                    handler: function (node, $div) {
                        add(node.nodeId, node.level);
                    }
                },
                {
                    toolbarId: "edit",
                    icon: "dtree-icon-bianji",
                    title: "编辑",
                    handler: function (node, $div) {
                        edit(node.nodeId);
                    }
                },
                {
                    toolbarId: "remove",
                    icon: "dtree-icon-roundclose",
                    title: "删除",
                    handler: function (node, $div) {
                        del(node.nodeId);
                    }
                }
            ],
            response: {
                statusCode: 0,
                message: "msg",
                treeId: "id",
                parentId: "parentId",
                title: "name"
            },
            toolbarFun: {
                loadToolbarBefore: function (buttons, param, $div) {
                    if (param.level === "1") { // 如果是顶级节点, 则取消编辑和删除功能.
                        buttons.edit = "";
                        buttons.remove = "";
                    }
                    if (!hasDeptAdd) {
                        buttons.add = "";
                    }
                    if (!hasDeptDelete) {
                        buttons.remove = "";
                    }
                    if (!hasDeptUpdate) {
                        buttons.edit = "";
                    }
                    return buttons;
                }
            }
        });

        table.render({
            elem: '#dept-table'
            , url: '/depttree/list'
            , where: {
                parentId: 0
            }
            , cellMinWidth: 80
            , toolbar: '#toolbar'
            , smartReloadModel: true
            , cols: [
                [
                    {type: 'numbers', title: '序号', width: "5%"}
                    , {field: 'id', title: 'ID', hide: true}
                    , {field: 'name', title: '名称'}
//                        , {title: '排序', fixed: 'right', align: 'center', toolbar: '#column-toolbar-sort'}
//                    , {title: '操作', align: 'center', toolbar: '#column-toolbar'}
                ]
            ]
        });

        dtree.on("node('deptTree')", function (obj) {
            table.reload('dept-table', {
                where: {
                    parentId: obj.param.nodeId
                }
            });
            parentDeptId = obj.param.nodeId;
            $("#card-header").html("[" + obj.param.context + "]的子部门");
        });

        table.on('toolbar', function (obj) {
            var event = obj.event;
            if (event === 'add') {
                add(parentDeptId);
            }
        });

        table.on('tool', function (obj) {
            var data = obj.data;
            var swapId;
            var currentId;
            if (obj.event === 'edit') {
                edit(data.id);
            } else if (obj.event === 'del') {
                del(data.id);
            } else if (obj.event === "up") {
                swapId = $(obj.tr).prev().find("td[data-field='id'] div").html();
                currentId = data.id;
                if (typeof swapId == 'undefined') {
                    layer.msg("已是第一层");
                    return;
                }
                swapSort(currentId, swapId);
            } else if (obj.event === "down") {
                swapId = $(obj.tr).next().find("td[data-field='id'] div").html();
                currentId = data.id;
                if (typeof swapId == 'undefined') {
                    layer.msg("已是最后一层");
                    return;
                }
                swapSort(currentId, swapId);
            } else if (obj.event === "top") {
                swapId = $("tr[data-index='0']").first().parent().children().first().find("td[data-field='id'] div").html();
                currentId = data.id;
                swapSort(currentId, swapId);
            } else if (obj.event === "bottom") {
                swapId = $("tr[data-index='0']").first().parent().children().last().find("td[data-field='id'] div").html();
                currentId = data.id;
                swapSort(currentId, swapId);
            }
        });


        function add(parentDeptId, level) {
            layer.open({
                content: "/depttree?parentId=" + parentDeptId + "&a=" + 1,
                title: "添加子节点",
                area: ['40%', '85%'],
                type: 2,
                maxmin: true,
                shadeClose: true,
                end: function () {
                    refresh();
                }
            });
        }

        function swapSort(currentId, swapId) {
            $.post('/depttree/swap', {currentId: currentId, swapId: swapId}, function (data) {
                layer.msg("交换成功");
                handlerResult(data, function () {
                    refresh();
                });
            });
        }

        function del(id) {
            layer.confirm("你确定删除数据吗？如果存在下级节点则一并删除！", {icon: 3, title: '提示'},
                    function (index) {
                        $.post('/depttree/' + id, {_method: "DELETE"}, function (data) {
                            layer.close(index);
                            handlerResult(data, function () {
                                refresh();
                            });
                        });
                    }, function (index) {
                        layer.close(index);
                    }
            );
        }

        function refresh() {
            table.reload("dept-table");
            DTree.menubarMethod().refreshTree();
        }


        function edit(id) {
            layer.open({
                content: '/depttree/' + id,
                title: "编辑",
                area: ['40%', '85%'],
                type: 2,
                maxmin: true,
                shadeClose: true,
                end: function () {
                    refresh();
                }
            });
        }
    });
</script>
</body>

</html>