{% extends "admin/base.html" %}
{% load i18n static admin_urls admin_list %}

{# ---------- TITLE ---------- #}
{% block title %}{% if cl.formset and cl.formset.errors %}{% translate "Error:" %} {% endif %}{{ cl.opts.verbose_name_plural|capfirst }} | {{ site_title|default:'' }}{% endblock %}

{# ---------- BODY CLASS ---------- #}
{% block bodyclass %}{{ cl.opts.app_label }}-{{ cl.opts.model_name }} change-list-page{% endblock %}

{# ---------- MEDIA (align with Django admin change_list) ---------- #}
{% block extrastyle %}
  {{ block.super }}
  {% if cl.formset or action_form %}
    <script src="{% url 'admin:jsi18n' %}"></script>
  {% endif %}
  {{ media.css }}
  {% if not actions_on_top and not actions_on_bottom %}
    <style>
      #changelist table thead th:first-child { width: inherit; }
    </style>
  {% endif %}
{% endblock %}

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

{# ---------- BREADCRUMBS ---------- #}
{% block breadcrumbs %}
  {% if not is_popup %}
    <div class="breadcrumbs">
      <a href="{% url 'admin:index' %}">{% trans 'Home' %}</a>
      &rsaquo; {{ cl.opts.verbose_name_plural|capfirst }}
    </div>
  {% endif %}
{% endblock %}

{# ---------- TITLE + ADD (one row: title left, add right) ---------- #}
{% block content_title %}
  <div class="lucus-changelist__pagehead">
    <h1 class="lucus-changelist__title">{{ cl.opts.verbose_name_plural|capfirst }}</h1>
    {% if has_add_permission %}
      <div class="lucus-changelist__pagehead-actions">
        {% url cl.opts|admin_urlname:'add' as add_url %}
        <a href="{% add_preserved_filters add_url is_popup to_field %}" class="lucus-changelist__addlink addlink">
          {% blocktrans with cl.opts.verbose_name as name %}Add {{ name }}{% endblocktrans %}
        </a>
      </div>
    {% endif %}
  </div>
{% endblock %}

{% block object-tools %}{% endblock %}

{# ---------- CONTENT ---------- #}
{% block content %}
  <div id="content-main" class="lucus-content">
    {% if cl.formset and cl.formset.errors %}
      <p class="errornote">
        {% blocktranslate count counter=cl.formset.total_error_count %}Please correct the error below.{% plural %}Please correct the errors below.{% endblocktranslate %}
      </p>
      {{ cl.formset.non_form_errors }}
    {% endif %}

    <div class="module lucus-changelist{% if cl.has_filters %} filtered{% endif %}" id="changelist">
      <div class="lucus-changelist__layout">
        <div class="lucus-changelist__main-column">
          {# Pagination (left) + search (right), above the table; GET search stays outside POST form. #}
          {% if cl.search_fields or cl.result_count or show_all_url %}
            <div class="lucus-changelist__toolbar">
              <div class="lucus-changelist__toolbar-pagination">
                {% if cl.result_count or show_all_url %}
                  {% pagination cl %}
                {% endif %}
              </div>
              <div class="lucus-changelist__toolbar-search">
                {% if cl.search_fields %}
                  {% block search %}{% search_form cl %}{% endblock %}
                {% endif %}
              </div>
            </div>
          {% endif %}
          {% if cl.date_hierarchy %}
            <div class="lucus-changelist__filters-bar">
              <div class="lucus-changelist__date-hierarchy">
                {% block date_hierarchy %}{% date_hierarchy cl %}{% endblock %}
              </div>
            </div>
          {% endif %}

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

        {% if is_popup %}
          <input type="hidden" name="_popup" value="1">
        {% endif %}
        {% if to_field %}
          <input type="hidden" name="_to_field" value="{{ to_field }}">
        {% endif %}

        {% if cl.formset %}
          <div>{{ cl.formset.management_form }}</div>
        {% endif %}

        {% if action_form and actions_on_top and cl.show_admin_actions %}
          <div class="lucus-changelist__topbar">
            <div class="lucus-changelist__actions">
              {% admin_actions %}
            </div>
          </div>
        {% endif %}

        <div class="results">
          <div class="lucus-results-scroll">
            {% block result_list %}
              {% result_list cl %}
            {% endblock %}
          </div>
        </div>

        {% if cl.result_count or show_all_url or action_form and actions_on_bottom and cl.show_admin_actions or cl.formset and cl.result_count %}
          <div class="lucus-changelist__bottombar">
            {% if action_form and actions_on_bottom and cl.show_admin_actions %}
              <div class="lucus-changelist__actions">
                {% admin_actions %}
              </div>
            {% endif %}
            {% if cl.result_count or show_all_url %}
              {% pagination cl %}
            {% endif %}
            {% if cl.formset and cl.result_count %}
              <input type="submit" name="_save" class="default" value="{% translate 'Save' %}">
            {% endif %}
          </div>
        {% endif %}
      </form>
        </div>

        {% if cl.has_filters %}
          <aside class="lucus-changelist__aside" aria-label="{% translate 'Filter' %}">
            <header class="lucus-changelist__aside-header">
              <span class="lucus-changelist__aside-title">{% translate "Filters" %}</span>
            </header>
            <div class="lucus-changelist__aside-body">
              <div class="lucus-changelist__filters">
                {% for spec in cl.filter_specs %}{% admin_list_filter cl spec %}{% endfor %}
              </div>
            </div>
          </aside>
        {% endif %}
      </div>
    </div>
  </div>
{% endblock %}
