{% extends "@AutoAdminAdmin/entity_layout.html.twig" %}

{% block inner_page_top %}
  <div class="row mb-2">
    <div class="col align-items-center">
      {% include "@AutoAdminAdmin/_partials/_locale_switcher.html.twig" %}
      {% include '@AutoAdminAdmin/entity/_partials/quick_filters.html.twig' %}
    </div>
    <div class="col">
      {% include '@AutoAdminAdmin/entity/_partials/global_search.html.twig' %}
    </div>
  </div>

  {% block table_utils %}
    <div class="row mb-3">
      <div class="col">
        {% include '@AutoAdminAdmin/entity/_partials/group_actions.html.twig' %}
      </div>
      <div class="col">
        {% include '@AutoAdminAdmin/_partials/_datatable_pagination.html.twig' %}
        {% include '@AutoAdminAdmin/entity/_partials/per_page.html.twig' %}
      </div>
    </div>
  {% endblock %}
{% endblock %}


{% block inner_page_bottom %}
  {{ block('table_utils') }}
{% endblock %}


{% block inner_page_content %}
  <form action="" class="multi-form-entity-table">
    <input type="hidden" name="sorting_order" value="{{ app.request.query.get('sorting_order') }}">
  </form>
  <div class="row">
    <div class="col-12">
      {% set entityFields = entityArray.head|default([]) %}
      <table class="table table-striped table-resizable entity-list-table">
        <thead>
        <tr>
          {% for key, headInfo in entityFields %}
            {% if key == 'group_actions' %}
              <th class="checkbox-field-header">
                <input type="checkbox" id="group-actions-checkbox-all" class="form-control">
              </th>
            {% else %}
              {% set colSortKey = 'sort_' ~ key|replace({'_tr.': '_tr_'}) %}
              <th class="entity-field-header{% if key == 'id' %} identifier-header-type{% elseif entityArray.body is not empty and ('fa-check' in entityArray.body[0][key] or 'fa-times' in entityArray.body[0][key]) %} boolean-header-type{% endif %}{% if key == 'actions' %} actions-table-header{% endif %}">
                <div class="d-flex flex-row">
                  <div class="entity-field-title">
                    <span>{{ headInfo.name }}</span>
                  </div>
                  {% if headInfo.sortable %}
                    <div class="entity-field-sort">
                      <form class="multi-form-entity-table sorting-widget">
                        <input type="hidden" name="{{ colSortKey }}" value="">
                        <i class="fa fa-long-arrow-down" data-value="ASC"></i>
                        <i class="fa fa-long-arrow-up" data-value="DESC"></i>
                      </form>
                    </div>
                  {% endif %}
                  {% if key == 'actions' %}
                    <div class="btn-group ml-auto">
                      <button
                          class="btn btn-sm table-header-btn header-search-row-toggler{% if entityArray.isSearching|default(false) %} active{% endif %}"
                          title="{{ 'entity.list.toggle_col_filters'|trans({}, 'aa_admin') }}"
                      ><i class="fa fa-filter"></i></button>
                    </div>
                  {% endif %}
                </div>
              </th>
            {% endif %}
          {% endfor %}
        </tr>
        <tr class="header-search-row{% if entityArray.isSearching|default(false) %} active{% endif %}">
          {% for key, searchInfo in entityArray.search|default([]) %}
            <th>
              {% if searchInfo.filterable %}
                {% if searchInfo.formView %}
                  {{ form(searchInfo.formView) }}
                {% endif %}
              {% elseif searchInfo.searchable %}
                <form class="multi-form-entity-table searching-widget searching-by-empty">
                  {% set colSearchKey = 'search_' ~ key|replace({'_tr.': '_tr_'}) %}
                  {% set colSearchKeyEmpty = 'search_' ~ key ~ '_empty'|replace({'_tr.': '_tr_'}) %}
                  <input
                    class="form-control"
                    type="search"
                    name="{{ colSearchKey }}"
                    value="{{ app.request.get(colSearchKey)|default('') }}"
                  >
                  <input
                    id="{{ colSearchKeyEmpty }}"
                    class="search-empty-input"
                    type="checkbox"
                    name="{{ colSearchKeyEmpty }}"
                    {% if app.request.get(colSearchKeyEmpty) %}checked{% endif %}
                  >
                  <label for="{{ colSearchKeyEmpty }}" class="search-empty-label" title="Filtrer les non renseignés">
                    <i class="fa fa-ban" aria-hidden="true"></i>
                  </label>
                </form>
              {% endif %}

              {% if loop.last %}
                <button
                  class="btn btn-sm btn-primary btn-icon-split submit-all-filters"
                  title="{{ 'entity.list.col_search_send'|trans({}, 'aa_admin') }}"
                >
                  <span class="icon">
                    <i class="fa fa-search"></i>
                  </span>
                </button>
                <button
                  class="btn btn-sm btn-icon-split btn-warning reset-all-filters"
                  title="{{ 'entity.list.col_search_reset_title'|trans({}, 'aa_admin') }}"
                >
                  <span class="icon">
                      <i class="fa fa-close"></i>
                  </span>
                </button>
              {% endif %}
            </th>
          {% endfor %}
        </tr>
        </thead>
        {% set entityBody = entityArray.body|default([]) %}
        <tbody>
        {% if entityBody|length == 0 %}
          <tr>
            <td colspan="{{ entityFields|length }}"
                class="text-center"
            >{{ 'entity.list.no_results'|trans({}, 'aa_admin') }}</td>
          </tr>
        {% else %}
          {% for entityRow in entityBody %}
            <tr>
              {% for key, entityValue in entityRow|default([]) %}
                {% if key == 'actions' %}
                  <td>
                    <div class="d-flex align-items-center p-1">
                      {% set principalActions = entityValue|slice(0, 4) %}
                      {% for action in principalActions %}
                        <a
                          href="{{ action.link }}"
                          class="btn btn-{{ action.buttonColor }} small-action-button {% if action.action == 'delete' %}prevent{% endif %}"
                          {% if action.action == 'delete' %}
                            data-prevent-content="{{ action.extra.prevent_content|default('') }}"
                          {% endif %}
                          title="{{ action.name }}"
                          {% if action.action == 'show' %}
                            target="_blank"
                          {% endif %}
                        >
                          <span class="text d-none">{{ action.name }}</span>
                          <i class="{{ action.icon }}"></i>
                        </a>
                      {% endfor %}

                      {% set extraActions = entityValue|slice(4) %}
                      {% if extraActions is not empty %}
                        <div class="dropleft">
                          <button type="button" class="btn btn-secondary" data-bs-toggle="dropdown">
                            <i class="far fa-ellipsis-vertical"></i>
                          </button>
                          <div class="dropdown-menu">
                            {% for action in extraActions %}
                              <a class="dropdown-item" href="{{ action.link }}">
                                <i class="{{ action.icon }}"></i> {{ action.name }}
                              </a>
                            {% endfor %}
                          </div>
                        </div>
                      {% endif %}
                    </div>
                  </td>
                {% else %}
                  <td>{{ entityValue|raw }}</td>
                {% endif %}
              {% endfor %}
            </tr>
          {% endfor %}
        {% endif %}
        </tbody>
      </table>
    </div>
  </div>
{% endblock %}