{% extends "wiki/article.html" %}
{% load wiki_tags i18n humanize %}


{% block wiki_pagetitle %}{% trans "Attachments" %}: {{ article.current_revision.title }}{% endblock %}

{% block wiki_contents_tab %}
<div class="row">
  <div class="col-lg-8">
    <p class="lead">{% trans "The following files are available for this article. Copy the markdown tag to directly refer to a file from the article text." %}</p>
    <p> {% blocktrans trimmed %}
      Complete markdown code syntax: <code>[attachment:id title:"text" size]</code><br>
      &nbsp;&nbsp;title: Link text replacement for the file name.
      size: Show file size after the title.
    {% endblocktrans %} </p>

    {% for attachment in attachments %}
    <table class="table table-bordered table-striped">
      <tr>
        <th colspan="4">
          <h4 style="margin-top:0;">
            <a href="{% url 'wiki:attachments_download' path=urlpath.path article_id=article.id attachment_id=attachment.id %}">{{ attachment.current_revision.get_filename }}</a>
            <span class="badge badge-dark">{{ attachment.current_revision.created|naturaltime }}</span>
            {% if attachment.current_revision.deleted %}
            <span class="badge badge-important">{% trans "deleted" %}</span>
            {% endif %}
          </h4>
          {{ attachment.current_revision.description }}
        </th>
      </tr>
      <tr>
        <th style="width: 25%">{% trans "Markdown tag" %}</th>
        <th style="width: 25%">{% trans "Uploaded by" %}</th>
        <th style="width: 25%">{% trans "Size" %}</th>
        <td style="width: 25%; text-align: right; white-space: nowrap;" rowspan="2">
          {% if attachment|can_write:user %}
            {% if not attachment.current_revision.deleted %}
              <a href="{% url 'wiki:attachments_replace' path=urlpath.path article_id=article.id attachment_id=attachment.id %}" class="btn btn-secondary btn-sm">{% trans "Replace" %}</a>
              {% if attachment.article == article %}
                <a href="{% url 'wiki:attachments_delete' path=urlpath.path article_id=article.id attachment_id=attachment.id %}" class="btn btn-secondary btn-sm">{% trans "Delete" %}</a>
              {% else %}
                <a href="{% url 'wiki:attachments_delete' path=urlpath.path article_id=article.id attachment_id=attachment.id %}" class="btn btn-secondary btn-sm">{% trans "Detach" %}</a>
              {% endif %}
            {% else %}
               Deleted
            {% endif %}
          {% endif %}
          <p style="margin-top: 10px; margin-bottom:0;">
          <a href="{% url 'wiki:attachments_history' path=urlpath.path article_id=article.id attachment_id=attachment.id %}">
            <span class="fa fa-clock"></span>
            {% trans "File history" %} ({{ attachment.attachmentrevision_set.all.count }} {% trans "revisions" %})
          </a>
          </p>
        </td>
      </tr>
      <tr>
        <td><code>[attachment:{{ attachment.id }}]</code></td>
        <td>
        {% include "wiki/includes/revision_info.html" with revision=attachment.current_revision hidedate=1 hidenumber=1 %}
        </td>
        <td>{{ attachment.current_revision.get_size|filesizeformat }}</td>
      </tr>
    </table>
    {% empty %}

      <p style="margin-bottom: 20px;"><em>{% trans "There are no attachments for this article." %}</em></p>

    {% endfor %}
  </div>
  <div class="col-lg-4" style="min-width: 330px;">
  {% if article|can_write:user and not article|is_locked %}
      <div class="card">
        <div class="card-header pb-0">
          <a class="card-toggle" href="#collapse_upload" data-toggle="collapse">
            <p class="card-title"><span class="fa fa-fw fa-upload"></span> {% trans "Upload new file" %}</p>
          </a>
        </div>
        <div id="collapse_upload" class="card-collapse collapse{% if form.errors %} show{% endif %}">
          <div class="card-body">
            {% if anonymous_disallowed %}
              {% include "wiki/includes/anonymous_blocked.html" %}
            {% else %}
            <form method="POST" class="form-vertical" id="attachment_form" enctype="multipart/form-data">
              {% wiki_form form %}
              <button type="submit" name="save" value="1" class="btn btn-primary">
                {% trans "Upload file" %}
              </button>
            </form>
            {% endif %}
          </div>
        </div>
      </div>

      <div class="card">
        <div class="card-header pb-0">
          <a class="card-toggle" data-toggle="collapse" href="#collapse_add">
            <p class="card-title"><span class="fa fa-fw fa-plus-circle"></span> {% trans "Search and add file" %}</p>
          </a>
        </div>

        <div id="collapse_add" class="card-collapse collapse">
          <div class="card-body">
            <p>{% trans "You can reuse files from other articles. These files are subject to updates on other articles which may or may not be a good thing." %}</p>
            <form method="GET" action="{% url 'wiki:attachments_search' path=urlpath.path article_id=article.id %}" class="wiki-form-block">
              <div class="input-group">
                {{ search_form.query }}
                <span class="input-group-btn">
                  <button class="btn btn-secondary" type="submit">
                    <span class="fa fa-search"></span>
                  </button>
                </span>
              </div><!-- /input-group -->
            </form>
          </div>
        </div>
      </div>

      {% if article|can_write:user %}
        <div class="card">
          <div class="card-header pb-0">
            <a class="card-toggle" data-toggle="collapse" href="#collapse_restore">
              <p class="card-title"><span class="fa fa-fw fa-trash"></span> {% trans "Restore attachments" %}</p>
            </a>
          </div>
          <div id="collapse_restore" class="card-collapse collapse">
            <div class="card-body">

            {% for attachment in deleted_attachments %}
            <p>
              {{ attachment.current_revision.get_filename }}
              {% if attachment.current_revision.previous_revision.id %}
              <form method="POST" action="{% url 'wiki:attachments_revision_change' path=urlpath.path article_id=article.id attachment_id=attachment.id revision_id=attachment.current_revision.previous_revision.id %}" style="margin:0; padding:0;">
                {% csrf_token %}
                <button class="btn btn-secondary">
                  {% trans "Restore" %}
                </button>
              </form>
              {% endif %}
            </p>
            {% empty %}

            <p><em>{% trans "Nothing to restore" %}</em></p>

            {% endfor %}

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


  {% else %}

    {% if article|is_locked %}
    <p class="muted">
      <i class="fa fa-lock"></i>
      {% trans "The article is currently locked for editing, and therefore no new attachments can be added." %}
    </p>
    {% endif %}

  {% endif %}
  </div>

</div>

{% endblock %}
