{% macro render_registration_info(registration, from_management=false) %}
    {{ template_hook('before-render-registration-info', registration=registration, from_management=from_management) }}
    {% for section in registration.sections_with_answered_fields if section.is_visible_in_summary(from_management) -%}
        <tr class="regform-done-title">
            <td colspan="2">{{ section.title }}</td>
        </tr>
        {% for field in section.children if (field.is_field and field.id in registration.data_by_field and
                                             registration.is_field_shown(field) and
                                             (from_management or not field.is_deleted)) -%}
            <tr class="{{ 'deleted-field' if from_management and (field.is_deleted or section.is_deleted) }}">
                <th class="regform-done-caption">{{ field.title }}</th>
                <td class="regform-done-data">
                    {%- if not field.is_purged -%}
                        {{- render_data(registration, field, from_management) -}}
                    {%- endif -%}
                    {% if from_management and (field.is_deleted or section.is_deleted) -%}
                        <span class="icon-warning deleted-field-warning right"
                              data-qtip-style="warning"
                              title="{% trans %}This field no longer exists{% endtrans %}">
                        </span>
                    {% endif %}
                    {% if field.is_purged -%}
                        <span class="icon-warning purged-field-warning right"
                              data-qtip-style="warning"
                              title="{% trans %}The field data has been purged due to an expired retention period{% endtrans %}">
                        </span>
                    {% endif %}
                </td>
            </tr>
        {%- endfor %}
    {%- endfor %}
    <tr class="regform-done-title">
        <td colspan="2">{% trans %}Privacy options{% endtrans %}</td>
    </tr>
    <tr>
        <th class="regform-done-caption">{% trans %}Visibility{% endtrans %}</th>
        <td class="regform-done-data ui form field">
            {% if from_management %}
                <div class="regform-participant-visibility">
                    {% if registration.visibility == registration.visibility_before_override %}
                        <div>{{ registration.visibility_before_override.title }}</div>
                    {% else %}
                        <div class="old-visibility">{{ registration.visibility_before_override.title }}</div>
                        <div>{{ registration.visibility.title }}</div>
                    {% endif %}
                    {% set tooltip_text -%}
                        {%- if registration.participant_hidden -%}
                            {% trans %}This will restore this participant's visibility based on the form's settings{% endtrans %}
                        {%- else -%}
                            {% trans %}This will hide this participant from the list of participants regardless of other settings{% endtrans %}
                        {%- endif -%}
                    {%- endset %}
                    <button class="i-button {% if registration.participant_hidden %}icon-eye{% else %}icon-eye-blocked{% endif %}"
                            data-update="#registration-details"
                            data-method="POST"
                            data-href="{{ url_for('.hide_registration', registration) }}"
                            title="{{ tooltip_text }}">
                        {%- if registration.participant_hidden -%}
                            {% trans %}Show participant{% endtrans %}
                        {%- else -%}
                            {% trans %}Hide participant{% endtrans %}
                        {%- endif -%}
                    </button>
                </div>
            {% else %}
                <div id="registration-summary-consent-to-publish"
                     data-locator="{{ registration.locator.registrant|tojson|forceescape }}"
                     data-publish-to-participants="{{ registration.registration_form.publish_registrations_participants.name }}"
                     data-publish-to-public="{{ registration.registration_form.publish_registrations_public.name }}"
                     data-initial-consent-to-publish="{{ registration.consent_to_publish.name }}"></div>
            {% endif %}
        </td>
    </tr>
{% endmacro %}

{% macro render_data(registration, field, from_management=false) %}
    {% set data = registration.data_by_field -%}
    {% set friendly_data = data[field.id].friendly_data if field.id in data else '' -%}
    {% if field.input_type == 'accommodation' and friendly_data %}
        {% if not friendly_data.is_no_accommodation %}
            <div>
                {% trans arrival_date=friendly_data.arrival_date | format_date -%}
                    <strong>Arrival:</strong> {{ arrival_date }}
                {%- endtrans %}
            </div>
            <div>
                {% trans departure_date=friendly_data.departure_date | format_date -%}
                    <strong>Departure:</strong> {{ departure_date }}
                {%- endtrans %}
            </div>
        {% endif %}
        <div>
            {% trans accommodation=friendly_data.choice -%}
                <strong>Accommodation:</strong> {{ accommodation }}
            {%- endtrans %}
        </div>
    {% elif field.input_type in ('multi_choice', 'sessions') %}
        <ul>
            {% for item in friendly_data -%}
                <li>{{ item }}</li>
            {%- endfor %}
        </ul>
    {% elif field.input_type == 'file' and friendly_data and from_management %}
        <a href="{{ url_for('.manage_registration_file', data[field.id].locator.file) }}">{{ friendly_data }}</a>
    {% elif field.input_type == 'picture' and friendly_data %}
        {% set picture_url = url_for('.manage_registration_file', data[field.id].locator.file) if from_management else
                             url_for('.registration_picture', data[field.id].locator.registrant_file) %}
        <img class="picture-preview" src="{{ picture_url }}" alt="{{ friendly_data }}">
    {% elif friendly_data is not none and friendly_data != '' %}
        {{- field.field_impl.render_summary_data(data[field.id]) -}}
    {% endif %}
{% endmacro %}

