{% load paper_styles %}

<fieldset class="paper-formset__form paper-card card mb-3 {% paper_form_classes inline_form %}{% if inline_form.form.errors %} paper-formset__form--invalid{% endif %}{% if inline_admin_formset.sortable_allowed %} sortable-item{% endif %}{% if inline_form.original or inline_form.show_url %} has_original{% endif %}">
  <div class="card-header py-2">
    <div class="d-flex align-items-center justify-content-between flex-wrap">
      <h4 class="paper-formset__form-caption card-title inline-label pr-4 mb-0">
        {% if inline_form.original %}
          {{ inline_form.original }}
        {% else %}
          {{ inline_admin_formset.opts.verbose_name|capfirst }} #{% if form_index == '__prefix__' %}{{ form_index }}{% else %}{{ form_index|add:1 }}{% endif %}
        {% endif %}
      </h4>

      <div class="btn-square-group">
        {% if inline_form.show_url %}
          <a href="{{ inline_form.absolute_url }}"
             class="btn btn-square btn-sm btn-square--info"
             data-toggle="tooltip"
             data-placement="top"
             data-trigger="hover"
             data-html="true"
             title="{{ _("View on site") }}"
             target="_blank"
             rel="noopener">
            <i class="fa fa-eye"></i>
          </a>
        {% endif %}

        {% if inline_admin_formset.sortable_allowed %}
          <button type="button"
                  class="sortable-move-up btn btn-square btn-sm btn-square--info"
                  data-formset-toggle="up"
                  data-toggle="tooltip"
                  data-placement="top"
                  data-trigger="hover"
                  data-html="true"
                  title="{{ _("Move up") }}">
            <i class="fa fa-chevron-up"></i>
          </button>

          <button type="button"
                  class="sortable-move-down btn btn-square btn-sm btn-square--info"
                  data-formset-toggle="down"
                  data-toggle="tooltip"
                  data-placement="top"
                  data-trigger="hover"
                  data-html="true"
                  title="{{ _("Move down") }}">
            <i class="fa fa-chevron-down"></i>
          </button>
        {% endif %}

        {% if inline_admin_formset.formset.can_delete and inline_admin_formset.has_delete_permission and inline_form.original %}
          <div class="delete-inline btn btn-square btn-sm btn-square--danger"
               data-toggle="tooltip"
               data-placement="top"
               data-trigger="hover"
               data-html="true"
               title="{{ inline_form.deletion_field.field.label }}">
            <div class="custom-control custom-checkbox user-select-none">
              {{ inline_form.deletion_field.field }}
              <label for="{{ inline_form.deletion_field.field.id_for_label }}"
                     class="custom-control-label"></label>
              <i class="delete-inline__icon fa fa-trash"></i>
            </div>
          </div>
        {% endif %}

        {% if not inline_form.original and not inline_form.show_url %}
          <button type="button"
                  class="btn btn-square btn-sm btn-square--danger"
                  data-formset-toggle="delete"
                  data-toggle="tooltip"
                  data-placement="top"
                  data-trigger="hover"
                  data-html="true"
                  title="{{ _("Delete") }}">
            <i class="fa fa-trash"></i>
          </button>
        {% endif %}
      </div>
    </div>
  </div>

  <div class="card-body p-0">
    {% spaceless %}
      <ul class="paper-messages messagelist mb-3">
        {% for error in inline_form.form.non_field_errors %}
          <li class="paper-message paper-message--error">
            {{ error }}
          </li>
        {% endfor %}
      </ul>
    {% endspaceless %}

    {% if inline_form.needs_explicit_pk_field %}
      {{ inline_form.pk_field.field }}
    {% endif %}

    {{ inline_form.fk_field.field }}

    {% for fieldset in inline_form %}
      {% if fieldset.has_visible_field %}
        <fieldset class="paper-card card border-0 {{ fieldset.classes }}">
          {% include "admin/includes/fieldset.html" %}
        </fieldset>
      {% else %}
        <fieldset class="{{ fieldset.classes }}" hidden>
          {% include "admin/includes/fieldset.html" %}
        </fieldset>
      {% endif %}
    {% endfor %}
  </div>
</fieldset>
