{% set show_attach_button = user.has_perm('add_attachment') %}

<section id="page-attachments">
  <h2>{{ _('Attachments') }} <bdi>(<span id="page-attachments-count">{{ attachment_data | length}}</span>)</bdi></h2>

  {% if show_attach_button and attachment_form %}
  <p class="add">
    <a href="javascript:;" id="page-attachments-button">Attach Files</a>
  </p>
  {% endif %}

  <p id="page-attachments-no-message" class="{% if attachment_data|length %}hidden{% endif %}">
    {{ _('This document has no attachments.') }}
  </p>

  <script>
    // This is a global, but has to be :/
    MDN_ATTACHMENTS = {{ (attachment_data_json | safe) or '[]' }};
  </script>
  <table cellpadding="0" cellspacing="0" id="page-attachments-table" class="{% if not attachment_data|length %}hidden{% endif %}">
    <thead>
      <th>{{ _('File') }}</th>
      <th>{{ _('Size') }}</th>
      <th>{{ _('Date') }}</th>
      <th>{{ _('Attached by') }}</th>
    </thead>
    <tbody>
      {% for attachment in attachment_data %}
        {{ attachment.html }}
      {% endfor %}
    </tbody>
  </table>

  {% if show_attach_button and attachment_form %}
  <form method="post" action="{{ url('wiki.new_attachment') }}" id="page-attachments-form" enctype="multipart/form-data">
  <table cellpadding="0" cellspacing="0" id="page-attachments-new-table" class="hidden">
    <thead>
      <th class="hidden page-attachment-actions-cell">&nbsp;</th>
      <th>{{ _('File') }}</th>
      <th>{{ _('Title') }}</th>
      <th>{{ _('Description') }}</th>
      <th>{{ _('Comments') }}</th>
    </thead>
    <tbody>
      <tr>
        <td class="hidden page-attachment-actions-cell"><a href="javascript:;" class="hidden delete-attachment">Delete</a></td>
        <td class="page-attachment-actions-file-cell">{{ attachment_form.file | safe }}</td>
        <td>{{ attachment_form.title | safe }}</td>
        <td>{{ attachment_form.description | safe }}</td>
        <td>{{ attachment_form.comment | safe }}</td>
      </tr>
      <tr>
        <td colspan="4">
          <input type="submit" name="submit" value="Upload" />
          {# <input type="button" name="attachMore" id="page-attachments-more" value="Attach More Files" /> #}
          <img src="/media/img/wiki/upload-spinner.gif" id="page-attachments-spinner" alt="Uploading" />
        </td>
      </tr>
    </tbody>
  </table>
  <iframe id="page-attachments-upload-target" name="page-attachments-upload-target"></iframe>
  </form>
  {% endif %}
</section>