{#
 # ---------------------------------------------------------------------
 #
 # GLPI - Gestionnaire Libre de Parc Informatique
 #
 # http://glpi-project.org
 #
 # @copyright 2015-2025 Teclib' and contributors.
 # @licence   https://www.gnu.org/licenses/gpl-3.0.html
 #
 # ---------------------------------------------------------------------
 #
 # LICENSE
 #
 # This file is part of GLPI.
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # the Free Software Foundation, either version 3 of the License, or
 # (at your option) any later version.
 #
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 #
 # ---------------------------------------------------------------------
 #}

{% import 'components/alerts_macros.html.twig' as alerts %}
{% set searchform_id = data['searchform_id']|default('search_' ~ rand) %}
{% set massive_action_form_id = massive_action_form_id ?? "" %}
{% set is_trashbin = data['search']['is_deleted'] %}

<div class="table-responsive-lg">
   <table
      class="search-results table card-table table-hover {{ is_trashbin ? "table-danger deleted-results" : "table-striped" }}"
      data-testid="{{ is_trashbin ? "search-results-trashbin" : "search-results" }}"
      id="{{ searchform_id }}"
   >
      <thead>
         <tr {% if count == 0 %}style="display: none;"{% endif %}>
            {% if showmassiveactions %}
            <th style="width: 30px;">
               <div>
                  <input class="form-check-input massive_action_checkbox" type="checkbox" id="checkall_{{ rand }}"
                        value="" aria-label="{{ __('Check all as') }}"
                        onclick="checkAsCheckboxes(this, '{{ searchform_id|e('js') }}', '.massive_action_checkbox');"
                        form="{{ massive_action_form_id }}" />
               </div>
            </th>
            {% endif %}

            {% set sorts = data['search']['sort'] %}
            {% if search_was_executed %}
            {% for col in data['data']['cols'] %}
               {# construct header link (for sorting) #}
               {% set linkto = '' %}
               {% set so_no_sort = col['searchopt']['nosort'] ?? false %}
               {% set meta = col['meta'] ?? false %}
               {% set sort_order = 'nosort' %}
               {% set sort_num = '' %}
               {% set can_sort = not meta and not no_sort and not so_no_sort %}
               {% if can_sort %}
                  {% for sort_opt in sorts %}
                     {% if sort_opt == col['id'] %}
                        {% set sort_order = data['search']['order'][loop.index0]|default('ASC') %}
                        {% set sort_num = loop.index %}
                     {% endif %}
                  {% endfor %}
               {% endif %}

               {% set col_name = col['name'] %}
               {# prefix by group name (corresponding to optgroup in dropdown) if exists #}
               {% if col['groupname'] is defined %}
                  {% set groupname = (col['groupname']['name'] ?? col['groupname']) %}
                  {% set col_name = __('%1$s - %2$s')|format(groupname, col['name']) %}
               {% endif %}

               {# Not main itemtype, prefix col_name by secondary itemtype #}
               {% if not itemtype == col['itemtype'] %}
                  {% set col_name = __('%1$s - %2$s')|format(col['itemtype']|itemtype_name, col_name) %}
               {% endif %}

               <th data-searchopt-id="{{ col['id'] }}" {% if not can_sort %}data-nosort="true"{% endif %} data-sort-order="{{ sort_order }}"
                  {% if sort_num is not empty %}data-sort-num="{{ sort_num - 1 }}"{% endif %}>
                  {% set sort_icon = sort_order == 'ASC' ? 'ti ti-caret-down-filled' : (sort_order == 'DESC' ? 'ti ti-caret-up-filled' : '') %}
                  {{ col_name }}
                  {% if can_sort %}
                     <span class="sort-indicator"><i class="{{ sort_icon }}"></i><span class="sort-num">{{ sorts|length > 1 ? sort_num : '' }}</span></span>
                  {% endif %}
               </th>
            {% endfor %}
            {% endif %}

            {# display itemtype in AllAssets #}
            {% if union_search_type[itemtype] is defined %}
               <th>
                  {{ __('Item type') }}
               </th>
            {% endif %}
         </tr>
      </thead>
      <tbody>

         {% if count == 0 %}
            {% if not search_was_executed %}
               {# Nothing to display #}
            {% else %}
               <tr>
                  <td colspan="{{ data['data']['cols']|length }}">
                     {% if search_error %}
                        {{ alerts.alert_danger(__('An error occurred during the search'), __('Consider changing the search criteria or adjusting the displayed columns.')) }}
                     {% else %}
                        {{ alerts.alert_info(__('No results found')) }}
                     {% endif %}
                  </td>
               </tr>
            {% endif %}
         {% else %}
            {% for rowkey, row in data['data']['rows'] %}
               <tr>
                  {% if showmassiveactions %}
                  <td>
                     <div>
                        {# disable massiveaction checkbox for some specific cases #}
                        {% if itemtype == 'Entity' and not has_access_to_entity(row['id']) %}
                        {% elseif itemtype == 'User' and not can_view_all_entities() and not has_access_to_user_entities(row['id']) %}
                        {% elseif item is instanceof('CommonDBTM') and item.maybeRecursive() and not has_access_to_entity(row['entities_id'])  %}
                        {% else %}
                           {% set row_itemtype = row['TYPE'] ?? itemtype %}
                           {% if call(row_itemtype ~ '::isMassiveActionAllowed', [row['id']]) %}
                              {% set checked = session('glpimassiveactionselected')[row_itemtype][row['id']] ?? false %}
                              <input class="form-check-input massive_action_checkbox" type="checkbox" data-glpicore-ma-tags="common"
                                 value="1" aria-label="{{ __("Select item") }}" {% if checked %}checked="checked"{% endif %}
                                 name="item[{{ row['TYPE'] ?? itemtype }}][{{ row['id'] }}]"
                                 form="{{ massive_action_form_id }}" />
                           {% endif %}
                        {% endif %}
                     </div>
                  </td>
                  {% endif %}

                  {% for col in data['data']['cols'] %}
                     {% set colkey = col['itemtype'] ~ '_' ~ col['id'] %}
                     {# showItem function returns "<td ...>...</td>" #}
                     {% if col['meta'] is defined and col['meta'] %}
                        {{ showItem(0, row[colkey]['displayname'], 0, 0) }}
                     {% else %}
                        {# Add a data-searchopt-content-id to each td to allow easy css selectors for a whole column #}
                        {{ showItem(0, row[colkey]['displayname'], 0, 0, "data-searchopt-content-id=\"" ~ col['id']|e ~ "\" " ~ call('Search::displayConfigItem', [itemtype, col['id'], row])) }}
                     {% endif %}
                  {% endfor %}

                  {# display itemtype in AllAssets #}
                  {% if union_search_type[itemtype] is defined %}
                     <td>
                        {{ row['TYPE']|itemtype_name }}
                     </td>
                  {% endif %}
               </tr>
            {% endfor %}
         {% endif %}
      </tbody>
   </table>
</div>
