{% from 'message_box.html' import message_box %}
{% from 'attachments/_management_info_column.html' import render_attachment_info %}

{% macro render_session_list(event, sessions, default_colors, types) -%}
    {% if sessions -%}
        <form method="POST">
            <input type="hidden" name="csrf_token" value="{{ session.csrf_token }}">
            <table class="i-table tablesorter">
                <thead>
                    <tr class="i-table">
                        <th class="i-table thin-column" data-sorter="false"></th>
                        <th class="i-table id-column">{% trans %}ID{% endtrans %}</th>
                        <th class="i-table protection-column" data-sorter="false"></th>
                        <th class="i-table">{% trans %}Title{% endtrans %}</th>
                        <th class="i-table">{% trans %}Code{% endtrans %}</th>
                        <th class="i-table">{%- trans %}Type{% endtrans -%}</th>
                        <th class="i-table" data-sorter="false">{% trans %}Blocks{% endtrans %}</th>
                        <th class="i-table">{% trans %}Material{% endtrans %}</th>
                        <th data-sorter="false"></th>
                    </tr>
                </thead>
                <tbody>
                    {% for sess in sessions -%}
                        <tr class="i-table session-row" data-friendly-id="{{ sess.friendly_id }}">
                            <td class="i-table">
                                <span class="vertical-aligner">
                                    <input class="select-row" type="checkbox" value="{{ sess.id }}" name="session_id">
                                </span>
                            </td>
                            <td class="i-table id-column">
                                <span class="vertical-aligner">#{{ sess.friendly_id }}</span>
                            </td>
                            <td class="i-table">
                                <span class="vertical-aligner">
                                    <i class="{% if sess.is_self_protected %}icon-protection-self
                                              {% elif sess.is_public %}icon-protection-public{% endif %}"></i>
                                </span>
                            </td>
                            <td class="i-table" data-searchable="{{ sess.title | lower }}">
                                <span class="vertical-aligner"><strong>{{ sess.title }}</strong></span>
                            </td>
                            <td class="i-table" data-searchable="{{ sess.code | lower }}">
                                <span class="vertical-aligner">{{ sess.code }}</span>
                            </td>
                            <td class="i-table">
                                <button class="i-button {{ 'session-type-picker' if not event.is_locked else 'disabled' }}"
                                        data-items="{{ types | tojson | forceescape }}"
                                        data-href="{{ url_for('.session_rest', sess) }}"
                                        data-method="PATCH"
                                        data-selected-item-id="{{ (sess.type.id if sess.type else none) | tojson }}">
                                    <span class="label">
                                        {% if sess.type %}
                                            {{ sess.type.name }}
                                        {% else %}
                                            {% trans %}No type{% endtrans %}
                                        {% endif %}
                                    </span>
                                    {% if not event.is_locked %}<span class="icon-arrow-down"></span>{% endif %}
                                </button>
                            </td>
                            <td class="i-table">
                                <span class="i-label show-session-blocks" data-count="{{ sess.blocks | length }}"
                                      data-title="{% trans title=sess.title %}Blocks within '{{ title }}'{% endtrans %}"
                                      data-href="{{ url_for('sessions.session_blocks', sess) }}">
                                    {%- trans %}Session blocks {% endtrans -%}
                                    <span class="badge">{{ sess.blocks | length }}</span>
                                </span>
                            </td>
                            {{ render_attachment_info(sess) }}
                            <td class="i-table">
                                <div class="group right entry-action-buttons vertical-aligner">
                                    <a href=""
                                       class="i-button color-button
                                              {{ 'palette-picker-trigger' if not event.is_locked else 'disabled' }}"
                                       style="{{ sess.colors.css }}"
                                       data-method="PATCH"
                                       data-href="{{ url_for('.session_rest', sess) }}"
                                       data-colors="{{ default_colors | tojson | forceescape }}"
                                       data-color="{{ sess.colors | tojson | forceescape }}"
                                       title="{% if not event.is_locked %}{% trans %}Change color{% endtrans %}{% endif %}"></a>
                                    <a href="#" class="i-link highlight icon-edit edit-session hide-if-locked"
                                       title="{% trans %}Edit this session{% endtrans %}"
                                       data-title="{% trans title=sess.title %}Edit session '{{ title }}'{% endtrans %}"
                                       data-href="{{ url_for('.modify_session', sess) }}"
                                       data-update="#sessions-wrapper"
                                       data-ajax-dialog></a>
                                    <a href="#" class="i-link icon-shield hide-if-locked"
                                       title="{% trans %}Manage protection{% endtrans %}"
                                       data-href="{{ url_for('.session_protection', sess) }}"
                                       data-title="{% trans %}Manage session protection{% endtrans %}"
                                       data-update="#sessions-wrapper"
                                       data-ajax-dialog></a>
                                    <a href="{{ url_for('timetable.manage_session', sess) }}"
                                       class="i-link highlight icon-time"
                                       title="{% trans %}Go to this session timetable{% endtrans %}"></a>
                                    <a href="#" class="i-link highlight icon-remove hide-if-locked"
                                       title="{% trans %}Remove this session{% endtrans %}"
                                       data-method="DELETE"
                                       data-href="{{ url_for('.session_rest', sess)}}"
                                       data-params="{{ dict(sessions_ids=sess.id) | tojson | forceescape }}"
                                       data-update="#sessions-wrapper"
                                       data-confirm="{% trans title=sess.title %}Do you really want to delete the session '{{ title }}'?{% endtrans %}"></a>
                                </div>
                            </td>
                        </tr>
                    {%- endfor %}
                </tbody>
            </table>
        </form>
    {%- else %}
        {%- call message_box('info') -%}
            {% trans %}There are no sessions yet.{% endtrans %}
        {%- endcall %}
    {%- endif %}
{%- endmacro %}
