<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <title>SQL编辑器-{{ data.name }}</title>
    <link rel="stylesheet" href="/jd_report/static/plugins/font-awesome/css/font-awesome.css">
    <script src="/web/static/lib/jquery/jquery.js" type="text/javascript"></script>
    <link rel="stylesheet" href="/jd_report/static/src/css/index.css">
    <link rel="stylesheet" href="/jd_report/static/plugins/codemirror/codemirror.css">
    <link rel="stylesheet" href="/jd_report/static/plugins/codemirror/addon/hint/show-hint.css">
    <link rel="stylesheet" href="/jd_report/static/plugins/codemirror/theme/dracula.css"/>
    <script src="/jd_report/static/plugins/codemirror/codemirror.js" type="text/javascript"></script>
    <script src="/jd_report/static/plugins/codemirror/sql/sql.js"></script>
    <script src="/jd_report/static/plugins/codemirror/addon/hint/show-hint.js"></script>
    <script src="/jd_report/static/plugins/codemirror/addon/hint/sql-hint.js"></script>
    <style>
        .CodeMirror {
            border: 1px solid #eee;
            height: auto;
            min-height: 400px;
        }

        .CodeMirror-scroll {
            height: auto;
            overflow-y: hidden;
            overflow-x: auto;
        }

    </style>
</head>
<body style="width: 98%;height: 98%;">
<input id="sql-id" value="{{ data.id }}" type="hidden"/>
<div id="vm">
    <div class="head" style="height: 70px !important;">
        <div class="tool" style="padding: 0px !important;">
            <div id="begin" class="tool-box" style="display: flex;">
                <div class="tool-item file">
                    <div class="left"><i class="fa fa-folder-open"></i>
                        <div class="icon"><span>文件</span> <i class="fa fa-caret-down"></i></div>
                        <div hidden="hidden" class="tool-list" style="display: none;">
                            <li data-family="SimSun" class="tool-list-item">新建</li>
                            <li data-family="SimSun" class="tool-list-item">
                                本地打开
                                <input id="openFile" type="file" accept="application/vnd.ms-excel" hidden="hidden"></li>
                            <li data-family="SimSun" class="tool-list-item">本地保存</li>
                        </div>
                    </div>
                    <div class="right">
                        <div class="common save-file-online"><i aria-hidden="true" class="fa fa-file-text-o"></i>
                            打开
                        </div>
                        <div class="common save-file-online" onclick="onSave();"><i aria-hidden="true"
                                                                                    class="fa fa-floppy-o"></i>
                            保存
                        </div>
                    </div>
                </div>
                <div class="tool-item preview">
                    <div class="con"><i aria-hidden="true" class="fa fa-eye"></i>
                        <div class="text">
                            预览
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </div>

    <div id="content" class="content" style="height: 100% !important; overflow: scroll;">
        <textarea id="sql-code">{{ data.sql }}</textarea>
    </div>

</div>
<script>
    var textArea = document.getElementById("sql-code");
    var editor = CodeMirror.fromTextArea(textArea, {
        mode: 'text/x-pgsql',
        indentWithTabs: true,
        theme: "dracula",
        smartIndent: true,
        lineNumbers: true,
        matchBrackets: true,
        autofocus: true,
        extraKeys: {"Ctrl-Space": "autocomplete"},
        hintOptions: {
            tables: {
                users: ["name", "score", "birthDate"],
                countries: ["name", "population", "size"]
            }
        }
    });

    function onSave() {
        var sql = editor.getValue();
        $.post("/v1/jd_report/sql/save", {sql: sql, id: $("#sql-id").val()}, function (result) {
            console.log(result);
        });
    }
</script>
</body>
</html>