{#
/**
 * @file
 * Template for Content X fragments pager.
 */
#}

{% if pager.total_pages > 1 %}
<div class="content-x-pager">
  <div class="content-x-pager-info">
    {{ 'Page @current of @total'|t({'@current': pager.current_page + 1, '@total': pager.total_pages}) }}
    <span class="content-x-pager-count">({{ pager.total_items }} {{ 'items'|t }})</span>
  </div>
  <div class="content-x-pager-nav">
    {# Previous button #}
    <button type="button"
            class="content-x-pager-btn content-x-pager-prev{% if pager.current_page == 0 %} is-disabled{% endif %}"
            {% if pager.current_page > 0 %}
            hx-get="{{ filter_url|default('/nodehive/admin/content/fragments/filter') }}"
            hx-target="#content-x-content-area"
            hx-include="#filter-title, #hidden-type, #hidden-view, #hidden-display-lang, #hidden-status, #hidden-author, #hidden-space, #hidden-limit"
            hx-vals='{"page": {{ pager.current_page - 1 }}}'
            hx-indicator=".htmx-indicator"
            {% else %}
            disabled
            {% endif %}
            title="{{ 'Previous page'|t }}">
      <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
        <polyline points="15 18 9 12 15 6"></polyline>
      </svg>
    </button>

    {# Page numbers #}
    {% set start_page = max(0, pager.current_page - 2) %}
    {% set end_page = min(pager.total_pages - 1, pager.current_page + 2) %}

    {% if start_page > 0 %}
      <button type="button"
              class="content-x-pager-btn content-x-pager-num"
              hx-get="{{ filter_url|default('/nodehive/admin/content/fragments/filter') }}"
              hx-target="#content-x-content-area"
              hx-include="#filter-title, #hidden-type, #hidden-view, #hidden-display-lang, #hidden-status, #hidden-author, #hidden-space, #hidden-limit"
              hx-vals='{"page": 0}'
              hx-indicator=".htmx-indicator">
        1
      </button>
      {% if start_page > 1 %}
        <span class="content-x-pager-ellipsis">...</span>
      {% endif %}
    {% endif %}

    {% for i in start_page..end_page %}
      <button type="button"
              class="content-x-pager-btn content-x-pager-num{% if i == pager.current_page %} is-active{% endif %}"
              {% if i != pager.current_page %}
              hx-get="{{ filter_url|default('/nodehive/admin/content/fragments/filter') }}"
              hx-target="#content-x-content-area"
              hx-include="#filter-title, #hidden-type, #hidden-view, #hidden-display-lang, #hidden-status, #hidden-author, #hidden-space, #hidden-limit"
              hx-vals='{"page": {{ i }}}'
              hx-indicator=".htmx-indicator"
              {% endif %}>
        {{ i + 1 }}
      </button>
    {% endfor %}

    {% if end_page < pager.total_pages - 1 %}
      {% if end_page < pager.total_pages - 2 %}
        <span class="content-x-pager-ellipsis">...</span>
      {% endif %}
      <button type="button"
              class="content-x-pager-btn content-x-pager-num"
              hx-get="{{ filter_url|default('/nodehive/admin/content/fragments/filter') }}"
              hx-target="#content-x-content-area"
              hx-include="#filter-title, #hidden-type, #hidden-view, #hidden-display-lang, #hidden-status, #hidden-author, #hidden-space, #hidden-limit"
              hx-vals='{"page": {{ pager.total_pages - 1 }}}'
              hx-indicator=".htmx-indicator">
        {{ pager.total_pages }}
      </button>
    {% endif %}

    {# Next button #}
    <button type="button"
            class="content-x-pager-btn content-x-pager-next{% if pager.current_page >= pager.total_pages - 1 %} is-disabled{% endif %}"
            {% if pager.current_page < pager.total_pages - 1 %}
            hx-get="{{ filter_url|default('/nodehive/admin/content/fragments/filter') }}"
            hx-target="#content-x-content-area"
            hx-include="#filter-title, #hidden-type, #hidden-view, #hidden-display-lang, #hidden-status, #hidden-author, #hidden-space, #hidden-limit"
            hx-vals='{"page": {{ pager.current_page + 1 }}}'
            hx-indicator=".htmx-indicator"
            {% else %}
            disabled
            {% endif %}
            title="{{ 'Next page'|t }}">
      <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
        <polyline points="9 18 15 12 9 6"></polyline>
      </svg>
    </button>
  </div>
</div>
{% endif %}
