<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
>
<head>
    <meta charset="UTF-8">
    <title>角色编辑</title>
    <link rel="stylesheet" href="/css/metroStyle/metroStyle.css">
    <link rel="stylesheet" href="/plugins/layui/css/layui.css">
    <link rel="stylesheet" href="/css/public.css">
</head>
<body class="child-body">
<div class="child-nav">
    <span class="layui-breadcrumb">
         <a>系统管理</a>
         <a href="/admin/role/list">角色列表</a>
         <a><cite th:text="${#strings.equals(role.id,null)?'新增角色':'修改角色'}"></cite></a>
    </span>
    <a class="layui-btn layui-btn-sm" href="javascript:location.replace(location.href);" title="刷新"
       style="float:right;margin-top: 3px"><i class="layui-icon" style="line-height:30px">&#x1002;</i></a>
</div>
<div class="layui-col-md12">
    <div class="layui-card">
        <div class="layui-card-header" th:text="${#strings.equals(role.id,null)?'新增角色':'修改角色'}"></div>
        <div class="layui-card-body">
            <form class="layui-form" action="/admin/role/save" method="post">
                <input type="hidden" id="roleId" name="id" th:value="${role.id}">
                <input type="hidden" id="resourceIds" name="resourceIds">
                <div class="layui-form-item">
                    <label class="layui-form-label">角色名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="roleName" th:value="${role.roleName}" lay-verify="required"
                               lay-vertype="tips" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">标识符</label>
                    <div class="layui-input-inline">
                        <input type="text" name="roleKey" th:value="${role.roleKey}" lay-verify="required"
                               lay-vertype="tips" class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux">唯一标识 如：administrator</div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">角色描述</label>
                    <div class="layui-input-inline">
                        <input type="text" name="roleDesc" th:value="${role.roleDesc}" lay-verify="pass"
                               lay-vertype="tips" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">资源分配</label>
                    <div class="layui-input-inline">
                        <input type="text" id="resourceGrant" autocomplete="off" class="layui-input"
                               th:value="${checkNodesName}" readonly>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="save"
                                th:text="${#strings.equals(role.id,null)?'保存':'修改'}"></button>
                        <a href="javascript:history.back()" type="button" class="layui-btn layui-btn-primary">返回</a>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<div class="row layui-hide" id="tree">
    <div class="row" style="padding-left: 60px">
        <div class="zTreeDemoBackground left">
            <ul id="menuTree" class="ztree"></ul>
        </div>
    </div>
</div>
<script src="/js/jquery.min.js"></script>
<script src="/js/jquery.ztree.all.min.js"></script>
<script src="/plugins/layui/layui.js"></script>
<script type="text/javascript">
    var setting = {
        view: {
            selectedMulti: false
        },
        check: {
            enable: true,
            chkboxType: {"Y": "p", "N": "p"}
        },
    };


    var treeObj;
    layui.use(['element', 'form', 'layer'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        $.ajax({
            url: '/admin/resource/getByRoleId?roleId=' + $('#roleId').val(),
            dataType: 'json',
            success: function (data) {
                $.fn.zTree.init($("#menuTree"), setting, data);
                treeObj = $.fn.zTree.getZTreeObj("menuTree");
                treeObj.expandAll(true);
            }
        });

        //监听提交
        form.on('submit(save)', function (data) {
            var headers = {};
            headers['X-CSRF-TOKEN'] = "[[${_csrf.token}]]";
            $.ajax({
                url: data.form.action,
                type: data.form.method,
                headers: headers,
                data: data.field,
                dataType: 'json',
                success: function (data) {
                    if (data.code == 0) {
                        layer.msg(data.msg, {icon: 1, time: 600}, function () {
                            location.href = "/admin/role/list";
                        });
                    } else {
                        layer.msg(data.msg, {time: 1000});
                    }
                }
            });
            return false;
        });

        $("#resourceGrant").click(function () {
            $('#tree').removeClass('layui-hide');
            var treeOpen = layer.open({
                type: 1,
                skin: 'layui-layer-molv',
                area: ['400px', '500px'],
                title: ['角色授权'],
                shade: 0.5,
                maxmin: true,
                content: $('#tree'),
                btn: ['确定', '关闭'],
                yes: function (index) {
                    $('#tree').addClass('layui-hide');

                    var nodes = treeObj.getCheckedNodes(true);
                    var checkNodes = new Array();
                    var checkNodesName = new Array();
                    for (var i = 0; i < nodes.length; i++) {
                        checkNodes.push(nodes[i].id);
                        checkNodesName.push(nodes[i].name);
                    }
                    $('#resourceIds').val(checkNodes.join(","));
                    $('#resourceGrant').val(checkNodesName.join("，"))
                    layer.close(treeOpen);
                },
                btn2: function (index) {
                    $('#tree').addClass('layui-hide');
                    layer.close(treeOpen);
                },
                cancel: function(){
                    $('#tree').addClass('layui-hide');
                    layer.close(treeOpen);
                }
            });
        });

    });
</script>
</body>
</html>