{% from "macros/user.html" import m_user_photo, m_user_link %}
{% from "macros/form.html" import m_field %}

{# default macros for displaying attachments #}

{# attachments list #}
{% macro m_attachments(entity) %}
  {%- if attachments.has_attachments(entity) %}
    <ul class="attachments">
      {%- for attachment in attachments.for_entity(entity) %}
        <li id="attachment-{{ attachment.id }}">{{ m_attachment(attachment) }}</li>
      {%- endfor %}
    </ul>
  {%- endif %}
{% endmacro %}

{# single attachment #}
{% macro m_attachment(attachment) %}
  {%- set entity = attachment.entity %}
  {%- set manager = attachments.manager(entity) %}
  {%- set can_edit = manager.can_edit(entity) %}
  {%- set can_delete = manager.can_delete(entity) %}
  <div class="media">
    <div class="media-left">
      <i class="fa fa-file fa-big"></i>
    </div>
    <div class="media-body">
      {%- if can_edit or can_delete %}
        <div class="pull-right">
          {%- if can_edit %}
            <a class="btn btn-default"
                title="{{ _("Edit") }}"
                href="{{ url_for('attachments.edit', entity_id=entity.id, object_id=attachment.id) }}"><i
                class="fa fa-edit"></i></a>
          {%- endif %}
          {%- if can_delete %}
            <a class="btn btn-danger js-widget"
                href="{{ url_for('attachments.delete', entity_id=entity.id, object_id=attachment.id) }}"
                data-init-with="deleteConfirm"
                data-init-params="{{ {
                              'title': _('Delete attachment ?'),
                              'message': _('Do you really want to delete this attachment?'),
                              'label': _("Delete"),
                              'cancelLabel': _('Cancel'),
                              }|tojson|forceescape }}">
              <i class="fa fa-trash fa-inverse"></i>
            </a>
          {%- endif %}
        </div>
      {%- endif %}
      <h4 class="media-heading">
        <a href="{{ url_for('attachments.download',
                            entity_id=attachment.entity.id,
                            object_id=attachment.id) }}"
            title="{{ _('Download') }}">{{ attachment.name }}</a>
        <small>{{ attachment.blob.size | filesize }} - {{ attachment.updated_at | age }}</small>
      </h4>
      <p>{{ attachment.description }}</p>
    </div>
  </div>
{% endmacro %}

{# new attachment form #}
{% macro m_attachment_form(entity) %}
  {%- set ctx = attachments.get_form_context(entity) %}
  <form class="form-inline" action="{{ ctx.url }}" method="POST" enctype="multipart/form-data" role="form">
    {%- for field in ctx.form %}
      {{ m_field(field) }}
    {%- endfor %}

    <div class="form-group form-actions">
      {%- for button in ctx.buttons %}
        {{ button.render() }}
      {%- endfor %}
    </div>
  </form>
{% endmacro %}