{% macro render_registration_summary(registration, from_management=false) %}
    <div id="registration-summary" class="regform-done" data-bypass-target="{% trans %}Skip to registration summary{% endtrans %}">
        <div class="i-box-header">
            <div class="i-box-title">
                {% trans %}Summary{% endtrans %}
            </div>
            <div class="i-box-metadata">
                <span class="label">
                    {% trans %}Reference:{% endtrans %}
                </span>
                <span class="content">
                    #{{ registration.friendly_id }}
                </span>
                <span class="label">
                    {% trans %}Date:{% endtrans %}
                </span>
                <span class="content">
                    {{ registration.submitted_dt|format_date }}
                </span>
            </div>
        </div>
        <div class="i-box-content">
            <table class="registration-info">
                {{ render_registration_info(registration, from_management) }}
            </table>
        </div>
    </div>
    <div id="receipts-list">
        {{ render_receipts_list(registration, from_management) }}
    </div>
{% endmacro %}

{% macro render_receipts_list(registration, from_management=false) %}
    {% set receipts = registration.receipt_files if from_management else registration.published_receipts %}
    {% set can_generate_receipts = from_management and registration.event.has_receipt_templates() %}

    {% if receipts or can_generate_receipts %}
        <div class="regform-done">
            <div class="i-box-header">
                <div class="i-box-title">
                    {% trans %}Documents{% endtrans %}
                </div>
                {% if can_generate_receipts %}
                    <a href="#" class="i-button accept icon-agreement"
                        data-callback="printReceipts"
                        data-params="{{ {'registration_id': [registration.id|string],
                                         'event_id': registration.registration_form.event.id,
                                         'reload_after': true} | tojson | forceescape }}">
                        {% trans %}Generate document{% endtrans %}
                    </a>
                {% endif %}
            </div>
            <div class="i-box-content">
                {%- if receipts %}
                    <table class="registration-info">
                        {% for receipt in receipts | sort(attribute='file.filename') %}
                            {% set filename = receipt.file.filename %}
                            <tr class="{{ 'unpublished-receipt' if not receipt.is_published }}">
                                <th class="regform-done-caption">{{ receipt.template.type.title if receipt.template.type else '' }}</th>
                                <td class="regform-done-data">
                                    <a href="{{ url_for('.download_receipt', receipt.locator.filename) if from_management else receipt.registrant_download_url }}">
                                        {{ filename }}
                                    </a>
                                    {% if not receipt.is_published -%}
                                        <span class="unpublished-receipt-warning">({% trans %}not published{% endtrans %})</span>
                                    {% endif %}
                                </td>
                                {% if from_management -%}
                                    <td class="actions-column">
                                        <div class="group right entry-action-buttons">
                                            {% if receipt.is_published %}
                                                <a href="#" class="icon-eye-blocked"
                                                   title="{% trans %}Unpublish document{% endtrans %}"
                                                   data-title="{% trans name=receipt.file.filename %}Unpublish '{{ name }}' to the registrant{% endtrans %}"
                                                   data-href="{{ url_for('.unpublish_receipt', receipt) }}"
                                                   data-method="POST"
                                                   data-update="#receipts-list"
                                                   data-confirm="{% trans name=filename %}Are you sure you want to unpublish the document '{{ name }}' to the registrant?{% endtrans %}"></a>
                                            {% else %}
                                                <a href="#" class="icon-eye js-dialog-action"
                                                   title="{% trans %}Publish document{% endtrans %}"
                                                   data-title="{% trans name=receipt.file.filename %}Publish '{{ name }}' to the registrant{% endtrans %}"
                                                   data-href="{{ url_for('.publish_receipt', receipt) }}"
                                                   data-update="#receipts-list"
                                                   data-ajax-dialog></a>
                                            {% endif %}
                                            <a href="#" class="icon-remove js-delete"
                                               data-title="{% trans name=receipt.file.filename %}Delete the document '{{ name }}'?{% endtrans %}"
                                               title="{% trans %}Delete document{% endtrans %}"
                                               data-confirm="{% trans name=filename %}Are you sure you want to completely delete the document '{{ name }}'?{% endtrans %}"
                                               data-update="#receipts-list"
                                               data-method="DELETE"
                                               data-href="{{ url_for('.delete_receipt', receipt) }}"></a>
                                        </div>
                                    </td>
                                {% endif %}
                            </tr>
                        {% endfor %}
                    </table>
                {%- else -%}
                    <div class="no-data-placeholder">
                        <i class="icon-agreement"></i>
                        <span>
                            {% trans %}There are no documents yet.{% endtrans %}
                        </span>
                    </div>
                {%- endif %}
            </div>
        </div>
    {% endif %}
{% endmacro %}

