<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>新增</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/lib/layui/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form" style="margin-left: -40px; margin-right: 20px">
    <div class="layui-form-item">
        <label class="layui-form-label required">标识</label>
        <div class="layui-input-block">
            <input type="text" name="sign" lay-verify="required|letter" lay-reqtext="请输入角色标识" placeholder="请输入角色标识"
                   autocomplete="off" class="layui-input">
            <tip1>角色标识作为唯一标识，请输入全英文</tip1>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">名称</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required|name" lay-reqtext="请输入角色名称" placeholder="请输入角色名称"
                   autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">描述</label>
        <div class="layui-input-block">
            <textarea name="descript" class="layui-textarea" autocomplete="off"></textarea>
        </div>
    </div>

    <!-- 右侧悬浮按钮 -->
    <div class="right-bottom-btn">
        <button class="layui-btn" lay-submit lay-filter="saveBtn"><i
                class="layui-icon layui-icon-ok"></i>确认
        </button>
        <button class="layui-btn" lay-filter="cancleBtn"><i
                class="layui-icon layui-icon-close"></i>取消
        </button>
    </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/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 = /*[[@{/}]]*/'';
    layui.use(['form', 'layer'], function () {
        var form = layui.form,
            layer = layui.layer;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var index = layer.alert('确认提交吗？', function () {
                var loading = layer.load(0, {shade: 0.1}); //0代表加载的风格，支持0-2
                AjaxUtil.post({
                    url: AjaxUtil.ctx + "roleinfo/doAdd",
                    data: data.field,
                    success: function (res) {
                        if (res.code === 0) {
                            layer.close(index);
                            layer.close(loading);
                            Message.success(1000, res.message, function () {
                                parent.setUpdateFlag();
                                var iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);
                            });
                        } else {
                            layer.close(loading);
                            Message.error(res.message, 1000);
                        }
                    },
                    error: function (error) {
                    }
                });
            });
            return false;
        });

        form.verify({
            name: function (value, item) { //value：表单的值、item：表单的DOM对象
                if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                    return '角色名不能有特殊字符';
                }
                if (/(^\_)|(\__)|(\_+$)/.test(value)) {
                    return '角色名首尾不能出现下划线\'_\'';
                }
                if (/^\d+\d+\d$/.test(value)) {
                    return '角色名不能全为数字';
                }
            },
            letter: function (value, item) {
                if (!new RegExp("^[a-zA-Z]+$").test(value)) {
                    return '角色标识作为唯一标识，请输入全英文';
                }
            }
        });

        // 监听取消按钮
        $("button[lay-filter='cancleBtn']").click(function () {
            var iframeIndex = parent.layer.getFrameIndex(window.name);
            parent.layer.close(iframeIndex);
        });
    });
</script>
</body>
</html>