{#
  Copyright (C) 2020-2021 CERN.
  Copyright (C) 2020-2021 Northwestern University.
  Copyright (C) 2021 New York University.

  Invenio RDM Records is free software; you can redistribute it and/or modify
  it under the terms of the MIT License; see LICENSE file for more details.
#}


{%- from "invenio_app_rdm/records/macros/detail.html" import show_section_custom_fields, show_references, show_add_titles,
show_alternate_identifiers, show_related_identifiers, show_funding, show_dates %}

{% set hasContent = record.ui.additional_titles or
                    metadata.identifiers or
                    record.ui.related_identifiers or
                    metadata.funding or
                    record.ui.dates or
                    metadata.references or
                    record.custom_fields
%}
{% set rights = record.ui.get('rights') %}
{% set funding = record.ui.get('funding') %}
{% set custom_fields_per_section = custom_fields_ui | groupby('section') %}
{% set active = true %}

{% if hasContent %}
  <div class="ui divider"></div>

  <h2 id="record-details-heading">{{ _('Additional details') }}</h2>

  <div class="rdm-tab-container ml-0-mobile mr-0-mobile">
    {# Tabs #}
    <div role="tablist" class="ui top attached tabs menu rdm-tab-menu">
      {% if record.ui.additional_titles %}
        <a
          role="tab"
          aria-selected="{{ active }}"
          aria-controls="additional-titles-tab-panel"
          data-tab="additional-titles"
          tabindex="0"
          class="{{ 'active' if active }} item"
          id="additional-titles-tab"
        >
          {{ _('Additional titles') }}
        </a>
        {% set active = false %}
      {% endif %}


      {% if metadata.identifiers %}
        <a
          role="tab"
          aria-selected="{{ active }}"
          aria-controls="identifiers-tab-panel"
          data-tab="identifiers"
          tabindex="0"
          class="{{ 'active' if active }} item"
          id="identifiers-tab"
        >
          {{ _('Identifiers') }}
        </a>
        {% set active = false %}
      {% endif %}

      {% if record.ui.related_identifiers %}
        <a
          role="tab"
          aria-selected="{{ active }}"
          aria-controls="related-works-tab-panel"
          data-tab="related-works"
          tabindex="0"
          class="{{ 'active' if active }} item"
          id="related-works-tab"
        >
          {{ _('Related works') }}
        </a>
        {% set active = false %}
      {% endif %}

      {% if metadata.funding %}
        <a
          role="tab"
          aria-selected="{{ active }}"
          aria-controls="funding-tab-panel"
          data-tab="funding"
          tabindex="0"
          class="{{ 'active' if active }} item"
          id="funding-tab"
        >
          {{ _('Funding') }}
        </a>
        {% set active = false %}
      {% endif %}

      {% if record.ui.dates %}
        <a
          role="tab"
          aria-selected="{{ active }}"
          aria-controls="dates-tab-panel"
          data-tab="dates"
          tabindex="0"
          class="{{ 'active' if active }} item"
          id="dates-tab"
        >
          {{ _('Dates') }}
        </a>
        {% set active = false %}
      {% endif %}

      {% if metadata.references %}
        <a
          role="tab"
          aria-selected="{{ active }}"
          aria-controls="references-tab-panel"
          data-tab="references"
          tabindex="0"
          class="{{ 'active' if active }} item"
          id="references-tab"
        >
          {{ _('References') }}
        </a>
      {% endif %}
      {% if record.custom_fields %}
        {%- for section, fields in custom_fields_per_section %}
          {% set section_format = section | replace(" ", "-") %}
          <a
            role="tab"
            aria-selected="{{ active }}"
            aria-controls="{{section_format}}-tab-panel"
            data-tab="{{section_format}}"
            tabindex="0"
            class="{{ 'active' if active }} item"
            id="{{section_format}}-tab"
          >
          {{ section }}
          </a>
        {%- endfor %}
      {% endif %}
    </div>

    {# Content #}
    {% set active = true %}

    {% if record.ui.additional_titles %}
      <div
          role="tabpanel"
          class="ui bottom attached {{ 'active' if active }} tab segment"
          data-tab="additional-titles"
          aria-labelledby="additional-titles-tab"
          id="additional-titles-tab-panel"
          hidden="{{ not active }}"

      >
        <dl class="details-list">
          {{ show_add_titles(record.ui.additional_titles) }}
        </dl>
      </div>
      {% set active = false %}
    {% endif %}

    {% if metadata.identifiers %}
      <div
          role="tabpanel"
          class="ui bottom attached {{ 'active' if active }} tab segment"
          data-tab="identifiers"
          aria-labelledby="identifiers-tab"
          id="identifiers-tab-panel"
          hidden="{{ not active }}"
      >
        <dl class="details-list">
          {{ show_alternate_identifiers(metadata.identifiers) }}
        </dl>
      </div>
      {% set active = false %}
    {% endif %}

    {% if record.ui.related_identifiers %}
      <div
          role="tabpanel"
          class="ui bottom attached {{ 'active' if active }} tab segment"
          data-tab="related-works"
          aria-labelledby="related-works-tab"
          id="related-works-tab-panel"
          hidden="{{ not active }}"
      >
        <dl class="details-list">
          {{ show_related_identifiers(record.ui.related_identifiers) }}
        </dl>
      </div>
      {% set active = false %}
    {% endif %}

    {% if funding %}
      <div
          role="tabpanel"
          class="ui bottom attached {{ 'active' if active }} tab segment"
          data-tab="funding"
          aria-labelledby="funding-tab"
          id="funding-tab-panel"
          hidden="{{ not active }}"
      >
        <dl class="details-list">
          {{ show_funding(funding) }}
        </dl>
      </div>
      {% set active = false %}
    {% endif %}

    {% if record.ui.dates %}
      <div
          role="tabpanel"
          class="ui bottom attached {{ 'active' if active }} tab segment"
          data-tab="dates"
          aria-labelledby="dates-tab"
          id="dates-tab-panel"
          hidden="{{ not active }}"
      >
        <dl class="details-list">
          {{ show_dates(record.ui.dates) }}
        </dl>
      </div>
      {% set active = false %}
    {% endif %}

    {% if metadata.references %}
      <div
          role="tabpanel"
          class="ui bottom attached {{ 'active' if active }} tab segment"
          data-tab="references"
          aria-labelledby="references-tab"
          id="references-tab-panel"
          hidden="{{ not active }}"
      >
        <dl class="details-list">
          {{ show_references(metadata.references) }}
        </dl>
      </div>
    {% endif %}

    {% if record.custom_fields %}
        {%- for section_cfg in custom_fields_ui %}
          {% set section_format = section_cfg.section | replace(" ", "-") %}
          <div
            role="tabpanel"
            class="ui bottom attached {{ 'active' if active }} tab segment"
            data-tab="{{section_format}}"
            aria-labelledby="{{section_format}}-tab"
            id="{{section_format}}-tab-panel"
            hidden="{{ not active }}"
          >
            <dl class="details-list">
              {{ show_section_custom_fields(record.ui.custom_fields, section_cfg.fields) }}
            </dl>
          </div>
        {%- endfor %}
    {% endif %}
  </div>
{% endif %}
