{% extends 'events/display/conference/base.html' %}

{% from 'events/display/common/_conferences.html' import render_attachments %}
{% from 'events/display/indico/_common.html' import render_location, render_users, render_speakers %}
{% from 'events/papers/_contributions.html' import render_paper_section, render_editables_section, render_proceedings_section %}
{% from 'events/timetable/display/indico/_common.html' import render_references %}
{% from 'events/_affiliation.html' import render_affiliation %}
{% from 'message_box.html' import message_box %}

{% block page_class %}conference-page item-summary{% endblock %}

{% macro _render_field_value(field_value) -%}
    {% set field = field_value.contribution_field %}
    <tr>
        <th>{{ field.title }}</th>
        <td>
            {% if field.field_type == 'text' %}
                {{ field_value.data }}
            {% elif field.field_type == 'single_choice' %}
                {{ field_value.friendly_data }}
            {% endif %}
        </td>
    </tr>
{%- endmacro %}

{% macro render_author_list(authors, show_author_link) %}
    <div class="speaker-list">
        {% call(user_data) render_users(authors|sort(attribute='display_order_key'), separator='') %}
            <span itemprop="performers" itemscope itemtype="http://schema.org/Person" class="speaker-item icon-user">
                {% if user_data.contribution.event.show_titles and user_data.title -%}
                    <span class="speaker-title">
                        {{ user_data.title }}
                    </span>
                {%- endif %}
                {% if show_author_link %}
                    <a href="{{ url_for('contributions.display_author', user_data) }}">
                        <span>{{ user_data.display_full_name }}</span>
                    </a>
                {% else %}
                    <span>{{ user_data.display_full_name }}</span>
                {% endif %}
                {% if user_data.affiliation -%}
                    <span class="affiliation">
                        <span class="text">({{ render_affiliation(user_data) }})</span>
                    </span>
                {%- endif %}
            </span>
        {% endcall %}
    </div>
{% endmacro %}

{% block title -%}
    <span class="js-mathjax">
        {{ contribution.title }}
    </span>
{%- endblock %}

{% block info -%}
    <div class="flexrow f-j-space-between">
        <div>
            {% if contribution.code %}
                <div class="generic-data">
                    <i class="icon-tag header-data"></i>
                    <span>{{ contribution.code }}</span>
                </div>
            {% endif %}
            <div class="generic-data">
                <i class="icon-calendar header-data"></i>
                <span class="time-info">
                    {%- if contribution.start_dt_display %}
                        {{- contribution.start_dt_display|format_datetime(timezone=contribution.event.display_tzinfo) -}}
                    {%- else -%}
                        {%- trans %}Not scheduled{% endtrans -%}
                    {%- endif -%}
                </span>
            </div>
            {% if contribution.duration_display -%}
                <div class="generic-data">
                    <i class="icon-time header-data"></i>
                    <span class="time-info" title="{% trans %}Duration{% endtrans %}">
                        {{ contribution.duration_display|format_human_timedelta(narrow=true) }}
                    </span>
                </div>
            {%- endif %}
            {% if contribution.has_location_info %}
                <div class="generic-data">
                    {{ render_location(contribution, class='contribution-location') }}
                </div>
            {% endif %}
            {% if contribution.board_number -%}
                <div class="generic-data">
                    <i class="icon-price-tag header-data"></i>
                    <span class="board-info">
                        {% trans board_number=contribution.board_number %}Board: {{ board_number }}{% endtrans %}
                    </span>
                </div>
            {%- endif %}
        </div>
        <div>
            {% if contribution.type -%}
                <span class="contrib-type">
                    {{ contribution.type.name }}
                </span>
            {%- endif %}
            {% if contribution.track -%}
                <span class="icon-stack contrib-track"
                      title="{{ contribution.track.title_with_group if contribution.track.track_group }}">
                    {{ contribution.track.title }}
                </span>
            {%- endif %}
            {% if contribution.session -%}
                <span class="contrib-session" style="{{ contribution.session.colors.css }}">
                    <a href="{{ url_for('sessions.display_session', contribution.session) }}"
                       style="color: #{{ contribution.session.colors.text }}">{{ contribution.session.title }}</a>
                </span>
            {%- endif %}
        </div>
    </div>
{%- endblock %}

{% block page_actions -%}
    <div class="toolbar">
        {% if contribution.can_edit(session.user) -%}
            <button class="i-button icon-edit"
                    data-title
                    title="{% trans %}Edit contribution{% endtrans %}"
                    data-href="{{ url_for('contributions.manage_update_contrib', contribution, standalone=true) }}"
                    data-ajax-dialog
                    data-reload-after></button>
        {%- endif %}
        {{ template_hook('url-shortener', target=url_for('contributions.display_contribution', contribution),
                         classes='i-button icon-link') }}
        {% if indico_config.LATEX_ENABLED %}
            <a href="{{ url_for('contributions.export_pdf', contribution) }}" class="i-button icon-file-pdf" title="{% trans %}Export to PDF{% endtrans %}"></a>
        {% endif %}
        {% if contribution.timetable_entry -%}
            {% if g.static_site %}
                <a href="{{ url_for('contributions.export_ics', contribution) }}"
                   class="i-button icon-calendar" title="{% trans %}Export{% endtrans %}"></a>
            {% else %}
                <div id="contribution-calendar-link" data-event-id="{{ contribution.event_id }}"
                     data-contribution-id="{{ contribution.id }}"></div>
            {% endif %}
        {%- endif %}
    </div>
{%- endblock %}

