<!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="bigdataModuleContentSaveEvent">保存模板内容</a>
                                    <a class="btn btn-big btn-danger" id="bigdataTemplateContentBuiderEvent" style="margin-left: 30px;">重新生成包含该模块的所有模板的静态页</a>
                                </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 bigdataModuleId = top.restAjax.params(window.location.href).bigdataModuleId;

        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;
                }
                console.log(codeTxt);
                var loadLayerIndex;
                var obj = {bigdataModuleContent:codeTxt};
                top.restAjax.put(top.restAjax.path('api/bigdatamodule/updatebigdatamodulecontent/{bigdataModuleId}', [bigdataModuleId]), obj, null, function(code, data) {
                    top.dialog.msg('保存成功');
                }, 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);
                });
            }
        });

        // 文本内容
        $(document).on('click','#bigdataTemplateContentBuiderEvent',function(){
            top.dialog.confirm(top.dataMessage.commit, function(index) {
                top.dialog.close(index);
                var loadLayerIndex;
                top.restAjax.put(top.restAjax.path('api/bigdatamodule/buiderbigdatatemplateall/{bigdataModuleId}', [bigdataModuleId]), {}, 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) {
                    console.log(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);
                });
            });
        });

        $(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));
        }

        $(cmainFrame());

        // 文本内容
        $(document).on('click','#bigdataModuleContentSaveEvent',saveData);

        function saveData() {
            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 = {bigdataModuleContent:codeTxt};
                top.restAjax.put(top.restAjax.path('api/bigdatamodule/updatebigdatamodulecontent/{bigdataModuleId}', [bigdataModuleId]), obj, null, function(code, data) {
                    top.dialog.msg('保存成功');
                }, 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/bigdatamodule/getbigdatamodulebyid/{bigdataModuleId}', [bigdataModuleId]), {}, null, function(code, data) {
                setCodeTxt(data.bigdataModuleContent);
                form.val('dataForm', {bigdataModuleContent:data.bigdataModuleContent});
                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>