{# vim: set ts=2 et sts=2 sw=2: #}
{% extends "wiki/base.html" %}
{% from "layout/errorlist.html" import errorlist %}
{% set title = _('{document} | Edit Article')|f(document=document.title) %}
{% block title %}{{ page_title(title) }}{% endblock %}
{# TODO: Change KB url to landing page when we have one #}
{% set crumbs = [(url('wiki.category', document.category), document.get_category_display()),
                 (document.get_absolute_url(), document.title),
                 (None, _('Edit Article'))] %}
{% set classes = 'edit' %}
{% block bodyclass %}edit{% if document.is_template %} is-template{% endif %}{% endblock %}

{% block content %}
<section id="content">
  <div class="wrap">
    <div id="content-main" class="full">
  <article id="edit-document" class="article" role="main">
    <form id="wiki-page-edit" class="editing" method="post" action="">
      <fieldset>

        {{ errorlist(revision_form) }}

        {% if collision %}
        <section class="collision-compare">
          <p>{% trans %}
            Please review the differences between your changes and the changes
            saved since you started editing. You may save again, but try to
            resolve any conflicts by hand so that the changes made since you
            started are not lost.
          {% endtrans %}</p>

          <div class="revision-diff">
            <header>
              <div>
                  <h3>{{ _('Your Changes') }}</h3>
                </h3>
                <p>{{ _('Revision {id} by {user} on {date}')|fe(id=original_revision.id, user=original_revision.creator, date=datetimeformat(original_revision.created, format='longdatetime')) }}</p>
              </div>
              <div>
                <h3>
                    <a href="{{ url('wiki.revision', current_revision.document.full_path, current_revision.id) }}">
                      {{ _('Current Revision:')|f(num=current_revision.id) }}
                    </a>
                </h3>
                <p>{{ _('Revision {id} by {user} on {date}')|fe(id=current_revision.id, user=current_revision.creator, date=datetimeformat(current_revision.created, format='longdatetime')) }}</p>
              </div>
            </header>

            {% set title = request.POST['title'] %}
            {% set slug = request.POST['slug'] %}
            {% set keywords = request.POST['request'] %}
            {% set summary = request.POST['summary'] %}

            {% if title and current_revision.title and title != current_revision.title %}
                <h4>{{ _('Title:') }}</h4>
                <div><p>{{ title }}</p></div>
                <div><p>{{ current_revision.title }}</p></div>
            {% endif %}

            {% if slug and current_revision.slug and slug != current_revision.slug %}
                <h4>{{ _('Slug:') }}</h4>
                <div><p>{{ slug }}</p></div>
                <div><p>{{ current_revision.slug }}</p></div>
            {% endif %}

            {% if tags and current_revision.tags and tags != current_revision.tags %}
                <h4>{{ _('Tags:') }}</h4>
                <div><p>{{ tags }}</p></div>
                <div><p>{{ current_revision.tags }}</p></div>
            {% endif %}

            {% if keywords and current_revision.keywords and keywords != current_revision.keywords %}
                <h4>{{ _('Keywords:') }}</h4>
                <div><p>{{ keywords }}</p></div>
                <div><p>{{ current_revision.keywords }}</p></div>
            {% endif %}

            {% if summary and current_revision.summary and summary != current_revision.summary %}
                <h4>{{ _('Search results summary:') }}</h4>
                <div><p>{{ summary }}</p></div>
                <div><p>{{ current_revision.summary }}</p></div>
            {% endif %}

            {% if content and current_revision.content and content != current_revision.content %}
                <h4>{{ _('Content:') }}</h4>
                {{ diff_table(content, current_content) }}
            {% endif %}

          </div>
        </section>
        {% endif %}

        <header id="article-head">

          <div class="title">
            <h1>{{ _('Editing <em>{title}</em>')|fe(title=revision.title) }}</h1>
            {% if not section_id %}
                <button type="button" id="btn-properties" title="Edit Page Title and Properties">{{ _('Edit Page Title and Properties') }}</button>
            {% endif %}
            <p class="save-state" id="draft-status">{% trans %}Draft <span id="draft-action"></span> <time id="draft-time" class="timeago" title=""></time>{% endtrans %}</p>
          </div>

          {% if revision_form and not section_id %}
            <ul class="metadata">
                <li><label>{{_('Title:')}}</label> {{ revision_form.title | safe }}</li>
                <li><label>{{_('Slug:')}}</label> {{ revision_form.slug | safe }}</li>
                {% if parent_slug %}
                  <li><label>{{_('Parent:')}}</label> 
                    <a href="{{ parent_path }}" class="metadataDisplay" target="_blank">{{ parent_slug }}</a></li>
                {% endif %}
                {% if not document.is_template %}
                  <li><label><abbr title="{{_('Generate table of contents')}}">{{_('TOC:')}}</abbr></label>
                    {{ revision_form.show_toc | safe }}
                  </li>
                {% endif %}
            </ul>
          {% endif %}

          {% if not collision %}
            {{ revision_form.current_rev | safe }}
          {% endif %}

          {% include 'wiki/includes/page_buttons.html' %}

        </header>
    
        {% if revision_form %}
            {{ revision_form.content | safe }}
            {% if document.is_template %}
              <div id="ace_content"></div>
            {% endif %}
            <input type="hidden" name="form" value="rev" />
            {{ revision_form.hidden_fields()|join|safe }}
            
            {% include 'wiki/includes/title_autosuggest_url.html' %}

            <section class="page-meta">
              {% set tags = document.tags.all() %}
              <section id="page-tags">
                <h2>{{ _('Tags') }}</h2>
                <input id="tagit_tags" type="text" name="tags" value="{% for tag in tags %}{{ tag.name }},{% endfor %}" maxlength="255" />
              </section>
            </section>

          <section>
          <h4>{{_('Review needed?')}}</h4>
            {{ revision_form.review_tags|safe }}
          </section>
        {% endif %}
      </fieldset>
    </form>

    <section class="page-meta">
      {% include 'wiki/includes/attachment_list.html' %}
    </section>
    
  </article>
    </div>
   </div>
</section>
{% endblock %}

{% block site_js %}
    {{ super() }}
    {{ js('framebuster') }}

    {% if document.is_template %}
      <script src="{{ MEDIA_URL }}ace/ace.js" 
        type="text/javascript" charset="utf-8"></script>
      <script src="{{ MEDIA_URL }}ace/theme-dreamweaver.js"
        type="text/javascript" charset="utf-8"></script>
      <script src="{{ MEDIA_URL }}ace/mode-javascript.js"
        type="text/javascript" charset="utf-8"></script>
    {% endif %}

{% endblock %}