{% from 'message_box.html' import message_box %}

{% macro render_registration_list(regform, registrations, dynamic_columns, static_columns, extra_columns, total_registrations) %}
    {% if registrations %}
        <form method="POST">
            <input type="hidden" name="csrf_token" value="{{ session.csrf_token }}">
            {% set filtered_registrations = registrations | length %}
            {% if filtered_registrations !=  total_registrations %}
                <div class="info-message-box">
                    <div class="message-text">
                        {%- trans -%}
                            Filtering is enabled. Displaying {{ filtered_registrations }} out of {{ total_registrations }} registrants.
                        {%- endtrans -%}
                    </div>
                </div>
            {% endif %}
            <div class="js-list-table-wrapper">
                <table class="i-table tablesorter" style="table-layout: auto;">
                    <thead>
                        <tr class="i-table">
                            <th class="i-table thin-column"></th>
                            {{ template_hook('registration-status-flag', regform=regform, registration=none, header=true) }}
                            <th class="i-table id-column">{% trans %}ID{% endtrans %}</th>
                            <th class="i-table">{% trans %}Full name{% endtrans %}</th>
                            {% for item in static_columns if not item.get('filter_only') %}
                                <th class="i-table" data-sorter="text">{{ item.caption }}</th>
                            {% endfor %}
                            {% for item in extra_columns if not item.filter_only %}
                                <th class="i-table" data-sorter="text">{{ item.title }}</th>
                            {% endfor %}
                            {% for item in dynamic_columns %}
                                <th class="i-table" data-sorter="text">{{ item.title }}</th>
                            {% endfor %}
                        </tr>
                    </thead>
                    <tbody>
                        {% for registration in registrations %}
                            {% set data = registration.data_by_field %}
                            <tr id="registration-{{ registration.id }}" class="i-table">
                                <td class="i-table">
                                    <input class="select-row" type="checkbox" name="registration_id"
                                           value="{{ registration.id }}"
                                           data-has-files="{{ registration.has_files | tojson }}"
                                           data-has-documents="{{ (registration.num_receipt_files > 0) | tojson }}">
                                </td>
                                {{ template_hook('registration-status-flag', regform=regform, registration=registration, header=false) }}
                                <td class="i-table">
                                    #{{ registration.friendly_id }}
                                </td>
                                <td class="i-table">
                                    <a href="{{ url_for('event_registration.registration_details', registration) }}"
                                       {% if registration.state.name in ('rejected', 'withdrawn') %}style="text-decoration: line-through;"{% endif %}>
                                        {{- registration.display_full_name -}}
                                    </a>
                                    {%- if registration.created_by_manager %}
                                        <i class="icon-user-chairperson text-not-important" title="{% trans %}This user has been registered by an event manager.{% endtrans %}"></i>
                                    {%- endif -%}
                                </td>
                                {% for item in static_columns if not item.get('filter_only') %}
                                    {% if item.id == 'reg_date' %}
                                        <td class="i-table" data-text="{{ registration.submitted_dt }}">
                                            {{- registration.submitted_dt | format_datetime(timezone=registration.event.tzinfo) -}}
                                        </td>
                                    {% elif item.id == 'state' %}
                                        <td class="i-table">{{ registration.state.title }}</td>
                                    {% elif item.id == 'price' %}
                                        <td class="i-table" data-text="{{ registration.price }}">{{ registration.render_price() }}</td>
                                    {% elif item.id == 'checked_in' %}
                                        <td class="i-table">
                                            {% if registration.checked_in %}
                                                {%- trans %}Yes{% endtrans -%}
                                            {% else %}
                                                {%- trans %}No{% endtrans -%}
                                            {% endif %}
                                    {% elif item.id == 'checked_in_date' %}
                                        <td class="i-table" data-text="{{ registration.checked_in_dt }}">
                                            {%- if registration.checked_in_dt %}
                                                {{- registration.checked_in_dt | format_datetime(timezone=registration.event.tzinfo) -}}
                                            {%- endif %}
                                        </td>
                                    {% elif item.id == 'payment_date' %}
                                        <td class="i-table" data-text="{{ registration.payment_dt }}">
                                            {%- if registration.payment_dt %}
                                                {{ registration.payment_dt | format_datetime(timezone=registration.event.tzinfo) }}
                                            {%- else %}
                                                -
                                            {% endif %}
                                        </td>
                                    {% elif item.id == 'tags_present' %}
                                        <td class="i-table" style="padding-top: 8px; padding-bottom: 8px;">
                                            {% for tag in registration.tags|sort(attribute='title', case_sensitive=false) %}
                                                <span class="ui label {{ tag.color }}">{{ tag.title }}</span>
                                            {% endfor %}
                                        </td>
                                    {% elif item.id == 'visibility' %}
                                        <td class="i-table" data-text="{{ registration.visibility }}">
                                            {{ registration.visibility.title }}
                                        </td>
                                    {% elif item.id == 'consent_to_publish' %}
                                        <td class="i-table" data-text="{{ registration.consent_to_publish }}">
                                            {{ registration.consent_to_publish.title }}
                                        </td>
                                    {% elif item.id == 'participant_hidden' %}
                                        <td class="i-table" data-text="{{ registration.participant_hidden }}">
                                            {% if registration.participant_hidden %}
                                                {%- trans %}Yes{% endtrans -%}
                                            {% else %}
                                                {%- trans %}No{% endtrans -%}
                                            {% endif %}
                                        </td>
                                    {% elif item.id == 'receipts_present' %}
                                        <td class="i-table">
                                            {% if registration.num_receipt_files %}
                                                {%- trans %}Yes{% endtrans %} ({{ registration.num_receipt_files }})
                                            {% else %}
                                                {%- trans %}No{% endtrans -%}
                                            {% endif %}
                                        </td>
                                    {% else %}
                                        {% set search_value = data[item.id].search_data if item.id in data else '' %}
                                        <td class="i-table" data-text="{{ search_value }}">
                                            {%- if item.id in data and data[item.id].friendly_data %}
                                                {{- data[item.id].friendly_data }}
                                            {%- endif %}
                                        </td>
                                    {% endif %}
                                {% endfor %}
                                {% for item in extra_columns if not item.filter_only %}
                                    {% set spec = item.data.get(registration) %}
                                    {% if spec %}
                                        <td class="i-table" data-text="{{ spec.text_value }}" {{ spec.td_attrs | html_params }}>
                                            {{ spec.content }}
                                        </td>
                                    {% else %}
                                        <td class="i-table" data-text=""></td>
                                    {% endif %}
                                {% endfor %}
                                {% for item in dynamic_columns %}
                                    {% set search_value = data[item.id].search_data if item.id in data else '' %}
                                    {% if item.id in data and data[item.id].field_data.field.is_purged %}
                                        <td class="i-table">
                                            <span class="icon-warning purged-field-warning"
                                                  data-qtip-style="warning"
                                                  title="{% trans %}The field data has been purged due to an expired retention period{% endtrans %}">
                                            </span>
                                        </td>
                                    {% elif item.id in data and data[item.id].field_data.field.input_type == 'checkbox' %}
                                        <td class="i-table{%- if data[item.id].data %} icon-checkmark{% endif %}"
                                            data-text="{{ search_value }}"></td>
                                    {% elif item.id in data and data[item.id].field_data.field.input_type == 'accommodation' %}
                                        <td class="i-table" data-text="{{ search_value }}">
                                            {% if data[item.id].friendly_data %}
                                                {%- if data[item.id].friendly_data.is_no_accommodation -%}
                                                    {{ data[item.id].friendly_data.choice }}
                                                {%- else -%}
                                                    {% trans nights=data[item.id].friendly_data.nights,
                                                             choice=data[item.id].friendly_data.choice -%}
                                                        {{ choice }} ({{ nights }} night)
                                                    {%- pluralize -%}
                                                        {{ choice }} ({{ nights }} nights)
                                                    {%- endtrans %}
                                                {%- endif -%}
                                            {% endif %}
                                        </td>
                                    {% elif item.id in data and data[item.id].field_data.field.input_type == 'multi_choice' %}
                                        <td class="i-table" data-text="{{ search_value }}">
                                            {%- if item.id in data %}
                                                {{- data[item.id].friendly_data | join(', ') }}
                                            {%- endif %}
                                        </td>
                                    {% elif item.id in data and data[item.id].field_data.field.input_type == 'sessions' %}
                                        <td class="i-table" data-text="{{ search_value }}">
                                            {%- if item.id in data and data[item.id].friendly_data != None %}
                                                {{- data[item.id].friendly_data | join('; ') }}
                                            {%- endif %}
                                        </td>
                                    {% elif item.id not in data %}
                                        <td class="i-table" data-text=""></td>
                                    {% else %}
                                        {% set spec = data[item.id].field_data.field.field_impl.render_reglist_column(data[item.id]) %}
                                        <td class="i-table" data-text="{{ spec.text_value }}" {{ spec.td_attrs | html_params }}>
                                            {{ spec.content }}
                                        </td>
                                    {% endif %}
                                {% endfor %}
                            </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </form>
    {% else %}
        {%- call message_box('info') -%}
            {%- if total_registrations %}
                {%- trans %}The filtering criteria do not match with any of the existing registrations. Try to clear the filters from the "Customize list" dialog.{% endtrans -%}
            {% else %}
                {% trans %}There are no registrations yet.{% endtrans %}
            {% endif -%}
        {%- endcall %}
    {% endif %}
{% endmacro %}
