{% extends wide_base %}

{% block head %}

{% include common/script/load_codemirror.html %}

{% if path.endswith(".py") %}
    <script type="text/javascript" src="{{_server_home}}/_static/lib/codemirror/mode/python.js"></script>
{% end %}
{% if path.endswith(".js") %}
    <script type="text/javascript" src="{{_server_home}}/_static/lib/codemirror/mode/javascript.js"></script>
{% end %}
{% if path.endswith(".php") %}
    <script type="text/javascript" src="{{_server_home}}/_static/lib/codemirror/mode/php.js"></script>
{% end %}
{% if path.endswith(".css") %}
    <script type="text/javascript" src="{{_server_home}}/_static/lib/codemirror/mode/css.min.js"></script>
{% end %}

<style type="text/css">
    .search-key {
        background-color: #FF8000;
        color: #000;
    }

    #result {
        border: 1px solid #ccc;
        padding: 4px;
        background-color: #eee;
    }

    #editorArea {
        border: 1px solid #ccc;
    }

    #editor {
        width: 100%;
        height: 100%;
    }

    .CodeMirror-search-match {
        background: gold;
        border-top: 1px solid orange;
        border-bottom: 1px solid orange;
        -moz-box-sizing: border-box;
        box-sizing: border-box;
        opacity: .5;
    }

    .CodeMirror {
        font-family: Fira Code,Menlo,Monaco,Consolas,Liberation Mono,Courier New,Courier,monospace;
    }

    .global-warn {
        display: none;
    }
</style>
{% end %}

{% block body_left %}
{% import os %}
{% init readonly = False %}
{% init warn = "" %}
{% init error = "" %}
{% init pathlist = [] %}
{% init show_preview = False %}
{% init embed = False %}
{% init file_too_large = False %}
{% init post_action = "/code/update" %}
{% init show_fs_path = True %}
{% init title = "文本编辑" %}
{% init parent_link = None %}
{% init show_rename = True %}
{% init help_text = "" %}

<div class="card editor-title">
    <h3 class="card-title btn-line-height">
        {% if parent_link %}
            <a class="link2 path-link" href="{{parent_link.href}}">{{parent_link.text}}</a>
            <span>/</span>
        {% end %}

        <span>{{title}}</span>

        <div class="float-right">
            {% if show_preview %}
                <a class="btn" href="{{_server_home}}/code/wiki/{{path}}?embed={{embed}}">{{T("Preview")}}</a>
            {% end %}
            <a class="btn btn-default" href="javascript:history.back();">返回</a>
        </div>
    </h3>
</div>

<div class="card">

    {% if show_fs_path %}
        {% include "mod_fs_path.html" %}
    {% end %}

    {% if error != "" %}
        <div class="col-md-12 error">
            {{?error}}
        </div>
    {% end %}

    {% if warn != "" and warn != None %}
        <pre class="col-md-12 warn">
            {{warn}}
        </pre>
    {% end %}

    {% if help_text %}
        <pre class="row info"><a onclick="xnote.tmp.showHelp(this)" data-text="{{help_text}}">查看帮助</a></pre>
    {% end %}

    {% if file_too_large %}
    <div class="row warn">
        <span>文件过大，只显示部分内容</span>
        {% for link in part_links %}
            {% render link %}
        {% end %}
    </div>
    {% end %}

    <form method="POST" action="{{post_action}}">
        <input name="path" class="hide" value="{{path}}"/>
        <input name="basename" class="hide" value="{{os.path.basename(path)}}"/>
        <input name="dirname" class="hide" value="{{os.path.dirname(path)}}"/>
        <div id="editorArea" class="col-md-12">
            <textarea name="content" id="editor">{{content}}</textarea>
        </div>

        <div class="row top-offset-1">
            {% if not readonly %}
                <input type="button" value="保存" class="btn save-btn">
            {% end %}

            {% if not readonly and show_rename %}
                <input type="button" id="rename" class="btn rename-btn" value="重命名"/>
            {% end %}

            <input id="execute" type="button" class="btn hide" value="执行"/>
            {% if show_preview %}
                <a class="btn" href="{{_server_home}}/code/wiki/{{path}}?embed={{embed}}">{{T("Preview")}}</a>
            {% end %}
            {% if path.find("/plugins/") >= 0 or path.find("\\plugins\\") >= 0 %}
                <input type="button" class="btn link-btn" href="{{_server_home}}/plugin/{{plugin_name}}?embed={{embed}}&_reload=true" value="预览"/>
            {% end %}
            <span>注意不会自动保存</span>
        </div>

        <div id="resultDiv" class="col-md-12 hide">
            <div class="output-title">结果</div>
            <pre id="result" class="col-md-12 output-body">
            </pre>
        </div>
    </form>
</div>

{# TODO 需要处理下搜索的高亮 #}

<script type="text/javascript">
    if (xnote.getUrlParam("embed") == "true") {
        $("body").css("overflow-y", "hidden");
        $(".editor-title").hide();
    }


    $(function () {
        function getParamPath() {
            var path = xnote.getUrlParam("path");
            if (path) {
                return path;
            }
            return $("[name=path]").val();
        }
        var editorHeight = adjustHeight("#editorArea", 50);
        $("#editorArea").css("overflow-y", "hidden");
        var editor = initCodeMirror("#editor", {
            filename: getParamPath(),
            height: editorHeight
        });
        window.codeEditor = editor;

        // 重命名
        $(".rename-btn").click(function (event) {
            var name     = $("[name=basename]").val();
            var dirname  = $("[name=dirname]").val();
            xnote.prompt("重命名为", name, function (new_name) {
                var request =  {dirname: dirname, old_name: name, new_name: new_name};
                xnote.http.post("/fs_api/rename", request,
                    function (resp) {
                        if (resp.success) {
                            var location = resp.location;
                            window.location.href = "/code/edit?path=" + dirname + "/" + new_name;
                        } else {
                            showErrorMessage("重命名失败, %s".format(resp.message));
                        }
                });
            });
        });

        // 保存
        $(".save-btn").click(function (e) {
            var params = {};
            params.path = $("[name=path]").val();
            params.content = $("[name=content]").val();
            params.config_key = xnote.getUrlParam("config_key")
            xnote.http.post("{{post_action}}", params, function (resp) {
                if (resp.success) {
                    xnote.toast("保存成功");
                } else {
                    xnote.toast("保存失败: " + resp.message);
                }
            })
        });
    });

xnote.tmp.showHelp = function(target) {
    var text = $(target).attr("data-text");
    xnote.showTextDialog("帮助", text);
}
</script>
{% end %}