<!doctype html>
<html 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">
    <link rel="stylesheet" href="assets/js/vendor/codemirror/codemirror.css">
    <link rel="stylesheet" href="assets/js/vendor/codemirror/theme/dracula.css">
    <style>
        .CodeMirror {font-size : 13px;line-height: 150%;}
    </style>
</head>
<body>
<div class="layui-anim layui-anim-fadein">
    <div class="layui-card">
        <div class="layui-card-body" style="padding: 0px;">
            <form class="layui-form layui-form-pane" lay-filter="dataForm">
                <div class="layui-row">
                    <div id="codeBox" class="layui-col-md12 layui-col-xs12">
                        <textarea id="showPageCode" name="showPageCode" style="display: none"></textarea>
                    </div>
                    <div id="previewBox" class="layui-col-md4 layui-col-xs4" style="display: none;">
                        <iframe id="previewCode" frameborder="0" style="width: 100%;"></iframe>
                    </div>
                </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" id="previewBtn" class="layui-btn layui-btn-normal">预览</button>
                            <button type="button" class="layui-btn layui-btn-primary close">返回上级</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="assets/js/vendor/codemirror/codemirror.js"></script>
<script src="assets/js/vendor/codemirror/mode/css/css.js"></script>
<script src="assets/js/vendor/codemirror/mode/javascript/javascript.js"></script>
<script src="assets/js/vendor/codemirror/mode/htmlmixed/htmlmixed.js"></script>
<script src="assets/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: 'assets/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'laydate'], function(){
        var $ = layui.$;
        var win = $(window)
        var form = layui.form;
        var laytpl = layui.laytpl;
        var params = top.restAjax.params(window.location.href);
        var formId = params.formId;
        var formCode = params.formCode;
        var formVersion = params.formVersion;
        var previewTimeout;
        var isPreview = false;
        var codeMirrorConfig = {
            lineNumbers: true,
            theme: 'dracula',
            matchBrackets: true,
            indentUnit : 4,
            indentWithTabs: 4,
            flattenSpans: false,
            tabSize : 4,
            smartIndent: true,
            extraKeys: {
                'Shift-Tab': function (cm) {
                    if (cm.somethingSelected()) {
                        cm.indentSelection('subtract');
                    } else {
                        cm.indentLine(cm.getCursor().line, "subtract");
                    }
                    return;
                },
            }
        };

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

        function initPreview(code) {
            if(!isPreview) {
                return;
            }
            $('#previewCode').attr('srcdoc', code);
        }

        function initFormHtml(value) {
            var editor = CodeMirror.fromTextArea(document.getElementById('showPageCode'), codeMirrorConfig);
            editor.setValue(value);
            editor.setSize('100%', win.height() - 65);
            editor.on('change', function(self, changeValue) {
                $('#showPageCode').val(self.getValue());
                if(previewTimeout) {
                    clearTimeout(previewTimeout);
                }
                previewTimeout = setTimeout(function() {
                    initPreview(self.getValue());
                }, 1000);
            });
            $('#previewCode').height(win.height() - 65);
        }

        // 初始化
        function initData() {
            var loadLayerIndex;
            top.restAjax.get(top.restAjax.path('api/form/get-show-page-code/code/{formCode}/version/{formVersion}', [formCode, formVersion]), {}, null, function(code, data) {
                form.val('dataForm', {
                    showPageCode: data.showPageCode,
                });
                form.render(null, 'dataForm');

                initFormHtml(data.showPageCode);
            }, function(code, data) {
                top.dialog.msg(data.msg);
            }, function() {
                loadLayerIndex = top.dialog.msg(top.dataMessage.loading, {icon: 16, time: 0, shade: 0.3});
            }, function() {
                top.dialog.close(loadLayerIndex);
            });
        }
        initData();

        // 提交表单
        form.on('submit(submitForm)', function(formData) {
            top.dialog.confirm(top.dataMessage.commit, function(index) {
                top.dialog.close(index);
                var loadLayerIndex;
                top.restAjax.put(top.restAjax.path('api/form/update-show-page-code/{formId}', [formId]), formData.field, 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();
        });

        $('#previewBtn').on('click', function() {
            if(isPreview) {
                $('#codeBox').attr('class', 'layui-col-md12 layui-col-xs12');
                $('#previewBox').hide();
            } else {
                $('#codeBox').attr('class', 'layui-col-md8 layui-col-xs8');
                $('#previewBox').attr('class', 'layui-col-md4 layui-col-xs4');
                $('#previewBox').show();
            }
            isPreview = !isPreview;
            initPreview($('#showPageCode').val());
        });
    });
</script>
</body>
</html>