{#
/**
 * @file
 * Template for a generic table formatted by drupal's table element.
 */
#}
<table{{ attributes.addClass('kern-table', no_striping ? '' : 'kern-table--striped', sticky ? 'sticky-header') }}>
  {% if caption %}
    <caption class="kern-title">{{ caption }}</caption>
  {% endif %}

  {% for colgroup in colgroups %}
    {% if colgroup.cols %}
      <colgroup{{ colgroup.attributes }}>
        {% for col in colgroup.cols %}
          <col{{ col.attributes }} />
        {% endfor %}
      </colgroup>
    {% else %}
      <colgroup{{ colgroup.attributes }} />
    {% endif %}
  {% endfor %}

  {% if header %}
    <thead class="kern-table__head">
    <tr class="kern-table__row">
      {% for cell in header %}
        {% set column_classes = cell.default_classes ? [
          'views-field',
          'views-field-' ~ fields[key],
        ] : [] %}
        {% set cell_attributes = cell.attributes|default(create_attribute()) %}
        <{{ cell.tag ?? 'th' }}{{ cell_attributes.addClass('kern-table__header').addClass(column_classes) }}>

        {%- if cell.wrapper_element -%}
          <{{ cell.wrapper_element }}>
        {%- endif -%}
          {%- if cell.url -%}
            <a href="{{ cell.url }}" title="{{ cell.title }}" rel="nofollow">{{ cell.content }}{{ cell.sort_indicator }}</a>
          {%- else -%}
            {{ cell.content }}{{ cell.sort_indicator }}
          {%- endif -%}
        {%- if cell.wrapper_element -%}
          </{{ cell.wrapper_element }}>
        {%- endif -%}

        </{{ cell.tag ?? 'th' }}>
      {% endfor %}
    </tr>
    </thead>
  {% endif %}

  {% if rows %}
    <tbody class="kern-table__body">
    {% for row in rows %}
      {% set row_attributes = row.attributes|default(create_attribute()) %}
      <tr{{ row_attributes.addClass('kern-table__row') }}>
      {% for cell in row.cells %}
        {% set cell_attributes = cell.attributes|default(create_attribute()) %}
        {% set column_classes = cell.default_classes ? [
          'views-field',
          'views-field-' ~ fields[key],
        ] : [] %}
        <{{ cell.tag ?? 'td' }}{{ cell_attributes.addClass('kern-table__cell').addClass(column_classes) }}>

        {%- if cell.wrapper_element -%}
          <{{ cell.wrapper_element }}>
        {% endif %}
        {% if cell.content is iterable %}
          {% for content in cell.content %}
            {{- content.separator }}{{ content.field_output -}}
          {% endfor %}
        {% else %}
          {{- cell.content -}}
        {% endif %}
        {%- if cell.wrapper_element -%}
          </{{ cell.wrapper_element }}>
        {% endif %}

        </{{ cell.tag ?? 'td' }}>
      {% endfor %}
      </tr>
    {% endfor %}
    </tbody>
  {% elseif empty %}
    <tbody>
    <tr>
      <td colspan="{{ header_columns }}">{{ empty }}</td>
    </tr>
    </tbody>
  {% endif %}
  {% if footer %}
    <tfoot class="kern-table__footer">
    {% for row in footer %}
      {% set row_attributes = row.attributes|default(create_attribute()) %}
      <tr{{ row_attributes.addClass('kern-table__row') }}>
      {% for cell in row.cells %}
        {% set column_classes = cell.default_classes ? [
          'views-field',
          'views-field-' ~ fields[key],
        ] : [] %}
        {% set cell_attributes = cell.attributes|default(create_attribute()) %}
        <{{ cell.tag ?? 'td' }}{{ cell_attributes.addClass('kern-table__header').addClass('cell_attributes') }}>

        {%- if cell.wrapper_element -%}
          <{{ cell.wrapper_element }}>
        {% endif %}
        {% if cell.content is iterable %}
          {% for content in cell.content %}
            {{- content.separator }}{{ content.field_output -}}
          {% endfor %}
        {% else %}
          {{- cell.content -}}
        {% endif %}
        {%- if cell.wrapper_element -%}
          </{{ cell.wrapper_element }}>
        {% endif %}

        </{{ cell.tag ?? 'td' }}>
      {% endfor %}
      </tr>
    {% endfor %}
    </tfoot>
  {% endif %}
</table>
