<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <base id="ibase" 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/js/vendor/template/bootstrap.css" />
    <!-- 代码编辑器 -->
    <script src="assets/js/vendor/template/jquery.min.js"></script>
    <script src="assets/js/vendor/template/jstorage.min.js"></script>
    <script>
        var codetype="php";
        var unid="59396e99ae344";
    </script>
    <script src="assets/js/vendor/template/runcode.js"></script>
    <style type="text/css" media="screen">
        #editor {
        //position: absolute;
            width: 100%;
            height: 600px;
            float: left;
            font-size: 14px;
        }
    </style>
    <!-- 代码编辑器 -->
</head>
<body class="no-skin">
<div class="main-container" id="main-container">
    <!-- /section:basics/sidebar -->
    <div class="main-content">
        <div class="main-content-inner">
            <div class="page-content">
                <div class="row">
                    <div class="col-xs-12">
                        <table id="table_report" class="table table-striped table-bordered table-hover">
                            <tr>
                                <td colspan="10">
                                    <div class="starter-template">
                                        <div id="editor" class="ace_editor ace-monokai ace_dark">
                                            <textarea id="codeContent" class="ace_text-input" wrap="off" autocorrect="off" autocapitalize="off" spellcheck="false" style="opacity: 0; height: 17px; width: 8px; left: 45px; top: 0px;"></textarea>
                                        </div>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <a class="btn btn-big btn-primary" id="templateContentSaveEvent">保存模板内容</a>
                                    <a class="btn btn-big btn-danger" id="templateContentSaveAllEvent" style="margin-left: 30px;">重新生成该模板下全部文件【请先配置批量生成的接口地址】</a>
                                    <a class="btn btn-big btn-warm" id="messageAlert" style="margin-left: 30px;">60秒后自动保存</a>
                                    </br><span>${TEMPLATE} 模板引擎对象 . ${SYSTEM_URL} 系统地址 . ${SYSTEM_TITLE} 系统标题 . ${SYSTEM_PORT} 系统端口 . ${NGINX_URL} 外部服务器地址</span>
                                </td>
                            </tr>
                        </table>
                    </div>
                    <!-- /.col -->
                </div>
                <!-- /.row -->
            </div>
            <!-- /.page-content -->
        </div>
    </div>
    <!-- /.main-content -->
