{% extends 'events/registration/emails/base_registration.html' %}

{% block registration_body %}
    {%- set diff = diff if diff is not none else {} -%}
    {%- set old_price = old_price if old_price is not none else registration.price -%}
    {% for section, fields in registration.get_summary_data(hide_empty=true).items() if not section.is_manager_only %}
        <div style="padding-bottom: 20px; border-bottom: 1px solid #EBEBEB;">
            <h2>{{ section.title }}</h2>
            <dl>
                {% for field, regdata in fields.items() if regdata.friendly_data or field.html_field_name in diff %}
                    {{ render_field(field) }}
                    {% if field.html_field_name in diff %}
                        {{ render_modified_regdata(regdata, diff[field.html_field_name]) }}
                    {% else %}
                        {{ render_regdata(regdata) }}
                    {% endif %}
                {% endfor %}
            </dl>
            <div style="clear: both;"></div>
        </div>
    {% endfor %}
    {% if old_price != registration.price or registration.price %}
        <div style="padding-bottom: 20px; border-bottom: 1px solid #EBEBEB;">
            <p style="text-align: right; margin-bottom: 0; font-size: 0.9em;">
                <strong>{% trans %}Total:{% endtrans %}</strong>
                <span style="font-style: italic">
                    {% if old_price != registration.price %}
                        <s>{{- registration._render_price(old_price) -}}</s>
                        <span style="background-color: yellow; margin-left: 5px;">
                            {{- registration.render_price() -}}
                        </span>
                    {% else %}
                        {{- registration.render_price() -}}
                    {% endif %}
                </span>
            </p>
        </div>
    {% endif %}
{% endblock %}


{% macro render_field(field) %}
    <dt style="width: 30%; float: left; clear:left;">
        {{- field.title -}}:
    </dt>
{% endmacro %}


{% macro render_regdata(regdata) %}
    {%- set type = regdata.field_data.field.input_type -%}
    <dd style="width: 70%; float: left; margin: 0; min-height: 20px; {%- if type != 'picture' -%} white-space: pre-wrap; {%- endif -%} word-break: break-all; padding-left: 20px; box-sizing: border-box;">
        {%- set friendly_data = regdata.friendly_data -%}
        {%- set html_name = regdata.field_data.field.html_field_name -%}
        <span>
            {{- render_friendly_data(friendly_data, type, regdata) -}}
        </span>
        {%- if regdata.price -%}
            {{- render_price(regdata) -}}
        {%- endif -%}
    </dd>
{% endmacro %}


{% macro render_modified_regdata(regdata, diff) %}
    {%- set type = regdata.field_data.field.input_type -%}
    <dd style="width: 70%; float: left; margin: 0; min-height: 20px; {%- if type != 'picture' -%} white-space: pre-wrap; {%- endif -%} word-break: break-all; padding-left: 20px; box-sizing: border-box;">
        {%- set html_name = regdata.field_data.field.html_field_name -%}
        {%- set old = diff.old -%}
        {%- set new = diff.new -%}
        {%- set old_friendly_data = render_friendly_data(old.friendly_data, type, regdata, true) -%}
        {%- set new_friendly_data = render_friendly_data(new.friendly_data, type, regdata, true) -%}
        <span>
            {%- if old_friendly_data and type != 'picture' -%}
                <s style="margin-right: 5px;">{{ old_friendly_data }}</s>
            {%- endif -%}
            {%- if new_friendly_data -%}
                <span {% if type != 'picture' %}style="background-color: yellow;"{% endif %}>{{ new_friendly_data }}</span>
            {%- endif -%}
        </span>
        {%- if old.price != new.price -%}
            <span style="float: right; font-style: italic; font-size: 0.9em;">{#--#}
                <s style="margin-right: 5px;">{{- regdata._render_price(old.price) -}}</s>{#--#}
                <span style="background-color: yellow;">{{- regdata._render_price(new.price) -}}</span>{#--#}
            </span>
        {%- elif regdata.price -%}
            {{- render_price(regdata) -}}
        {%- endif -%}
    </dd>
{% endmacro %}


{% macro render_friendly_data(friendly_data, type, raw_data, from_diff=false) %}
    {%- if type == 'accommodation' and friendly_data -%}
        {{- render_accommodation(friendly_data) -}}
    {%- elif type == 'multi_choice' -%}
        {{- render_multichoice(friendly_data) -}}
    {%- elif type == 'sessions' -%}
        {{- render_sessions(friendly_data) -}}
    {%- elif type == 'picture' and friendly_data -%}
        {{- render_picture(friendly_data, raw_data) -}}
    {%- elif from_diff -%}
        {{- raw_data.field_data.field.field_impl.render_email_diff_data(friendly_data) -}}
    {%- else -%}
        {{- raw_data.field_data.field.field_impl.render_email_data(raw_data) -}}
    {%- endif -%}
{% endmacro %}


{% macro render_accommodation(friendly_data) %}
    {%- if friendly_data['is_no_accommodation'] -%}
        {{- friendly_data.choice -}}
    {%- else -%}
        {{- friendly_data.choice }} {# -#}
        ({{ friendly_data.arrival_date | format_date }} - {# -#}
        {{ friendly_data.departure_date | format_date -}})
    {%- endif -%}
{% endmacro %}


{% macro render_multichoice(friendly_data) %}
    {{- friendly_data | join(', ') -}}
{% endmacro %}


{% macro render_sessions(friendly_data) %}
    {{- friendly_data | join('<br>'|safe) -}}
{% endmacro %}


{% macro render_picture(friendly_data, raw_data) %}
    <img alt="{% trans %}Registrant picture{% endtrans %}" src="cid:{{ raw_data.attachment_cid }}"
         style="max-height: 250px; max-width: 250px; white-space: normal; padding-bottom: 10px; object-fit: cover;">
{% endmacro %}


{%- macro render_price(regdata) -%}
    <span style="float: right; font-style: italic; font-size: 0.9em;">
        {{- regdata.render_price() -}}
    </span>
{%- endmacro -%}
