{% from 'pagination.html' import render_pagination, render_pagination_entries_fragment %}
{% from 'categories/management/_create_category_button.html' import create_category_button %}
{% from 'events/management/_create_event_button.html' import create_event_button %}

{% macro event_row(event) %}
    <tr class="i-table">
        <td class="i-table column-icon">
            <input type="checkbox" name="event_id" value="{{ event.id }}">
        </td>
        <td class="i-table column-icon">
            {% if event.visibility == 0 %}
                <span class="icon-eye icon-eye-blocked" title="{% trans 'Event' %}Hidden{% endtrans %}"></span>
            {% endif %}
        </td>
        <td class="i-table column-icon">
            {% if event.is_self_protected %}
                <span class="icon-lock icon-protection-self"></span>
            {% elif event.is_public %}
                <span class="icon-unlocked icon-protection-public"></span>
            {% endif %}
        </td>
        <td class="i-table column-icon">
            {% if event.pending_move_request %}
                <span class="icon-transmission"
                      title="{% trans %}Event has a pending move request to another category{% endtrans %}"></span>
            {% endif %}
        </td>
        <td class="i-table">
            <a href="{{ url_for('event_management.settings', event) }}">
                {{ event.get_verbose_title(show_series_pos=(event.type == 'lecture')) | striptags }}
            </a>
        </td>
        <td class="i-table">{{ event.start_dt|format_datetime(timezone=event.category.tzinfo) }}</td>
        <td class="i-table actions">
            <a href="#"
               class="i-link icon-remove"
               title="{% trans %}Delete event{% endtrans %}"
               data-href="{{ url_for('event_management.delete', event) }}"
               data-ajax-dialog
               data-reload-after></a>
            <span class="js-move-event-to-subcategory-container"
                  data-event-id="{{ event.id }}"
                  {{ 'data-pending-request' if event.pending_move_request }}></span>
            <a href="{{ event.url }}"
               class="i-link icon-arrow-right-sparse"
               title="{% trans %}Go to the event display page{% endtrans %}"></a>
        </td>
    </tr>
{% endmacro %}

{% macro events_table(category, page, order_column, direction, events) %}
    <table class="i-table event-management">
        <thead>
            <tr class="i-table">
                <th class="i-table column-icon"></th>
                <th class="i-table column-icon"></th>
                <th class="i-table column-icon"></th>
                <th class="i-table column-icon"></th>
                <th class="i-table sortable-column">
                    <a href="{{ url_for('.manage_content', category, page=page,
                                        desc=(0 if direction == 'desc' and order_column == 'title' else 1),
                                        order='title') }}"
                       class="{{ direction if order_column == 'title' else ''}}">{% trans %}Title{% endtrans %}</a>
                </th>
                <th class="i-table sortable-column">
                    <a href="{{ url_for('.manage_content', category, page=page,
                                        desc=(0 if direction == 'desc' and order_column == 'start_dt' else 1),
                                        order='start_dt') }}"
                       class="{{ direction if order_column == 'start_dt' else ''}}">{% trans %}Start date{% endtrans %}</a>
                </th>
                <th class="i-table actions"></th>
            </tr>
        </thead>
        {% for event in events.items %}
            {{ event_row(event) }}
        {% endfor %}
    </table>
    {% call(page) render_pagination(events, single_page=(page == 'all')) %}
        {{ url_for('.manage_content', category, page=page,
                   desc=(1 if direction == 'desc' else 0), order=order_column) }}
    {% endcall %}
{% endmacro %}

{% macro render_events_list(category, page, order_column, direction, events) %}
    <div class="toolbars space-after">
        <div class="toolbar">
            <div class="group">
                <button class="i-button icon-checkbox-checked arrow js-dropdown" data-toggle="dropdown"></button>
                <ul class="i-dropdown">
                    <li>
                        <a href="#" data-select-all="#event-management input:checkbox[name=event_id]">
                            {# i18n: A button that selects all rows in a table #}
                            {% trans 'Selection' %}All{% endtrans %}
                        </a>
                    </li>
                    <li>
                        <a href="#" data-select-none="#event-management input:checkbox[name=event_id]">
                            {# i18n: A button that deselects all rows in a table #}
                            {% trans 'Selection' %}None{% endtrans %}
                        </a>
                    </li>
                </ul>
            </div>
            <div class="group">
                {% if not category.children %}
                    {{ create_category_button(category) }}
                {% endif %}
                {{ create_event_button(category, classes='icon-plus highlight', text=_("Create event")) }}
            </div>
        </div>
        {% if events.total %}
            <div class="toolbar js-event-management-toolbar">
                <div class="group">
                    {{ render_pagination_entries_fragment(events) }}
                </div>
                <div class="group">
                    <button class="i-button icon-split js-enabled-if-checked"
                            title="{% trans %}Split selected events into new categories{% endtrans %}"
                            data-method="POST"
                            data-href="{{ url_for('.split_category', category) }}"
                            data-title="{% trans %}Split category{% endtrans %}"
                            data-params-selector="#event-management input[type=checkbox]:checked"
                            data-ajax-dialog></button>
                </div>
                <div class="group">
                    <span id="js-move-events-to-subcategory-container"></span>
                    <button class="i-button icon-remove js-enabled-if-checked"
                            title="{% trans %}Delete selected events{% endtrans %}"
                            data-href="{{ url_for('.delete_events', category) }}"
                            data-method="POST"
                            data-params-selector="#event-management input[type=checkbox]:checked"
                            data-ajax-dialog></button>
                </div>
            </div>
        {% endif %}
    </div>
    <div id="selection-message" class="action-box highlight"></div>
    <div id="event-management" data-total="{{ events.total }}">
        <input type="checkbox" name="all_selected" style="display: none;" value="1">
        {% if events.total %}
            {{ events_table(category, page, order_column, direction, events) }}
        {% else %}
            <div class="info-message-box">
                <div class="message-text">
                    {% trans %}There are no events in this category{% endtrans %}
                </div>
            </div>
        {% endif %}
    </div>
{% endmacro %}