{% block content -%}
    {%- if not published and contribution.event.can_manage(session.user, permission='contributions') and not contribution.is_user_associated(session.user) -%}
        {% call message_box('warning') -%}
            {% trans %}
                The contributions of this event have not been published yet. You can only access
                this page because you are an event manager.
            {% endtrans %}
        {%- endcall %}
    {%- endif -%}

    {% if contribution.speakers %}
        {{- render_speakers(contribution) -}}
    {% endif %}

    {% if contribution.description %}
        <section>
            <div class="header">
                <div class="header-row">
                    <h3>{% trans %}Description{% endtrans %}</h3>
                </div>
            </div>
            <div class="description js-mathjax">
                {{ contribution.description }}
            </div>
        </section>
    {% endif %}

    {% for field_value in field_values %}
        {% set field = field_value.contribution_field %}
        {% if field.field_type == 'text' and field.field_data.multiline and field_value.data and field.is_active %}
            <h3>{{ field.title }}</h3>
            <div id="field-{{ field.id }}">
                {{ field_value.data | markdown }}
            </div>
        {% endif %}
    {% endfor %}

    <table class="other-fields">
        {% for field_value in field_values|sort(attribute='contribution_field.position') %}
            {% set field = field_value.contribution_field %}
            {% if (field.field_type == 'single_choice' or not field.field_data.multiline) and field_value.data and field.is_active %}
                {{ _render_field_value(field_value) }}
            {% endif %}
        {% endfor %}
    </table>

    {% if contribution.primary_authors %}
        <section>
            <div class="header">
                <div class="header-row">
                    <h3>
                        {% trans count=contribution.primary_authors|length -%}
                            Author
                        {%- pluralize -%}
                            Authors
                        {%- endtrans %}
                    </h3>
                </div>
            </div>
            {{ render_author_list(contribution.primary_authors, show_author_link) }}
        </section>
    {% endif %}

    {% if contribution.secondary_authors %}
        <section>
            <div class="header">
                <div class="header-row">
                    <h3>
                        {% trans count=contribution.secondary_authors|length -%}
                            Co-author
                        {%- pluralize -%}
                            Co-authors
                        {%- endtrans %}
                    </h3>
                </div>
            </div>
            {{ render_author_list(contribution.secondary_authors, show_author_link) }}
        </section>
    {% endif %}

    {{ render_attachments(contribution) }}

    {% if contribution.references -%}
        <section>
            <div class="header">
                <div class="header-row">
                    <h3>{% trans %}External references{% endtrans %}</h3>
                </div>
            </div>
            {{ render_references(contribution, 'reference-list-conference') }}
        </section>
    {%- endif %}

    {% if contribution.subcontributions -%}
        <section>
            <div class="header">
                <div class="header-row">
                    <h3>{% trans %}Subcontributions{% endtrans %}</h3>
                </div>
            </div>
            <div class="i-box no-padding">
                <div class="i-box-content">
                    <ul class="group-list no-content-before with-buttons">
                        {% for subcontrib in contribution.subcontributions -%}
                            <li class="flexrow f-j-space-between">
                                <span class="text">
                                    <a class="js-mathjax"
                                       href="{{ url_for('contributions.display_subcontribution', subcontrib) }}">
                                        {{- subcontrib.title -}}
                                    </a>
                                </span>
                                {% if subcontrib.can_edit(session.user) -%}
                                    <span>
                                        <a href="#" class="icon-edit"
                                           data-href="{{ url_for('contributions.manage_edit_subcontrib', subcontrib, standalone=true) }}"
                                           data-title="{% trans title=subcontrib.title %}Edit subcontribution '{{ title }}'{% endtrans %}"
                                           title="{% trans %}Edit subcontribution{% endtrans %}"
                                           data-ajax-dialog
                                           data-reload-after></a>
                                    </span>
                                {%- endif %}
                            </li>
                        {%- endfor %}
                    </ul>
                </div>
            </div>
        </section>
    {%- endif %}
    {% if contribution.event.has_feature('papers') %}
        {{ render_paper_section(contribution) }}
    {% endif %}
    {% if contribution.event.has_feature('editing') %}
        {{ render_editables_section(contribution) }}
        {% if contribution.has_published_editables %}
            {{ render_proceedings_section(contribution) }}
        {% endif %}
        <script>
            setupEditableSubmissionButton();
        </script>
    {% endif %}
    <script>
        setupAttachmentTreeView();
    </script>
{%- endblock %}