</div>
<!-- /.main-container -->
<!-- 页面底部js¨ -->
<script src="assets/js/vendor/template/ace.js" type="text/javascript" charset="utf-8"></script>
<!--提示框-->
<script src="assets/layuiadmin/layui/layui.js"></script>
<script type="text/javascript">

    layui.config({
        base: 'assets/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form' , 'laytpl', 'table'], function(){
        var $ = layui.$;
        var form = layui.form;
        var table = layui.table;
        var templateConfigId = top.restAjax.params(window.location.href).templateConfigId;
        var templateAllGenerate = top.restAjax.params(window.location.href).templateAllGenerate;
        var templateCode = top.restAjax.params(window.location.href).templateCode;

        $(window).resize(function() {
            var href = location.href;
            setTimeout("location.replace('"+ href + "')",100);
        })

        $(document).ready(function() {
            $("#editor").css("height", (window.innerHeight - 100)+"px");
        });

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

        var messageAlert = 60;
        var messageAlertInterval;
        function openInterval() {
           messageAlertInterval =  setInterval(function(){
                if(messageAlert == 0) {
                    messageAlert = 60;
                    var codeTxt  = getCodeTxt();
                    if(codeTxt.trim().length < 10) {
                        layer.msg("监测到疑似误操作无法保存");
                        return false;
                    }
                    var obj = {templateContent:codeTxt};
                    top.restAjax.put(top.restAjax.path('api/templateconfig/updatetemplateconfigcontent/{templateConfigId}', [templateConfigId]), obj, null, function(code, data) {
                        layer.msg("已自动保存");
                    }, function(code, data) {
                        top.dialog.msg(data.msg);
                    }, function() {
                    }, function() {
                    });
                }
                $("#messageAlert").html(messageAlert + "秒后自动保存")
                messageAlert--;
            }, 1000);
        }

        $(document).on('click','#messageAlert',function(){
            if(messageAlertInterval) {
                window.clearInterval(messageAlertInterval);
                messageAlertInterval = null;
            }else{
                openInterval();
            }
        });

        $(cmainFrame());
        openInterval();

        document.addEventListener('keydown', function(e){
            if (e.keyCode == 83 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)){
                e.preventDefault();
                var codeTxt  = getCodeTxt();
                if(codeTxt.trim().length < 10) {
                    layer.msg("监测到疑似误操作无法保存");
                    return false;
                }
                var obj = {templateContent:codeTxt};
                top.restAjax.put(top.restAjax.path('api/templateconfig/updatetemplateconfigcontent/{templateConfigId}', [templateConfigId]), obj, null, function(code, data) {
                    layer.msg("完成保存");
                }, function(code, data) {
                    top.dialog.msg(data.msg);
                }, function() {
                }, function() {
                });
            }
        });

        // 全部生成
        $(document).on('click','#templateContentSaveAllEvent',function(){
            if (!templateAllGenerate) {
                top.layer.msg("未配置批量生成接口地址");
                return;
            }
            var codeTxt  = getCodeTxt();
            console.log(codeTxt);
            top.dialog.confirm(top.dataMessage.commit, function(index) {
                top.dialog.close(index);
                var loadLayerIndex;
                console.log("批量生成接口：" + templateAllGenerate)
                top.restAjax.get(top.restAjax.path(templateAllGenerate, []), {}, null, function(code, data) {
                    console.log(data);
                    if(code == 200) {
                        top.layer.msg('重新生成该模板下全部页面执行成功！',{
                            icon: 1,
                            time: 20000,
                        });
                    }else{
                        top.layer.msg('重新生成该模板下全部页面执行失败！',{
                            icon: 2,
                            time: 20000,
                        });
                    }
                }, function(code, data) {
                    top.layer.msg("接口地址配置错误");
                }, function() {
                    loadLayerIndex = top.dialog.msg(top.dataMessage.committing, {icon: 16, time: 0, shade: 0.3});
                }, function() {
                    top.dialog.close(loadLayerIndex);
                });
            });
        });

        // 文本内容
        $(document).on('click','#templateContentSaveEvent',function(){
            var codeTxt  = getCodeTxt();
            if(codeTxt.trim().length < 10) {
                layer.msg("监测到疑似误操作无法保存");
                return false;
            }
            console.log(codeTxt);
            top.dialog.confirm(top.dataMessage.commit, function(index) {
                top.dialog.close(index);
                var loadLayerIndex;
                var obj = {templateContent:codeTxt};
                top.restAjax.put(top.restAjax.path('api/templateconfig/updatetemplateconfigcontent/{templateConfigId}', [templateConfigId]), obj, null, function(code, data) {
                    var layerIndex = top.dialog.msg(top.dataMessage.updateSuccess, {
                        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);
                });
            });
        });

        // 初始化内容
        function initData() {
            var loadLayerIndex;
            top.restAjax.get(top.restAjax.path('api/templateconfig/gettemplateconfigbyid/{templateConfigId}', [templateConfigId]), {}, null, function(code, data) {
                setCodeTxt(data.templateContent);
                form.val('dataForm', {templateContent:data.templateContent});
                form.render(null, 'dataForm');
            }, 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();


        //设置代码内容
        function setCodeTxt(value){
            if(typeof(editor) == "undefined"){
                $('#editorBox').val(value);
            }else{
                editor.setValue(value,-1);
            }
        }

        //获取代码内容
        function getCodeTxt(){
            if(typeof(editor) == "undefined"){
                return $('#editorBox').text();
            }else{
                return editor.getValue();
            }
        }

        if(ie_error()){
            $('#editor').hide();
        }else{
            $('#editorBox').hide();
            ace.require("ace/ext/language_tools");
            var editor = ace.edit("editor");
            editor.setOptions({
                enableBasicAutocompletion: true,
                enableSnippets: true,
                enableLiveAutocompletion: true
            });
            editor.setTheme("ace/theme/monokai");
            editor.getSession().setMode("ace/mode/php");
        }

        function cmainFrame(){
            var hmain = document.getElementById("editor");
            var bheight = document.documentElement.clientHeight;
            hmain .style.width = '100%';
            hmain .style.height = (bheight  - 100) + 'px';
        }

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

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