{% extends "admin/base_site.html" %}
{% load i18n admin_urls admin_list %}
{% load paper_filters paper_list %}

{% block styles %}
  {{ block.super }}
  {{ media.css }}
{% endblock %}

{% block scripts %}
  {{ block.super }}
  {{ media.js }}
{% endblock %}

{% block bodyclass %}{{ block.super }} app-{{ opts.app_label }} model-{{ opts.model_name }} change-list{% endblock %}


{% block header_buttons %}
  {% if has_add_permission %}
    <li class="list-inline-item d-lg-none">
      {% url cl.opts|admin_urlname:'add' as add_url %}
      <a href="{% add_preserved_filters add_url is_popup to_field %}" class="btn btn-sm btn-success">
        <i class="fa fa-plus"></i>
        <span>{{ _("Add") }}</span>
      </a>
    </li>
  {% endif %}
{% endblock %}


{% block breadcrumb_items %}
  <li class="breadcrumb-item">
    <a href="{% url 'admin:app_list' app_label=cl.opts.app_label %}">{{ cl.opts.app_config.verbose_name }}</a>
  </li>

  <li class="breadcrumb-item active" aria-current="page">
    <span>{{ cl.opts.verbose_name_plural|capfirst }}</span>
  </li>
{% endblock breadcrumb_items %}


{% block messages %}
  {{ block.super }}

  {% if cl.formset and cl.formset.non_form_errors %}
    {% for error in cl.formset.non_form_errors %}
      <li class="paper-message paper-message--error">
        {{ error }}
      </li>
    {% endfor %}
  {% endif %}
{% endblock %}


{% block content_title %}
  <div class="row">
    <div class="col">
      {% firstof cl.opts.verbose_name_plural title as page_title %}
      <h1>{{ page_title|capfirst }}</h1>
    </div>
    <div class="col-auto d-none d-lg-block">
      {% block object-tools %}
        <ul class="list-inline list-default">
          {% block object-tools-items %}
            {% include "admin/change_list_object_tools.html" %}
          {% endblock %}
        </ul>
      {% endblock %}
    </div>
  </div>
{% endblock %}


{% block toolbar %}
  {% if cl.has_filters %}
    <div class="paper-card card">
      <h3 class="card-header">{{ _("Filters") }}</h3>
      <div class="card-body">
        {% block filters %}
          <form action="" method="get">
            {% for spec in cl.filter_specs %}
              {% paper_list_filter cl spec %}
            {% endfor %}

            {% get_filter_keep_params as filter_keep_params %}
            {% for name, value in filter_keep_params %}
              <input type="hidden" name="{{ name }}" value="{{ value }}">
            {% endfor %}

            <button type="submit" class="btn btn-info btn-lg btn-block">
              {{ _("Apply") }}
            </button>

            {% if cl.has_active_filters %}
              <a href="{{ cl.clear_all_filters_qs }}" class="btn btn-block btn-danger">
                <i class="fa fa-remove"></i>
                <span>{{ _("Clear all filters") }}</span>
              </a>
            {% endif %}
          </form>
        {% endblock %}
      </div>
    </div>
  {% endif %}
{% endblock %}


{% block content %}
  {% if cl.result_count and cl.date_hierarchy %}
    <div class="paper-card card mb-3">
      <h5 class="card-header">
        {{ _("Filter by date") }}
      </h5>
      <div class="card-body">
        {% block date_hierarchy %}
          {% date_hierarchy cl %}
        {% endblock %}
      </div>
    </div>
  {% endif %}

  {% if cl.result_count %}
    {% if action_form and actions_on_top and cl.show_admin_actions or cl.search_fields %}
      <div class="row">
        {% if action_form and actions_on_top and cl.show_admin_actions %}
          <div class="col-lg order-1 order-lg-0 mb-3">
            {% include "admin/actions.html" with index=0 %}
          </div>
        {% else %}
          <div class="col-lg"></div>
        {% endif %}

        {% if cl.search_fields %}
          <div class="col-auto mb-3">
            {% block search %}
              {% include "admin/search_form.html" %}
            {% endblock %}
          </div>
        {% endif %}
      </div>
    {% endif %}

    <form id="changelist-form" method="post"{% if cl.formset and cl.formset.is_multipart %} enctype="multipart/form-data"{% endif %} novalidate>
      {% csrf_token %}

      {% if cl.formset %}
        {{ cl.formset.management_form }}
      {% endif %}

      {% block result_list %}
        {% paper_result_list cl %}
      {% endblock %}
    </form>

    {% if cl.formset or action_form and actions_on_bottom and cl.show_admin_actions %}
      <div class="row">
        {% if action_form and actions_on_bottom and cl.show_admin_actions %}
          <div class="col-md mt-3">
            {% include "admin/actions.html" with index=1 %}
          </div>
        {% else %}
          <div class="col-md"></div>
        {% endif %}

        {% if cl.formset %}
          <div class="col-auto mt-3">
            <button type="submit" form="changelist-form" name="_save" class="btn btn-sm btn-success">
              <i class="fa fa-save"></i><span>{{ _("Save") }}</span>
            </button>
          </div>
        {% else %}
          <div class="col-auto"></div>
        {% endif %}
      </div>
    {% endif %}
  {% else %}
    {% if cl.full_result_count %}
      <div class="paper-message paper-message--info">
        <h4>{{ _("Nothing found") }}!</h4>
        <a href="?{% if cl.is_popup %}{{ POPUP_VAR }}=1{% endif %}">
          {{ _("Reset search and filters") }}
        </a>
      </div>
    {% else %}
      <p class="paper-message paper-message--warning">
        {% blocktrans with cl.opts.verbose_name_plural|capfirst as name_plural %}{{ name_plural }} are not created yet{% endblocktrans %}.
      </p>
    {% endif %}
  {% endif %}
{% endblock %}
