<!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/resource/list">资源列表</a>
         <a><cite th:text="${#strings.equals(resource.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(resource.id,null)?'新增资源':'修改资源'}"></div>
        <div class="layui-card-body">
            <form class="layui-form" action="/admin/resource/save" method="post">
                <input type="hidden" id="resourceId" name="id" th:value="${resource.id}">
                <div class="layui-form-item">
                    <label class="layui-form-label">资源名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="name" th:value="${resource.name}" 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="resourceKey" th:value="${resource.resourceKey}" lay-verify="required"
                               lay-vertype="tips" class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux">唯一标识 如：admin:user:list</div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">父级资源</label>
                    <div class="layui-input-inline">
                        <select th:if="${resource.id==null}" name="parentResourceId" id='parentMenu'></select>
                        <input th:if="${resource.id!=null}" type="text" class="layui-input" th:value="${parentName}"
                               readonly>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">资源类型</label>
                    <div class="layui-input-inline">
                        <select name="resourceType" lay-verify="required">
                            <option value="menu">菜单</option>
                            <option value="button" th:selected="${#strings.equals(resource.resourceType,'button')}">按钮
                            </option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">路径</label>
                    <div class="layui-input-inline">
                        <input type="text" name="url" th:value="${resource.url}" lay-verify="required"
                               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="number" name="sort" th:value="${resource.sort}" lay-verify="sort"
                               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" name="icon" class="layui-input" th:value="${resource.icon}"
                               autocomplete="off">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">描述</label>
                    <div class="layui-input-inline">
                        <textarea name="description" placeholder="请输入菜单描述" class="layui-textarea" maxlength="255"
                                  th:field="${resource.description}"></textarea>
                    </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(resource.id,null)?'保存':'修改'}"></button>
                        <a href="javascript:history.back()" type="button" class="layui-btn layui-btn-primary">返回</a>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="/plugins/layui/layui.js"></script>
<script type="text/javascript">
    layui.use(['element', 'form', 'laydate', 'upload'], function () {
        var form = layui.form, $ = layui.jquery;

        form.verify({
            sort: [
                /^(?:0|[1-9][0-9]*)$/,
                '排序号必须是大于等于0的整数'
            ],
        });

        //监听提交
        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: 1000}, function () {
                            location.href = "/admin/resource/list";
                        });
                    } else {
                        layer.msg(data.msg, {time: 1000});
                    }
                }
            });
            return false;
        });

        $("#parentMenu").append(window.localStorage.getItem('menuOption'));
        form.render();
    });
</script>
</body>
</html>