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

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

{% block scripts %}
  {{ block.super }}
  {{ media.js }}
  {% if cl.formset or action_form %}
    <script src="{% url 'admin:jsi18n' %}"></script>
  {% endif %}
{% endblock %}

{% block bodyclass %}{{ block.super }} changelist-page{% endblock %}


{% block header_buttons %}
  {% if has_add_permission %}
    <li class="nav-item d-sm-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-fw fa-plus"></i>
        <span>{% blocktrans with cl.opts.verbose_name as name %}Add {{ name }}{% endblocktrans %}</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 message_items %}
  {{ block.super }}

  {% if cl.formset.errors %}
    <li class="list-group-item list-group-item-danger">
      {% if cl.formset.total_error_count == 1 %}
        {% trans "Please correct the error below." %}
      {% else %}
        {% trans "Please correct the errors below." %}
      {% endif %}
    </li>

    {% for error in cl.formset.non_form_errors %}
      <li class="list-group-item list-group-item-danger">
        {{ error }}
      </li>
    {% endfor %}
  {% endif %}
{% endblock %}


{% block toolbar %}
  {% if cl.has_filters %}
    <div class="card">
      <div class="card-header">
        <h3 class="card-title">{% trans 'Filter' %}</h3>
      </div>
      <div class="card-body">
        {% block filters %}
          <form action="" method="get">
            {% for spec in cl.filter_specs %}
              {% paper_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-block">
              {% trans 'Apply' %}
            </button>
          </form>
        {% endblock %}
      </div>
    </div>
  {% endif %}
{% endblock %}


{% block before_content %}
  {% firstof cl.opts.verbose_name_plural title as page_title %}

  <div class="row">
    <div class="col">
      <h1 class="paper-title">{{ page_title|capfirst }}</h1>
    </div>

    <div class="object-tools col-auto mb-2 d-none d-sm-block text-right">
      {% block object-tools %}
        {% if has_add_permission %}
          {% url cl.opts|admin_urlname:'add' as add_url %}
          <a href="{% add_preserved_filters add_url is_popup to_field %}" class="btn btn-success">
            <i class="fa fa-fw fa-plus"></i>
            <span>{% blocktrans with cl.opts.verbose_name as name %}Add {{ name }}{% endblocktrans %}</span>
          </a>
        {% endif %}
      {% endblock %}
    </div>
  </div>
{% endblock %}


{% block content %}
  {% if cl.full_result_count %}
    <div class="paper-filters-card card">
      <div class="card-body">
        <div class="card-row row">
          <div class="col">
            <small class="results-counter text-muted nowrap">
              {% blocktrans count counter=cl.result_count %}{{ counter }} result{% plural %}{{ counter }} results{% endblocktrans %}

              {% if cl.result_count != cl.full_result_count %}
                (<a href="?{% if cl.is_popup %}_popup=1{% endif %}">{% if cl.show_full_result_count %}{% blocktrans with full_result_count=cl.full_result_count %}{{ full_result_count }} total{% endblocktrans %}{% else %}{% trans "Show all" %}{% endif %}</a>)
              {% endif %}
            </small>
          </div>

          <div class="col-auto">
            {% search_form cl %}
          </div>
        </div>

        {% if cl.result_count and cl.date_hierarchy %}
          <div class="card-row">
            {% date_hierarchy cl %}
          </div>
        {% endif %}

        {% block hierarchy_filters %}{% endblock hierarchy_filters %}
      </div>
    </div>
  {% endif %}

  {% if cl.result_count %}
    <div class="card">
      <div class="card-body">
        {% block pagination_top %}
          {% paper_pagination cl %}
        {% endblock %}

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

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

          {% if cl.formset or action_form and actions_on_top and cl.show_admin_actions %}
            <div class="paper-actions-row mb-2">
              {% if action_form and actions_on_top and cl.show_admin_actions %}
                {% admin_actions %}
              {% endif %}

              {% if cl.formset %}
                <div class="paper-save-table-box">
                  <button type="submit" name="_save" class="btn btn-sm btn-info">
                    <i class="fa fa-fw fa-save"></i><span>{% trans 'Save' %}</span>
                  </button>
                </div>
              {% endif %}
            </div>
          {% endif %}

          {% block result_list %}
            {% paper_result_list cl %}
          {% endblock %}

          {% if cl.formset or action_form and actions_on_bottom and cl.show_admin_actions %}
            <div class="paper-actions-row mt-3">
              {% if action_form and actions_on_bottom and cl.show_admin_actions %}
                {% admin_actions %}
              {% endif %}

              {% if cl.formset %}
                <div class="paper-save-table-box">
                  <button type="submit" name="_save" class="btn btn-sm btn-info">
                    <i class="fa fa-fw fa-save"></i><span>{% trans 'Save' %}</span>
                  </button>
                </div>
              {% endif %}
            </div>
          {% endif %}
        </form>

        {% block pagination_bottom %}
          {% paper_pagination cl %}
        {% endblock %}
      </div>
    </div>
  {% else %}
    {% if cl.full_result_count %}
      <div class="alert alert-info mb-0">
        <h4>{% trans 'Nothing found' %}!</h4>
        <a href="?{% if cl.is_popup %}{{ POPUP_VAR }}=1{% endif %}">
          {% trans 'Reset search and filters' %}
        </a>
      </div>
    {% else %}
      <div class="alert alert-warning mb-0">
        {% blocktrans with cl.opts.verbose_name_plural|capfirst as name_plural %}{{ name_plural }} are not created yet{% endblocktrans %}.
      </div>
    {% endif %}
  {% endif %}
{% endblock %}
