{% extends "templates/base.html" %}

{%- macro sortable_column(label, new_sort) -%}
  {%- set params = {} -%}
  {%- if pagination -%}
    {# get any filter params #}
    {%- set params = pagination['params'] or {} -%}
  {%- endif -%}

  {%- set icon = '' -%}
  {%- if sort == new_sort -%}
    {# append sort direction suffix #}
    {%- set new_sort = '%s-' % new_sort if sort_asc else new_sort -%}
    {# select sort direction icon  #}
    {%- set icon = '&#x25B2;' if sort_asc else '&#x25BC;' -%}
  {%- endif -%}

  {# override sort param #}
  {%- do params.update({'sort': new_sort}) -%}

  <a href="{{ url_for(base_route, **params) }}">{{ label }}{{ icon | safe }}</a>
{%- endmacro -%}

{% block title %}{{ i18n('interface.resources.title') }}{% endblock %}
{% block container %}
  <h1>{{ self.title() }}</h1>

  {% block buttons %}
    <div class="btn-toolbar mb-2 gap-1">
      <a href="{{ url_for('new_%s' % endpoint_suffix) }}" class="btn btn-primary" role="button">
        {{ utils.render_icon('plus-lg') }} {% block new_resource_label %}{{ i18n('interface.common.new') }}{% endblock %}
      </a>

      <div class="btn-group search-group">
        <form action="{{ url_for(base_route) }}" method="get">
          <div class="input-group">
            <input name="search" type="text" class="form-control" placeholder="{{ i18n('interface.common.ph_search') }}" value="{{ search_text if search_text }}">
            <button class="btn btn-secondary" type="submit">{{ utils.render_icon('search') }}</button>
          </div>
        </form>
      </div>
      {% block extra_buttons %}{% endblock %}
    </div>
  {% endblock %}

  {% block pagination %}
    {% if pagination %}
      {% set params = pagination['params'] or {} %}
      {% set page = pagination['page'] %}
      {% set num_pages = pagination['num_pages'] %}
      {% set per_page = params['per_page'] %}
      {% if per_page == pagination['per_page_default'] %}
        {# clear default per_page value #}
        {% set per_page = none %}
      {% endif %}

      {% set start_page = 1 %}
      {% set end_page = num_pages %}
      {% set num_page_buttons = 5 %}
      {% if num_pages > num_page_buttons %}
        {% set start_page = [page - 2, 1] | max %}
        {% set end_page = start_page + num_page_buttons - 1 %}
        {% if end_page > num_pages %}
          {% set end_page = num_pages %}
          {% set start_page = end_page - num_page_buttons + 1 %}
        {% endif %}
      {% endif %}

      <div class="btn-toolbar mb-2 gap-1">
        <nav aria-label="{{ i18n('interface.main.page_navigation') }}">
          <ul class="pagination mb-0">

            {# prev page #}
            <li class="page-item {{ 'disabled' if page <= 1 else '' }}">
              <a class="page-link" href="{{ url_for(base_route, page=page-1, **params) }}" aria-label="{{ i18n('interface.main.previous') }}">
                <span aria-hidden="true">&laquo;</span>
              </a>
            </li>

            {# first page #}
            {% if start_page > 1 %}
              <li class="page-item">
                <a class="page-link" href="{{ url_for(base_route, page=1, **params) }}">1</a>
              </li>
            {% endif %}

            {# gap #}
            {% if start_page > 2 %}
              <li class="page-item disabled">
                <a class="page-link" href="#">
                  <span aria-hidden="true">&hellip;</span>
                </a>
              </li>
            {% endif %}

            {# pages around current page #}
            {% for p in range(start_page, end_page + 1) %}
              <li class="page-item {{ 'active' if p == pagination['page'] else '' }}">
                <a class="page-link" href="{{ url_for(base_route, page=p, **params) }}">{{ p }}</a>
              </li>
            {% endfor %}

            {# gap #}
            {% if end_page < num_pages -1 %}
              <li class="page-item disabled">
                <a class="page-link" href="#">
                  <span aria-hidden="true">&hellip;</span>
                </a>
              </li>
            {% endif %}

            {# last page #}
            {% if end_page < num_pages %}
              <li class="page-item">
                <a class="page-link" href="{{ url_for(base_route, page=num_pages, **params) }}">{{ num_pages }}</a>
              </li>
            {% endif %}

            {# next page #}
            <li class="page-item {{ 'disabled' if page >= num_pages else '' }}">
              <a class="page-link" href="{{ url_for(base_route, page=page+1, **params) }}" aria-label="{{ i18n('interface.main.next') }}">
                <span aria-hidden="true">&raquo;</span>
              </a>
            </li>
          </ul>
        </nav>

        {% if pagination['per_page_options'] %}
          <div class="dropdown">
            <button class="btn btn-light dropdown-toggle h-100" type="button" data-bs-toggle="dropdown" aria-expanded="false">
              {{ utils.render_icon('list') }}
            </button>
            <ul class="dropdown-menu">
            {% for pp in pagination['per_page_options'] %}
              <li>
                {% set page_params_items = (params.items()|list) + [('per_page', pp)] %}
                {% set page_params = dict.from_keys(page_params_items) %}
                <a class="dropdown-item {{ 'active' if pp == params['per_page'] }}" href="{{ url_for(base_route, **page_params) }}">{{ pp }} {{ i18n('interface.main.per_page') }}</a>
              </li>
            {% endfor %}
            </ul>
          </div>
        {% endif %}
      </div>

    {% endif %}
  {% endblock %}

  {% block table %}
    <table class="table table-striped">
      <thead>
        <tr>
          {% block table_headers %}
            <th>{{ i18n('interface.common.id') }}</th>
          {% endblock %}
          <th></th>
        </tr>
      </thead>
      <tbody>
      {% for resource in resources %}
        <tr>
          {% block resource_fields scoped %}
            <td>{{ resource.id }}</td>
          {% endblock %}
          <td>
            <a href="{{ url_for('edit_%s' % endpoint_suffix, id=resource[pkey]) }}" class="btn btn-primary" role="button">
              {{ utils.render_icon('pencil') }} {{ i18n('interface.common.edit') }}
            </a>
            {% if endpoint_suffix == "resource" %}
              <a href="{{ url_for('new_permission', resource_id=resource['id']) }}" class="btn btn-success" role="button">
                {{ utils.render_icon('plus-lg') }} {{ i18n('interface.common.new_permission') }}
              </a>
              {% if resource['type'] == 'map' %}
                <a href="{{ url_for('import_resource', id=resource['id']) }}" method="post" class="btn btn-success" role="button">
                  {{ utils.render_icon('plus-lg') }} {{ i18n('interface.common.import_resources') }}
                </a>
              {% endif %}
            {% endif %}
            <form action="{{ url_for('destroy_%s' % endpoint_suffix, id=resource[pkey]) }}" method="post" style="display: inline;">
              <input type="hidden" name="_method" value="DELETE" />
              <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
              <button type="submit" class="btn btn-danger" onclick="return confirm('{% block delete_resource_confirmation %}{{ i18n('interface.common.confirm_message_remove') }}{% endblock %}');">
                {{ utils.render_icon('x') }} {{ i18n('interface.common.remove') }}
              </button>
            </form>
          </td>
        </tr>
      {% endfor %}
      </tbody>
    </table>
  {% endblock %}
{% endblock %}
