
{% set form_id = 
    form_id 
    ?? (
        (table.id ?? 'gestable-main')
        ~ '-form-filtre'
    ) 
%}
{% set colCount = table.listCol | length + ( table.action ? 1 :0 ) %}


{# TODO : drop the necessity for a <form> #}
{% set hasFilter = true %}
<{{ hasFilter ? 'form' : 'div' }} class="tab-content" method="post" 
    name="{{ form_id | e('html_attr') }}" 
    id="{{ form_id | e('html_attr') }}"
>
    {% block pre_form %}{% endblock %}
    {% if hasFilter %}
    <div class="resultat_filtre">
        {{ bullFiltre(post ?? [], form_id) }}
    </div>
    {% endif %}
    {% block pre_table %}{% endblock %}
	<table id="{{ table.id }}" class="table table-hover {{ table.class }}">
		<thead class="thead-dark">
{#______________________________________________________________________________
    Header #}
			<tr>
                {% for col in table.listCol %} 
                <th scope="col" {{ col.shrink ? 'width="1%"' }}>
                    {% if col.sortable %}
                    {{- triTable(
                        col.name,
                        post[ 'tri_' ~ col.name ],
                        col.label,
                        null,
                        null,
                        form_id
                    ) }}
                    {% else %}
                    {{ col.label }}
                    {% endif %}
                </th>
				{% endfor %}
                {% if table.action %}
                    {% block action_header %}
                    <th width="1%">{# Action #}</th>
                    {% endblock %}
                {% endif %}
			</tr>
		</thead>
		<tbody>
{#______________________________________________________________________________
    Search #}
            {% if table.listCol | reduce(
                (carry, col) => carry or col.filter is not empty,
                false
            ) %}
			<tr class="recherche">
                {% for col in table.listCol %}
                <td>
                {% if col.filter.type == 'text' %}
                    {{- filterTable(
                        col.name,
                        post[ 'filter_' ~ col.name ],
                        '',
                        form_id,
                        null,
                        form_id,
                        '',
                        col.label,
                        '',
                        true
                    ) }}
                {% elseif col.filter.type == 'zoom' %}
                    {{- zoomFilterSimple(
                        col.label,
                        col.name,
                        post[ 'filter_' ~ col.name ],
                        form_id,
                        col.filter.zoomPath,
                        col.filter.zoomPathParam ?? {}
                    ) -}}
                {% elseif col.filter.type == 'date' %}
                    {% include '@EtatStatistiques/Tableau/_gestable_filterDate.twig' with {
                        label: col.label,
                        name: 'filter_date_' ~ col.name,
                        post: post,
                        form_id: form_id,
                    } only %}
                {% elseif col.filter.type == 'select' %}
                    {# Note : we use '+' instead of merge for listevalue to prevent integer key deletion #}
                    {{- filterSelect(
                        col.name,
                        post[ 'filter_' ~ col.name ],
                        col.filter.listChoice 
                        ?? (
                            {'': ''}
                            + (
                                col.labelmap 
                                ?? (col.enum | enumToChoice)
                            )
                        ),
                        form_id,
                        '', '',
                        table.pagination.path,
                        '',
                        col.label
                    ) }}
                {% else %}
                {# - unknown filter - #}
                {% endif %}
                </td>
				{% endfor %}
                {% if table.action %}
                <td>{# Action #}</td>
                {% endif %}
			</tr>
            {% endif %}
			{% for item in table.listRow %}
                {% set rowId = 'maintable' ~ loop.index0 %}
                {% set prevItem = table.listRow[ loop.index0 -1 ] ?? null %}
{#______________________________________________________________________________
    Rupture #}
                {% for rupture in table.listRupture %}
                    {% set itemRuptureValue = item[ rupture.field ] %}
                    {% if itemRuptureValue != prevItem[ rupture.field ] %}
                    {% block rupture %}
                    <tr class="rupture{{ loop.index }}">
                        <td colspan="{{ colCount }}">
                            <div class="bs-row">
                                <div class="bs-col bold">
                                    {{ rupture.label }}
                                </div>
                                {% if rupture.labelTot %}
                                <div class="bs-col text-right">
                                    <strong>{{ rupture.labelTot }}</strong>
                                    {{ colon() }}
                                    {{ item[ rupture.fieldTot ] | prix }}
                                </div>
                                {% endif %}
                            </div>
                        </td>
                    </tr>
                    {% endblock rupture %}
                    {% endif %}
                {% endfor %}
{#______________________________________________________________________________
    Rows #}
                <tr id="{{ rowId }}" {% block tr_attr %}{% endblock %}>
                    {% for col in table.listCol %}
                        {% if block('cell_'~col.name) is defined %}
                            {{ block('cell_'~col.name) }}
                        {% else %}
                            <td class="{{
                                col.format in ['integer', 'amount', 'quantity'] ? 
                                    'text-right'
                            }} {{ 
                                col.format in ['list'] ? 
                                    'white-tooltip'
                            }}">
                                {% set value = item[ col.name ] %}
                                {% if col.format == 'date' %}
                                    {{- value | dateTrad -}}
                                {% elseif col.format == 'datetime' %}
                                    {{- value | datetime -}}
                                {% elseif col.format == 'datetime_date' %}
                                    {{- value | datetime_date -}}
                                {% elseif col.format == 'bool' %}
                                    {{- value | boolSql ? 'Oui' | trad : 'Non' | trad -}}
                                {% elseif col.format == 'amount' %}
                                    {{- value | prix -}}
                                    {{- col.amountDevise ? ' ' ~ item[ col.amountDevise ] -}}
                                {% elseif col.format == 'quantity' %}
                                {# TODO require UNIT #}
                                    {{- value | qte -}}
                                {% elseif col.format == 'entity' %}
                                    {# TODO : retrieve formatId and formatLabel from queryBuilder #}
                                    {{ entityLabel(value, col.formatId, col.formatLabel) }}
                                {% elseif col.format == 'list' %}
                                    {{ value is not empty ? entityLabel(value[0], col.formatId, col.formatLabel) }}
                                    {% if value | length > 1 %}
                                    <span class="text-muted">
                                        +{{- value | length - 1 }}&nbsp;{{ 'autres' | trad -}}
                                    </span>
                                    <span class="infos_tooltip text-left left" style="width: max-content">
                                        <ul>
                                            {% for entity in value  %}
                                                <li>{{ entityLabel(entity, col.formatId, col.formatLabel) }}
                                            {% endfor %}
                                        </ul>
                                    </span>
                                    {% endif %}
                                {% elseif col.format == 'enum' %}
                                    {{- value | enumToLabel(col.enum ?? null) -}}
                                {% elseif col.format == 'labelmap' %}
                                    {{- col.labelmap[ value ] -}}
                                {% else %}
                                    {{- value -}}
                                {% endif %}
                            </td>
                        {% endif %}
                        
                    {% endfor %}
                    {% if table.action %}
                        {% block action %}
                        <td class="parametrage">
                            {{ menuContextuel(rowId, false, item.menu) }}
                        </td>
                        {% endblock %}
                    {% endif %}
                </tr>
            {% else %}
                <tr>
                    <td colspan="{{ colCount }}" 
                        class="text-center text-muted"
                    >{{ 'Aucun élément' | trad }}</td>
                </tr>
            {% endfor %}
        </tbody>
    </table>
    {% block post_table %}{% endblock %}
    {% if table.pagination %}
    {{ paginations(
        table.pagination.path,
        table.pagination.nombreDePage,
        table.pagination.page,
        table.pagination.post,
        form_id
    ) }}
    {% endif %}
</{{ hasFilter ? 'form' : 'div' }}>
