<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <base th:href="${#request.getContextPath() + '/'} ">
    <meta charset="utf-8">
    <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.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="assets/fonts/font-awesome/css/font-awesome.css"/>
    <link rel="stylesheet" href="assets/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="assets/layuiadmin/style/admin.css" media="all">
</head>
<body>
<div class="layui-anim layui-anim-fadein">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form layui-form-pane" lay-filter="dataForm">
                <input type="hidden" id="permissionType" name="permissionType" value="">
                <blockquote id="permissionTypeName" class="layui-elem-quote" style="display: none;"></blockquote>
                <div class="layui-form-item">
                    <textarea id="apiJSONObject" lay-verify="required" placeholder="请添加上述地址返回的JSON对象，SYSTEM 可根据需要替换为 APP、ROUTE 等" class="layui-textarea" required style="height: 308px"></textarea>
                </div>
                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0;">
                            <button type="button" class="layui-btn" lay-submit lay-filter="submitForm">提交新增</button>
                            <button type="button" class="layui-btn layui-btn-primary close">关闭</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="assets/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: 'assets/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'laydate', 'laytpl'], function(){
        var $ = layui.$;
        var form = layui.form;
        var laytpl = layui.laytpl;
        var laydate = layui.laydate;
        var permissionType = top.restAjax.params(window.location.href).permissionType;

        if(permissionType === 'permissionInsert') {
            $('#permissionTypeName').html('新增权限，访问系统的 <a href="system-api/SYSTEM/post" target="_blank"><b>/system-api/SYSTEM/post</b></a> 路径，可得到接口 JSON 对象');
        } else if(permissionType === 'permissionDelete') {
            $('#permissionTypeName').html('删除权限，访问系统的 <a href="system-api/SYSTEM/delete" target="_blank"><b>/system-api/SYSTEM/delete</b></a> 路径，可得到接口 JSON 对象');
        } else if(permissionType === 'permissionUpdate') {
            $('#permissionTypeName').html('修改权限，访问系统的 <a href="system-api/SYSTEM/put" target="_blank"><b>/system-api/SYSTEM/put</b></a> 路径，可得到接口 JSON 对象');
        } else if(permissionType === 'permissionQuery') {
            $('#permissionTypeName').html('查询权限，访问系统的 <a href="system-api/SYSTEM/get" target="_blank"><b>/system-api/SYSTEM/get</b></a> 路径、<a href="system-api/ROUTE/get" target="_blank"><b>/system-api/ROUTE/get</b></a>，可得到接口 JSON 对象');
        } else {
            $('#permissionTypeName').text('权限错误!');
        }
        $('#permissionTypeName').show();


        function closeBox() {
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        }

        // 提交表单
        form.on('submit(submitForm)', function(formData) {
            var apiJSONObject;
            try {
                apiJSONObject = JSON.parse($('#apiJSONObject').val());
            } catch (e) {
                top.dialog.msg('提交内容格式非标准 JSON');
                return;
            }
            if(typeof apiJSONObject != 'object' || !apiJSONObject) {
                top.dialog.msg('提交内容格式非标准 JSON');
                return;
            }
            top.dialog.confirm(top.dataMessage.commit, function(index) {
                top.dialog.close(index);
                var loadLayerIndex;
                top.restAjax.post(top.restAjax.path('api/permission/save-quick/{permissionType}', [permissionType]), apiJSONObject, null, function(code, data) {
                    var layerIndex = top.dialog.msg(top.dataMessage.commitSuccess, {
                        time: 0,
                        btn: [top.dataMessage.button.yes, top.dataMessage.button.no],
                        shade: 0.3,
                        yes: function(index) {
                            top.dialog.close(index);
                            window.location.reload();
                        },
                        btn2: function() {
                            closeBox();
                        }
                    });
                }, function(code, data) {
                    top.dialog.msg(data.msg);
                }, function() {
                    loadLayerIndex = top.dialog.msg(top.dataMessage.committing, {icon: 16, time: 0, shade: 0.3});
                }, function() {
                    top.dialog.close(loadLayerIndex);
                });
            });
            return false;
        });

        $('.close').on('click', function() {
            closeBox();
        });

        // 校验
        form.verify({
        });
    });
</script>
</body>
</html>