{% extends theme("layout.html") %}
{% import "forms.html" as forms with context %}
{% import "utils.html" as utils with context %}
{% from theme(form.meta_template) import basic_meta_editor with context %}
{% import theme("itemviews.html") as itemviews with context %}
{% import theme(form['content_form'].template) as content_template with context %}
{% set extra_head = content_template.extra_head %}
{% if content_template.basic_data_editor is defined %}
    {% set data_editor = content_template.basic_data_editor %}
{% else %}
    {% set data_editor = content_template.data_editor %}
{% endif %}
{% set title = _("Modifying '{item_name}'").format(item_name=item_name) %}

{% block head %}
    {{ super() }}
    {% if extra_head %}
        {{ extra_head() }}
    {% endif %}
{% endblock %}

{% block views %}
    {{ itemviews }}
{% endblock %}

{% block content %}
    <h1>{{ title }}</h1>
    <div id="moin-modify" class="moin-form">
        <a class="btn btn-success" href="{{ url_for('.download_item', item_name=item_name, mimetype='application/x.moin.download') }}">
            <span class="fa fa-download"></span> {{ _('Download') }}
        </a>
        {{ gen.form.open(form, method='post', enctype='multipart/form-data') }}
            {{ forms.render_errors(form) }}
            {{ forms.render_errors(form['meta_form']['acl']) }}
            {#
               Workaround:
               For *Draw content, hide submit button and form['comment'], since *Draw
               POSTs originate from their respective applets.
            #}
            <ul class="nav nav-tabs moin-nav moin-shadow">
                <li class="nav-item"><a class="nav-link active" href="#editor" data-toggle="tab">Edit Content</a></li>
                <li class="nav-item"><a class="nav-link" href="#meta" data-toggle="tab">Edit Meta</a></li>
                {% if user.may.admin(fqname) %}
                    <li class="nav-item"><a class="nav-link" href="#acl" data-toggle="tab">Edit ACL</a></li>
                {% endif %}
                <li class="nav-item"><a class="nav-link" href="#help" data-toggle="tab">Help</a></li>
            </ul>
            <div class="tab-content">
                <div class="tab-pane active" id="editor">
                    {% if not form['content_form'].is_draw and draft_data %}
                        <button class="moin-button moin-load-draft" type="button" >{{ _("Load Draft") }}</button>
                        {% set warning = _("Clicking this button will delete draft!") %}
                    {% endif %}
                    {{ data_editor(form['content_form'], item_name) }}
                </div>
                <div class="tab-pane active" id="meta">
                    <div class="row">
                        <div class="col-md-6">
                            {{ basic_meta_editor(form['meta_form']) }}
                        </div>
                        {% if item.meta['name'][0].endswith('Group') %}
                            {{ forms.render(form['usergroup']) }}
                            <div class="hint">
                                {{ _('Enter list of user names, one name per line. No leading or trailing blanks, no blank lines.') }}
                            </div>
                        {% endif %}
                        {% if item.meta['name'][0].endswith('Dict') %}
                            {{ forms.render(form['wikidict']) }}
                            <div class="hint">
                                {{ _('Enter "key=value" strings, one per line, no quotes, no blank lines.') }}
                            </div>
                        {% endif %}
                    </div>
                </div>

                {% set field = form['meta_form']['acl'] %}
                {% if user.may.admin(fqname) %}
                    <div class="tab-pane active" id="acl">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="form-group">
                                    {{ gen.label(field) }}
                                    {{ gen.textarea(field, rows='1', class='form-control') }}
                                </div>
                            </div>
                        </div>
                    </div>
                {% else %}
                    {{ gen.input(field, type='hidden', class='form-control') }}
                {% endif %}

                <div class="tab-pane active" id="help">
                    {{ utils.help_on_editing(help) }}
                </div>
            </div>
            {#
            {{ gen.textarea(form['meta_text'], lang='en', dir='ltr', rows=rows_meta, cols=cols) }}
            <br>
            {{ forms.render_errors(form['meta_text']) }}
            #}
            {% if not form['content_form'].is_draw %}
                <div class="form-group">
                    {{ gen.input(form['comment'], type="text", class="form-control") }}
                </div>
                <div class="submit-buttons">
                    {{ gen.input(type='submit', id='moin-save-text-button', value=form.submit_label, class='moin-button moin-modify-submit') }}
                    {{ gen.input(type='submit', id='moin-preview-text-button', name='preview', value=form.preview_label,
                        class='moin-button moin-modify-submit', onclick="$('#moin-modify').removeClass('moin-changed-input')",
                        title=warning) }}
                    <button class="moin-button moin-cancel" type="button">{{ _("Cancel") }}</button>
                </div>
            {% endif %}

        {{ gen.form.close() }}
    </div>

    {% if preview_rendered %}
        {# user clicked Preview button, show diff and modified item as draft #}
        {{ utils.diff_table(preview_diffs) }}
        <div class="moin-watermark" id="moin-content-data" >
            {{ preview_rendered | safe }}
        </div>
    {% endif %}

{% endblock %}

{% block options_for_javascript %}
    {%- if user.scroll_page_after_edit -%}
        <br id="moin-scroll-page-after-edit">
    {%- endif %}
    {%- if draft_data -%}
        <textarea id="moin-draft-data">{{ draft_data }}</textarea>
    {%- endif %}
    {%- if lock_duration -%}
        <input id="moin-lock_duration" value="{{ lock_duration }}">
    {%- endif %}
    <input id="moin-item-name" value="{{ fqname }}" >
{% endblock %}
