{% extends 'events/management/base.html' if event.type == 'lecture' else 'events/management/full_width_base.html' %}

{% from 'events/management/_lists.html' import render_displayed_entries_fragment %}
{% from 'events/persons/management/_person_list_row.html' import render_person_row %}
{% from 'message_box.html' import message_box %}

{% block title %}
    {% trans %}Participant Roles{% endtrans %}
{% endblock %}

{% block title_actions %}
    {%- if event.type == 'conference' -%}
        <button class="i-button borderless icon-settings arrow js-dropdown" data-toggle="dropdown">
            {%- trans %}Settings{% endtrans -%}
        </button>
        <ul class="i-dropdown">
            <li>
                <a href="#"
                   title="{% trans %}Manage person lists{% endtrans %}"
                   data-reload-after="customData"
                   data-title="{% trans %}Manage person lists{% endtrans %}"
                   data-href="{{ url_for('.manage_person_lists', event) }}"
                   data-qtip-position="right"
                   data-ajax-dialog>
                    {% trans %}Person lists{% endtrans %}
                </a>
            </li>
        </ul>
    {%- endif -%}
{% endblock %}

{% block content %}
    {%- set has_no_account = persons|selectattr('roles.no_account')|any -%}
    {%- set has_uninvited = persons|selectattr('roles.no_account')|selectattr('person.invited_dt', 'none')|any -%}

    <div class="event-roles js-event-person-list">
        <div class="flashed-messages"></div>
        <div class="toolbars space-after">
            <div class="toolbar hide-if-locked">
                <div class="group">
                    <button class="icon-checkbox-checked i-button arrow js-dropdown" data-toggle="dropdown"></button>
                    <ul class="i-dropdown">
                        <li>
                            <a href="#" data-select-all="#event-participants-list input.select-row:visible:not(:disabled)">
                                {% trans 'Selection' %}All{% endtrans %}
                            </a>
                        </li>
                        <li>
                            <a href="#" data-select-none="#event-participants-list input.select-row:visible">
                                {% trans 'Selection' %}None{% endtrans %}
                            </a>
                        </li>
                    </ul>
                </div>
                <div class="group">
                    <div id="persons-email"
                         data-event-id="{{ event.id }}"
                         data-person-selector="#event-participants-list input[name=person_identifier][type=checkbox]:checked">
                    </div>
                    <button id="persons-email-trigger"
                            class="i-button icon-mail right highlight disabled js-requires-selected-row">
                        {%- trans %}Send email{% endtrans -%}
                    </button>
                </div>
                {% if event.type != 'lecture' %}
                    <div class="group i-selection">
                        <span class="i-button label">{% trans %}Privileges{% endtrans %}</span>
                        <button class="i-button icon-collapse arrow js-dropdown" data-toggle="dropdown"></button>
                        <ul class="i-dropdown">
                            <li>
                                <a href="#"
                                   title="{% trans %}Grant submission rights to all speakers{% endtrans %}"
                                   data-confirm="{% trans %}Are you sure you want to perform this operation?{% endtrans %}"
                                   data-href="{{ url_for('.grant_submission_rights', event) }}"
                                   data-method="POST"
                                   data-qtip-position="right">
                                    {% trans %}Submission rights{% endtrans %}</a>
                            </li>
                            <li>
                                <a href="#"
                                   title="{% trans %}Grant modification rights to all session conveners{% endtrans %}"
                                   data-confirm="{% trans %}Are you sure you want to perform this operation?{% endtrans %}"
                                   data-href="{{ url_for('.grant_modification_rights', event) }}"
                                   data-method="POST"
                                   data-qtip-position="right">
                                    {% trans %}Modification rights{% endtrans %}</a>
                            </li>
                        </ul>
                        <button class="i-button icon-expand arrow js-dropdown" data-toggle="dropdown"></button>
                        <ul class="i-dropdown">
                            <li>
                                <a href="#"
                                   title="{% trans %}Revoke submission rights{% endtrans %}"
                                   data-confirm="{% trans %}Are you sure you want to perform this operation?{% endtrans %}"
                                   data-href="{{ url_for('.revoke_submission_rights', event) }}"
                                   data-method="POST"
                                   data-qtip-position="right">
                                    {% trans %}Submission rights{% endtrans %}</a>
                            </li>
                        </ul>
                    </div>
                {% endif %}
                <div class="group">
                    <div id="invitation-email-all"
                         data-event-id="{{ event.id }}"
                         data-person-selector="#event-participants-list tr[data-no-account] input[name=person_identifier][type=checkbox]"
                         data-no-account></div>
                    {% if has_uninvited %}
                        <div id="invitation-email-not-invited"
                             data-event-id="{{ event.id }}"
                             data-person-selector="#event-participants-list tr[data-no-account] input[name=person_identifier][type=checkbox]"
                             data-no-account
                             data-not-invited-only></div>
                    {% endif %}
                    <a href="#"
                       class="i-button arrow highlight js-dropdown {{ 'disabled' if not has_no_account }}"
                       data-toggle="dropdown">
                        {% trans %}Send invitation{% endtrans %}
                    </a>
                    <ul class="i-dropdown">
                        <li>
                            <a href="#"
                               title="{% trans %}Send an email to all users with no Indico account{% endtrans %}"
                               id="invitation-email-all-trigger">
                                {% trans %}To all users with no account{% endtrans %}
                            </a>
                        </li>
                        {% if has_uninvited %}
                            <li>
                                <a href="#"
                                   title="{% trans %}Send an email to all users with no Indico account who have not
                                         received an invitation email yet{% endtrans %}"
                                   id="invitation-email-not-invited-trigger">
                                    {% trans %}To users not invited yet{% endtrans %}
                                </a>
                            </li>
                        {% endif %}
                    </ul>
                </div>
            </div>
            <div class="toolbar">
                {{ render_displayed_entries_fragment(persons|length, persons|length) }}
                <div id="person-filters" class="group list-filter">
                    <button class="i-button arrow highlight js-dropdown" data-toggle="dropdown">
                        {% trans %}Filter role{% endtrans %}
                    </button>
                    <ul class="i-dropdown">
                        {% if event.type != 'lecture' %}
                            <li class="enabled">
                                <input type="checkbox" id="filter-chairpersons" data-filter="chairperson">
                                <i class="icon-checkmark"></i>
                                <i class="icon-stop colored-square" style="color: #{{ builtin_roles.chairperson.color }};"></i>
                                <label for="filter-chairpersons">
                                    {%- trans %}Chairpersons{% endtrans -%}
                                </label>
                            </li>
                            <li class="enabled">
                                <input type="checkbox" id="filter-speakers" data-filter="speaker">
                                <i class="icon-checkmark"></i>
                                <i class="icon-stop colored-square" style="color: #{{ builtin_roles.speaker.color }};"></i>
                                <label for="filter-speakers">{% trans %}Speakers{% endtrans %}</label>
                            </li>
                            <li class="enabled">
                                <input type="checkbox" id="filter-conveners" data-filter="convener">
                                <i class="icon-checkmark"></i>
                                <i class="icon-stop colored-square" style="color: #{{ builtin_roles.convener.color }};"></i>
                                <label for="filter-conveners">{% trans %}Conveners{% endtrans %}</label>
                            </li>
                            <li class="enabled">
                                <input type="checkbox" id="filter-author" data-filter="author">
                                <i class="icon-checkmark"></i>
                                <i class="icon-stop colored-square" style="color: #{{ builtin_roles.author.color }};"></i>
                                <label for="filter-author">
                                    {%- trans %}Authors{% endtrans -%}
                                </label>
                            </li>
                        {% else %}
                            <li class="enabled">
                                <input type="checkbox" id="filter-lecture-speakers" data-filter="lecture_speaker">
                                <i class="icon-checkmark"></i>
                                <i class="icon-stop colored-square" style="color: #{{ builtin_roles.lecture_speaker.color }};"></i>
                                <label for="filter-lecture-speakers">{% trans %}Speakers{% endtrans %}</label>
                            </li>
                        {% endif %}
                        {% for role_id, role_data in custom_roles.items() %}
                            <li class="enabled">
                                <input type="checkbox" id="filter-{{ role_id }}" data-filter="{{ role_id }}">
                                <i class="icon-checkmark"></i>
                                <i class="icon-stop colored-square" style="color: #{{ role_data.color }};"></i>
                                <label for="filter-{{ role_id }}" title="{{ role_data.code }}">{{ role_data.name }}</label>
                            </li>
                        {% endfor %}
                        <li class="separator">
                            <span class="titled-rule">{% trans %}or{% endtrans %}</span>
                        </li>
                        <li for="filter-no-account">
                            <input type="checkbox" id="filter-no-account" data-filter="no_account">
                            <i class="icon-checkmark"></i>
                            <i class="icon-stop colored-square no-account"></i>
                            <label for="filter-no-account">{% trans %}Users with no account{% endtrans %}</label>
                        </li>
                        <li class="separator">
                            <span class="titled-rule">{% trans %}or{% endtrans %}</span>
                        </li>
                        <li for="filter-no-registration">
                            <input type="checkbox" id="filter-no-registration" data-filter="no_registration">
                            <i class="icon-checkmark"></i>
                            <i class="icon-stop colored-square no-registration"></i>
                            <label for="filter-no-registration">{% trans %}Unregistered users{% endtrans %}</label>
                        </li>
                        <li class="separator">
                            <span class="titled-rule">{% trans %}or{% endtrans %}</span>
                        </li>
                        <li for="filter-no-builtin-roles">
                            <input type="checkbox" id="filter-no-builtin-roles" data-filter="no_builtin_roles">
                            <i class="icon-checkmark"></i>
                            <i class="icon-stop colored-square no-builtin-roles"></i>
                            <label for="filter-no-builtin-roles">{% trans %}Users with no public roles{% endtrans %}</label>
                        </li>
                    </ul>
                    <button class="i-button highlight icon-close disabled js-reset-role-filter"
                       title="{% trans %}Reset role filters{% endtrans %}"></button>
                </div>
                <div class="group">
                    <span class="i-button label icon-search"></span>
                    <input type="text" id="search-input" placeholder="{% trans %}Search string{% endtrans %}">
                </div>
            </div>
        </div>
        <div id="event-participants-list">
            {% if num_no_account %}
                {% call message_box('warning') %}
                    {% trans count = num_no_account -%}
                        There is one user with no Indico account. An Indico account may be needed to upload materials and/or
                        manage contents.
                        {%- pluralize -%}
                        There are {{ count }} users with no Indico account. An Indico account may be needed to upload materials
                        and/or manage contents.
                    {%- endtrans -%}
                {% endcall %}
            {% endif %}
            <table class="i-table-widget tablesorter">
                {% set event_has_registration_forms = event.registration_forms|bool %}
                <thead>
                    <tr class="i-table">
                        <th class="i-table thin-column hide-if-locked" data-sorter="false"></th>
                        {% if event_has_registration_forms %}
                            <th class="i-table registration-column thin-column" data-sorter="false"></th>
                        {% endif %}
                        {# i18n: Full name of the person #}
                        <th class="i-table name-column">{{ pgettext('Person', 'Name') }}</th>
                        <th class="i-table email-column">{% trans %}Email{% endtrans %}</th>
                        <th class="i-table affiliation-column">{% trans %}Affiliation{% endtrans %}</th>
                        <th class="i-table roles-column" data-sorter="false">{% trans %}Roles{% endtrans %}</th>
                        <th class="i-table" data-sorter="false"></th>
                        <th class="i-table" data-sorter="false"></th>
                        <th class="i-table edit-column thin-column" data-sorter="false"></th>
                    </tr>
                </thead>
                <tbody>
                    {% for person_data in persons -%}
                        <tr id="person-{{ person_data.person.id }}"
                            class="i-table {{ 'untrusted' if person_data.person.is_untrusted }} {{ 'no-roles' if person_data.roles.no_roles }}"
                            data-person-roles="{{ person_data.roles | tojson | forceescape }}"
                            {{ 'data-no-account' if person_data.roles.no_account }}>
                            {{ render_person_row(person_data, event_has_registration_forms, person_schema, has_predefined_affiliations, allow_custom_affiliations) }}
                        </tr>
                    {%- else -%}
                        <tr class="i-table">
                            <td colspan="9">
                                {%- trans %}No persons{% endtrans -%}
                            </td>
                        </tr>
                    {%- endfor %}
                </tbody>
            </table>
        </div>
        <div id="filter-placeholder"></div>
    </div>
    <script>
        setupEventPersonsList({
            hasNoAccountFilter: true,
            hasNoRegistrationFilter: true,
            hasNoBuiltinRolesFilter: true,
        });
        setupEmailButton('#persons-email', '#persons-email-trigger');
        setupEmailButton('#invitation-email-all', '#invitation-email-all-trigger');
        {% if has_uninvited %}
            setupEmailButton('#invitation-email-not-invited', '#invitation-email-all-trigger');
        {% endif %}
    </script>
{% endblock %}