{% macro render_invoice(registration, payment_enabled, payment_conditions=false) %}
    {% if registration.price %}
        <div id="payment-summary" class="regform-done">
            <div class="i-box-header">
                <div class="i-box-title">
                    {% trans %}Invoice{% endtrans %}
                </div>
                {% if not registration.transaction or registration.transaction.status.name not in ('successful', 'pending')
                   or registration.is_pending_transaction_expired() %}
                    <div class="payment-status payment-not-paid right">
                        {% trans %}Not paid{% endtrans %}
                        <i class="icon-time"></i>
                    </div>
                {% elif registration.transaction.status.name == 'successful' %}
                    <div class="payment-status payment-done right">
                        {% trans %}Paid{% endtrans %}
                        <i class="icon-checkbox-checked"></i>
                    </div>
                {% elif registration.transaction and registration.transaction.status.name == 'pending' %}
                    <div class="payment-status payment-pending right">
                        {% trans %}Pending{% endtrans %}
                        <i class="icon-time"></i>
                    </div>
                {% endif %}
            </div>
            <div class="i-box-content">
                <table class="registration-info payment-info">
                    <tr>
                        <td class="regform-done-table-title">
                            {% trans %}Item{% endtrans %}
                        </td>
                        <td class="regform-done-table-title">
                            {% trans %}Value{% endtrans %}
                        </td>
                        <td class="regform-done-table-title text-right">
                            {% trans %}Cost{% endtrans %}
                        </td>
                    </tr>
                    {% for item in registration.billable_data %}
                        <tr class="regform-done-table-item">
                            <td class="text-left">
                                {{- item.field_data.field.title }}
                                <span class="section-name">({{ item.field_data.field.parent.title -}})</span>
                            </td>
                            <td class="text-left">
                                {% if item.field_data.field.input_type == 'accommodation' and item.friendly_data %}
                                    {% trans nights=item.friendly_data.nights,
                                             choice=item.friendly_data.choice -%}
                                        {{ choice }} ({{ nights }} night)
                                    {%- pluralize -%}
                                        {{ choice }} ({{ nights }} nights)
                                    {%- endtrans %}
                                {% elif item.field_data.field.input_type == 'multi_choice' %}
                                    {{ item.friendly_data | join(', ') }}
                                {% else %}
                                   {{ item.field_data.field.field_impl.render_invoice_data(item) }}
                                {% endif %}
                            </td>
                            <td class="text-right">{{ item.render_price() }}</td>
                        </tr>
                    {% endfor %}
                    {% if registration.base_price %}
                        <tr class="regform-done-table-item">
                            <td class="text-left">
                                {% trans %}Registration fee{% endtrans %}
                            </td>
                            <td></td>
                            <td class="text-right">
                                {{ registration.render_base_price() }}
                            </td>
                        </tr>
                    {% endif %}
                    {% if registration.price_adjustment %}
                        <tr class="regform-done-table-item">
                            <td class="text-left">
                                {% trans %}Price adjustment{% endtrans %}
                            </td>
                            <td></td>
                            <td class="text-right">
                                {{ registration.render_price_adjustment() }}
                            </td>
                        </tr>
                    {% endif %}
                    <tr>
                        <td class="regform-done-table-total text-right" colspan="3">
                            <strong>{% trans %}Total{% endtrans %}</strong>
                            {{ registration.render_price() }}
                        </td>
                    </tr>
                </table>
                {{ caller() }}
            </div>
        </div>
    {% endif %}
{% endmacro %}
