{% extends config.KERKO_TEMPLATE_LAYOUT %}
{% from "kerko/_attributes.html.jinja2" import title_aria_label %}
{% from "kerko/_badges.html.jinja2" import badges %}
{% from "kerko/_facets.html.jinja2" import facet_items %}

{% macro field(label_text, value_text, field_class=None, value_class=None) %}
    {%- if value_text %}
        <div class="row mb-3 {{ field_class if field_class }}">
            <div class="col-md-4 col-lg-3 font-weight-bold break-word text-md-right text-print-left">{{ label_text }}</div>
            <div class="col-md-8 col-lg-9 break-word {{ value_class if value_class }}">{{ value_text }}</div>
        </div>
    {%- endif %}
{%- endmacro %}

{% macro facet_field(label_text, value_text, key, add_link_title=None) %}
    {#- Display a field using its facet, if available, otherwise fallback to a regular field display. #}
    {%- if key in config.KERKO_COMPOSER.facets and key in item.facet_results %}
        {%- set f = config.KERKO_COMPOSER.facets[key] %}
        {#- We have a matching facet, display its link. #}
        {{- field(
                f.title,
                facet_items(
                    f.missing_label,
                    item.facet_results[f.key],
                    expand=True,
                    add_link_icon='',
                    remove_link_icon='',
                    list_tag='div',
                    item_tag='div',
                    add_link_title=add_link_title
                )
            )
        }}
    {%- else %}
        {#- There is matching facet, just display its value. #}
        {{- field(label_text, value_text) }}
    {%- endif %}
{%- endmacro %}

{% macro permalink(url) -%}
    <a rel="bookmark" title="{{ _('Permanent link to this bibliographic record') }}" href="{{ url }}">{{ url }}</a>
{%- endmacro -%}

{%- block metas %}
    {{- super() }}
    <link rel="canonical" title="{{ title|escape }}" href="{{ item_url }}">
{%- endblock metas %}

{%- block content_inner %}
    <div class="mb-4 item-content">
        {%- if item.attachments or config.KERKO_PRINT_ITEM_LINK or config.KERKO_COMPOSER.citation_formats %}
            <div class="row">
                <div class="col-auto ml-auto text-right">
                    {%- if item.attachments|length > 1 %}
                        <div class="{% block attachment_links_wrapper_classes %}d-inline-block d-print-none mb-2{% endblock %}">
                            <div class="dropdown">
                                <button class="{% block attachment_links_btn_classes %}btn btn-primary dropdown-toggle{% endblock %}" type="button" id="attachment-options" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                    <span class="fas fa-file" aria-hidden="true"></span> {{ _('Read documents') }}
                                </button>
                                <div class="dropdown-menu dropdown-menu-right dropdown-width-300" aria-labelledby="attachment-options">
                                    {%- for attachment in item.attachments %}
                                        <a class="dropdown-item" href="{{ url_for('kerko.item_attachment_download', item_id=item.id, attachment_id=attachment['id'], attachment_filename=attachment['filename']) }}"{% if config.KERKO_DOWNLOAD_ATTACHMENT_NEW_WINDOW %} target="_blank"{% endif %}>
                                            {{- attachment.filename|escape }}
                                        </a>
                                    {%- endfor %}
                                </div>
                            </div>
                        </div>
                    {%- elif item.attachments|length == 1 %}
                        <div class="{% block attachment_link_wrapper_classes %}d-inline-block d-print-none mb-2{% endblock %}">
                            <a class="{% block attachment_link_btn_classes %}btn btn-primary{% endblock %}" href="{{ url_for('kerko.item_attachment_download', item_id=item.id, attachment_id=item.attachments[0]['id'], attachment_filename=item.attachments[0]['filename']) }}"{% if config.KERKO_DOWNLOAD_ATTACHMENT_NEW_WINDOW %} target="_blank"{% endif %} {{ title_aria_label(_('Read \'{}\'').format(item.attachments[0]['filename']|escape)) }}>
                                <span class="fas fa-file" aria-hidden="true"></span> {{ _('Read document') }}
                            </a>
                        </div>
                    {%- endif %}
                    {%- if config.KERKO_PRINT_ITEM_LINK %}
                        <div class="{% block print_link_wrapper_classes %}d-none d-md-inline-block d-print-none mb-2{% endblock %}">
                            <button id="print-link" class="{% block print_btn_classes %}btn btn-light{% endblock %}" type="button">
                                <span class="fas fa-print" aria-hidden="true"></span> {% trans count=1, count_formatted='1' %}Print this citation{% pluralize %}Print {{ count_formatted }} citations{% endtrans %} {# TODO: Remove pluralization after solving issue where translation is not loaded when the same message is sometimes pluralized, sometimes not. #}
                            </button>
                        </div>
                    {%- endif %}
                    {%- if config.KERKO_COMPOSER.citation_formats %}
                        <div class="{% block download_link_wrapper_classes %}d-inline-block d-print-none mb-2{% endblock %}">
                            <div class="dropdown">
                                <button class="{% block download_btn_classes %}btn btn-secondary btn-light dropdown-toggle{% endblock %}" type="button" id="download-options" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                    <span class="fas fa-download" aria-hidden="true"></span> {% trans count=1, count_formatted='1' %}Download this citation{% pluralize %}Download {{ count_formatted }} citations{% endtrans %} {# TODO: Remove pluralization after solving issue where translation is not loaded when the same message is sometimes pluralized, sometimes not. #}
                                </button>
                                <div class="dropdown-menu dropdown-menu-right dropdown-width-300" aria-labelledby="download-options">
                                    {%- for citation_format in config.KERKO_COMPOSER.get_ordered_specs('citation_formats') %}
                                        <a class="dropdown-item" href="{{ url_for('kerko.item_citation_download', item_id=item.id, citation_format_key=citation_format.key) }}" rel="nofollow" {{ title_aria_label(_('Download in {download_option} format').format(download_option=citation_format.label|escape)) }}>
                                            {{- citation_format.label -}}
                                        </a>
                                        <p class="px-4 text-muted">{{ citation_format.help_text }}</p>
                                    {%- endfor %}
                                </div>
                            </div>
                        </div>
                    {%- endif %}
                </div>
            </div>
        {%- endif %}

        {{- facet_field(_("Resource type"), item.item_type, 'facet_item_type', add_link_title=_("Search the '{}' resource type").format(item.item_type)) }}
        {%- if item.data.creators %}
            <div class="row">
                <div class="col-md-4 col-lg-3 font-weight-bold break-word text-md-right text-print-left">{% trans count=item.data.creators|length %}Author/contributor{% pluralize %}Authors/contributors{% endtrans %}</div>
                <div class="col-md-8 col-lg-9 break-word">
                    <ul class="list-unstyled">
                        {%- for creator in item.data.creators %}
                            <li>
                                {%- if creator.url %}<a href="{{ creator.url }}" {{ title_aria_label(_('Search \'{}\'').format(creator.display|escape)) }}>{% endif %}
                                {{- creator.display|escape }}
                                {%- if creator.url %}</a>{% endif %}
                                {%- if creator.label %}
                                    ({{ creator.label }})
                                {%- endif %}
                            </li>
                        {%- endfor %}
                    </ul>
                </div>
            </div>
        {%- endif %}
        {#- Display item fields in proper order. #}
        {%- for f in item.item_fields %}
            {%- if item.data[f.field] %}
                {%- if f.field == 'accessDate' %}
                    {{- field(f.localized, item.data[f.field]|kerko_reformat_date(convert_tz=True)) }}
                {%- elif f.field in ['date', 'dateDecided', 'dateEnacted', 'filingDate', 'issueDate'] %}
                    {{- field(f.localized, item.data[f.field]|kerko_reformat_date) }}
                {%- else %}
                    {{- field(f.localized, item.data[f.field]|escape|urlize(target='_blank'), value_class='pre-line' if f.field == 'abstractNote' or f.field == 'extra') }}
                {%- endif %}
            {%- endif %}
        {%- endfor %}
        {%- if item.links %}
            <div class="row">
                <div class="col-md-4 col-lg-3 font-weight-bold break-word text-md-right text-print-left">{% trans count=item.links|length %}Link{% pluralize %}Links{% endtrans %}</div>
                <div class="col-md-8 col-lg-9 break-word">
                    <ul class="list-unstyled">
                        {%- for link in item.links %}
                            <li><a href="{{ link.url }}" target="_blank">{{ link.title|escape }}</a></li>
                        {%- endfor %}
                    </ul>
                </div>
            </div>
        {%- endif %}
        {{- field(_("Notes"), item.notes|join('')|safe) if item.notes }}
        {{- field(_("Citation"), ('<div class="card"><div class="card-body">' + item.bib + '</div></div>')|safe) }}
        {%- if item.facet_results %}
            {%- for f in config.KERKO_COMPOSER.get_ordered_specs('facets') if f.item_view and f.key in item.facet_results %}
                {{ f.render(item.facet_results[f.key], 'field') }}
            {%- endfor %}
        {%- endif %}
        {%- if item.attachments %}
            <div class="row">
                <div class="col-md-4 col-lg-3 font-weight-bold break-word text-md-right text-print-left">{% trans count=item.attachments|length %}Document{% pluralize %}Documents{% endtrans %}</div>
                <div class="col-md-8 col-lg-9 break-word">
                    <ul class="list-unstyled">
                        {%- for attachment in item.attachments %}
                            <li><a href="{{ url_for('kerko.item_attachment_download', item_id=item.id, attachment_id=attachment['id'], attachment_filename=attachment['filename']) }}"{% if config.KERKO_DOWNLOAD_ATTACHMENT_NEW_WINDOW %} target="_blank"{% endif %}><span class="fas fa-file d-print-none no-decorate mr-1" aria-hidden="true"></span>{{ attachment.filename|escape }}</a></li>
                        {%- endfor %}
                    </ul>
                </div>
            </div>
        {%- endif %}
        {{- field(_("Link to this record"), permalink(item_url)) }}
        {#- Determine which relation tab is to be initially active, if any. #}
        {%- set relations_ns = namespace(active_tab=None) %}
        {%- for rel in config.KERKO_COMPOSER.get_ordered_specs('relations') %}
            {%- if not relations_ns.active_tab %}
                {%- if item[rel.field.key] %}
                    {%- set relations_ns.active_tab = rel.key %}
                {%- elif rel.reverse and item[rel.reverse_field_key] %}
                    {%- set relations_ns.active_tab = rel.reverse_key %}
                {%- endif %}
            {%- endif %}
        {%- endfor %}
        {%- if relations_ns.active_tab %}
            <div class="row">
                <div class="col-md-4 col-lg-3 font-weight-bold break-word text-md-right mb-2 mt-md-2 d-print-none">{{ _('Relations') }}</div>
                <div class="col-md-8 col-lg-9 break-word">
                    <ul class="nav nav-tabs d-print-none" role="tablist">
                        {%- for rel in config.KERKO_COMPOSER.get_ordered_specs('relations') %}
                            {%- if item[rel.field.key] %}
                                <li class="nav-item" role="tab">
                                    <a id="tab-{{ rel.key }}" href="#{{ rel.key }}" class="nav-link {% if rel.key == relations_ns.active_tab %}active{% endif %}" data-toggle="tab" aria-controls="{{ rel.key }}" {% if rel.key == relations_ns.active_tab %}aria-selected="true"{% endif %}>{{ rel.label }}</a>
                                </li>
                            {%- endif %}
                            {%- if rel.reverse and item[rel.reverse_field_key] %}
                                <li class="nav-item" role="tab">
                                    <a id="tab-{{ rel.reverse_key }}" href="#{{ rel.reverse_key }}" class="nav-link {% if rel.reverse_key == relations_ns.active_tab %}active{% endif %}" data-toggle="tab" aria-controls="{{ rel.reverse_key }}" {% if rel.reverse_key == relations_ns.active_tab %}aria-selected="true"{% endif %}>{{ rel.reverse_label }}</a>
                                </li>
                            {%- endif %}
                        {%- endfor %}
                    </ul>
                    <div class="tab-content">
                        {%- for rel in config.KERKO_COMPOSER.get_ordered_specs('relations') %}
                            {%- if item[rel.field.key] %}
                                {%- with key=rel.key, field=rel.field.key, label=rel.label, is_active=rel.key == relations_ns.active_tab %}
                                    {%- include "kerko/_item-relations.html.jinja2" %}
                                {%- endwith %}
                            {%- endif %}
                            {%- if rel.reverse and item[rel.reverse_field_key] %}
                                {%- with key=rel.reverse_key, field=rel.reverse_field_key, label=rel.reverse_label, is_active=rel.reverse_key == relations_ns.active_tab %}
                                    {%- include "kerko/_item-relations.html.jinja2" %}
                                {%- endwith %}
                            {%- endif %}
                        {%- endfor %}
                    </div>
                </div>
            </div>
        {%- endif %}
    </div>
    {{- item.coins|safe }}
{%- endblock content_inner %}

{%- block content_footer %}
    {%- if config.DEBUG %}
        <div class="text-right mb-4 mt-2 text-muted">{% trans time="%.2f"|format(time) %}Processing time: {{time}} seconds{% endtrans %}</div>
    {%- endif %}
{%- endblock content_footer %}

{%- block content_header %}
    <h1 class="{% block item_heading_classes %}h2 my-2 clearfix{% endblock %}">
        {{- badges(item, mode='item') -}}
        {{- title|escape -}}
    </h1>
{%- endblock content_header %}

{%- block body_scripts %}
    {{- super() }}
    {%- block kerko_item_js %}
        <script src="{{ url_for('kerko.static', filename='kerko/js/item.js') }}?202008101934"></script>
    {%- endblock kerko_item_js %}
    {%- block kerko_print_js %}
        {%- if config.KERKO_PRINT_ITEM_LINK %}
            <script src="{{ url_for('kerko.static', filename='kerko/js/print.js') }}"></script>
        {%- endif %}
    {%- endblock kerko_print_js %}
{%- endblock body_scripts %}
