{% import "@core/multidelete-macro.twig" as multidelete %}
{{ infoRes|raw }} {# affiche le nombre de résultats #}

{# l'id doit être unique - dans le cas où il y a plusieurs tableaux sur la même page
, on utilise $params['nbbazarliste'] qui s'incrémente à chaque fois qu'on appelle bazarliste
pour différencier les différents tableaux table_1 table_2 #}

<table id="table_{{ params.nbbazarliste}}" class="table prevent-auto-init table-condensed display in-tableau-template" data-scroll-x="true">
  <thead>
    <tr>
      {# Column titles #}
          {% if displayadmincol %}
            {{ multidelete.insertSelectAll('bazar-list-'~ params.nbbazarliste,'top') }}
          {% endif %}
      {% for index,info in columnsInfo %}
        <th{{ (info.propertyName in sumFieldsIds) ? ' class="sum-activated"' : ''}}
          {% if columnswidth[info.propertyName] is not empty %}
            data-width="{{ columnswidth[info.propertyName] }}"
          {% elseif params.defaultcolumnwidth is not empty %}
            data-width="{{ params.defaultcolumnwidth }}"
          {% endif %}
          {% if info['not-visible'] %}
            data-visible="false"
          {% endif %}>{{ info.title|raw }}</th>
      {% endfor %}
      {% if displayadmincol %}
        <th class="not-export-this-col"></th>
      {% endif %}
    </tr>
  </thead>
  <tbody>
    {% for entry in entries %}
      <tr {{ entry.html_data|raw }}>
        {% if displayadmincol %}
          {{ multidelete.insertSelectLine(
              'bazar-list-'~ params.nbbazarliste,
              entry.id_fiche,
              entry.external-data is not empty or not(
                wiki.UserIsAdmin or wiki.UserIsOwner(entry.id_fiche))) }}
        {% endif %}
        {% set searchTitle = ("bf_titre" in columnsInfo|filter(
          info => info['not-visible'] is not defined or not info['not-visible'])|map(
            p => p.propertyName)) %}
        {% for info in columnsInfo %}
          {% set val = entry[info.propertyName] %}
          {# sanitize possible errors in values from checkbox #}
          {% set val = val is iterable ? val|filter(v => v == 1)|keys|join(',') : val %}
          {% set exportval = val %}
          {% if info.key is empty and displayvaluesinsteadofkeys and (optionsIfDisplayvaluesinsteadofkeys[info.propertyName] is not empty) %}
            {% if info.multivalues %}
              {% set exportval = val|split(',')|map( key => optionsIfDisplayvaluesinsteadofkeys[info.propertyName][key] 
                ? '"' ~ optionsIfDisplayvaluesinsteadofkeys[info.propertyName][key] ~ '"': key)|join(",") %}
              {% set val = val|split(',')|map( key => optionsIfDisplayvaluesinsteadofkeys[info.propertyName][key] 
                ? optionsIfDisplayvaluesinsteadofkeys[info.propertyName][key] : key)|join(",\n") %}  
            {% elseif optionsIfDisplayvaluesinsteadofkeys[info.propertyName][val] %}
              {% set val = optionsIfDisplayvaluesinsteadofkeys[info.propertyName][val] %}
              {% set exportval = val %}
            {% endif %}
          {% elseif info.key is empty and info.mapFieldId %}
            {% set val = entry[info.mapFieldId].latitude!='' and entry[info.mapFieldId].longitude!=''?'[' ~ entry[info.mapFieldId].latitude ~ ',' ~ entry[info.mapFieldId].longitude ~ ']'  : '' %}
            {% set exportval = val %}
          {% elseif info.key is not empty %}
            {% set val = (info.key in val|split(',')) ? 'X' : '' %}
            {% set exportval = val %}
          {% endif %}
          <td data-sort="{{ exportval }}">
            {% if loop.first %}
              <span class="bazar-entry" {{ entry.html_data|raw }}></span>
            {% endif %}
            {% if info.key is empty %}
              {% if (searchTitle and info.propertyName == "bf_titre") or 
                    (not searchTitle and loop.first and not info.mapFieldId) %}
                {% if colors[entry.id_fiche] is not empty %}<span class="pellet" style="background-color: {{ colors[entry.id_fiche] }} ;"></span>{% endif %}
                {% if icons[entry.id_fiche] is not empty %}<i class="{{ icons[entry.id_fiche] }}"></i>{% endif %}
                <a href="{{ entry.url }}" class="modalbox" data-size="modal-lg">
                  {{ val|nl2br }}
                </a>
              {% elseif info.mapFieldId %}
				  {{ val|nl2br }}
              {% elseif info.imageAsThumbnail and val is not empty %}
                {% set size = 100 %}
                {% set width = size*3//2 %}
                <div class="tableau-thumbnail" style="width:{{ width }}px;height:{{ size }}px">
                  <img loading="lazy" src="{{ urlImage({fileName:"files/#{val}",width:width,height:size,mode:"fit"})}}"/>
                </div>
              {% else %}
                {{ val|nl2br }}
              {% endif %}
            {% else %}
              {{ val }}
            {% endif %}
          </td>
        {% endfor %}
        {% if displayadmincol %}
          <td class="horizontal-admins-btn">
            {% if entry.external-data is empty %}
              {% if aclService.hasAccess('write',entry.id_fiche) %}
                <a href="{{ url({tag:entry.id_fiche,handler:'editiframe'}) }}"
                  data-toggle="tooltip"
                  data-placement="left"
                  class="btn btn-default btn-icon btn-xs modalbox"
                  data-size="modal-lg"
                  data-iframe="1"
                  title="{{ _t('MODIFY') }} {{ entry.id_fiche }}"
                  onclick="$(this).tooltip('hide');$(this).attr('title',$(this).data('original-title'));"
                  >
                  <i class="fas fa-pencil-alt"></i>
                </a>
              {% else %}
                <button disabled="disabled"
                  data-toggle="tooltip"
                  data-placement="left"
                  class="btn btn-default btn-icon btn-xs"
                  target="blank"
                  title="{{ _t('DENY_WRITE') }}"
                  >
                  <i class="fas fa-pencil-alt"></i>
                </buttom>
              {% endif %}
              {% if wiki.UserIsAdmin or wiki.UserIsOwner(entry.id_fiche) %}
                <a href="{{ url({tag:entry.id_fiche,handler:'deletepage',params:{incomingurl:url({handler:''})}}) }}"
                  data-toggle="tooltip"
                  data-placement="left"
                  class="btn btn-danger btn-icon btn-xs modalbox"
                  title="{{ _t('DELETE') }} {{ entry.id_fiche }}"
                  onclick="$(this).tooltip('hide');$(this).attr('title',$(this).data('original-title'));"
                  >
                  <i class="fas fa-trash"></i>
                </a>
              {% else %}
                <button disabled="disabled"
                  data-toggle="tooltip"
                  data-placement="left"
                  class="btn btn-danger btn-icon btn-xs"
                  title="{{ _t('DELETEPAGE_NOT_OWNER') }}"
                  >
                  <i class="fas fa-trash"></i>
                </buttom>
              {% endif %}
            {% endif %}
          </td>
        {% endif %}
      </tr>
    {% endfor %}
    {% if sumFieldsIds is not empty or displayadmincol %}
      {# sums by col #}
      <tfoot>
        <tr>
          {% if displayadmincol %}
            {{ multidelete.insertSelectAll('bazar-list-'~ params.nbbazarliste,'bottom') }}
          {% endif %}
          {% for index,info in columnsInfo %}
            <th>
              {% if sumFieldsIds is not empty %}
                {{ loop.first and (info.propertyName not in sumFieldsIds) ? _t('SUM'):''}}
              {% endif %}
            </th>
          {% endfor %}
          {% if displayadmincol %}
            <th class="not-export-this-col"></th>
          {% endif %}
        </tr>
      </tfoot>
    {% endif %}
  </tbody>
</table>
{% if displayadmincol %}
            {{ multidelete.insertButton('bazar-list-'~ params.nbbazarliste,'pages') }}
{% endif %}

{{ include_javascript('javascripts/vendor/datatables-full/jquery.dataTables.min.js') }}
{{ include_javascript('tools/bazar/presentation/javascripts/tableau.js') }}
{{ include_css('styles/vendor/datatables-full/dataTables.bootstrap.min.css') }}
{{ include_css('tools/bazar/presentation/styles/tableau.css') }}
