{#
    This template renders the web page used to modify an existing item, preview
    changes to a text item, or create a new wiki item.

    Based upon content type, the data_editor macro that is passed will generate a
    file upload option and/or a text editor, or specialized a drawing tool to
    create/modify svgdraw items.

    If a new item, this page is displayed after the user navigates through a series of web pages to:
        * select item type (modify_select_itemtype.html), user may select default, ticket, blog
        * select content type (modify_select_contenttype.html)
        * select edit "from scratch" or "from template"
#}

{% import "forms.html" as forms with context %}
{% import "utils.html" as utils with context %}

{% extends theme("show.html") %}

{% from form.meta_template import meta_editor with context %}
{# Import macros data_editor and extra_head from content_form's template.
   extra_head is optional, so instead of a simple "import from" we need to do
   this manually #}
{% import form['content_form'].template as content_template with context %}
{% set extra_head = content_template.extra_head %}
{% set data_editor = content_template.data_editor %}

{% set title = _("Modifying '{item_name}'").format(item_name=fqname|shorten_fqname) %}

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

{% block content %}
    <h1>{{ title }}</h1>
    <div id="moin-modify" class="moin-form">
        {% 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 %}
        {{ gen.form.open(form, method='post', enctype='multipart/form-data') }}

            {{ utils.help_on_editing(help) }}
            {{ data_editor(form['content_form'], item_name) }}
            {% set may_admin = user.may.admin(fqname) %}
            {{ meta_editor(form['meta_form'], may_admin) }}
            {#
                Workaround:
                For *Draw content, hide submit button and form['comment'], since *Draw
                POSTs originate from their respective applets.
             #}
             {% if not form['content_form'].is_draw %}
                <dl>
                    {{ forms.render(form['comment']) }}
                </dl>
            {% endif %}
            {{ forms.render_errors(form) }}
            {#
               Workaround:
               For *Draw content, hide submit button and form['comment'], since *Draw
               POSTs originate from their respective applets.
            #}
            {% if not form['content_form'].is_draw %}
                <div class="submit-buttons">
                    {% set warning = "" %}
                    {% if draft_data %}
                        <button class="moin-button moin-load-draft" type="button" >{{ _("Load Draft") }}</button>
                        {% set warning = _("Clicking this button will delete draft!") %}
                    {% endif %}
                    {{ gen.input(type='submit', id='moin-save-text-button', value=form.submit_label,
                        class='moin-button moin-modify-submit', title=warning, tabindex='-1') }}
                    {{ 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) }}
                    {{ gen.input(type='submit', id='moin-cancel-text-button', name='cancel', value=form.cancel_label,
                        class='moin-button moin-modify-submit negative', onclick="$('#moin-modify').removeClass('moin-changed-input')",
                        title=warning) }}
                </div>

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



{% 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 %}
