{% extends 'events/management/base.html' %}

{% from '_switch.html' import switch %}

{% macro extra_columns(data) %}
    {% for column in definition.render_data(event, data) or () %}
        <td>
            {{ column }}
        </td>
    {% endfor %}
{% endmacro %}


{% block title %}
    {% trans %}Agreements{% endtrans %}
{% endblock %}

{% block subtitle %}
    {{ definition.title }}
{% endblock %}

{% block description %}
    {{ definition.description or '' }}
{% endblock %}

{% block content %}
    {% set not_sent = definition.get_people_not_notified(event).values() %}
    {% set pending = agreements | selectattr('pending') | list %}
    {% set signed = agreements | rejectattr('pending') | list %}

    <div class="agreement-dashboard">
        <div class="action-box">
            <div class="section">
                <div class="icon icon-bullhorn"></div>
                <div class="text">
                    <div class="label">
                        {% trans %}Notify managers on signature{% endtrans %}
                    </div>
                    <div>
                        {% trans %}Event managers will receive emails every time an agreement is signed{% endtrans %}
                    </div>
                </div>
                <div class="toolbar">
                    {{ switch(id='toggle-manager-notifications',
                              checked=definition.event_settings.get(event, 'manager_notifications_enabled'),
                              data_href=url_for_plugin('.toggle_notifications', event, definition),
                              disabled=event.is_locked) }}
                </div>
            </div>
            {% if definition.paper_form_url %}
                <div class="section">
                    <div class="icon icon-file-pdf"></div>
                    <div class="text">
                        <div class="label">
                            {% trans %}Paper form available{% endtrans %}
                        </div>
                        <div>
                            {% trans %}Print this version for people to sign it physically{% endtrans %}
                        </div>
                    </div>
                    <div class="toolbar">
                        <a class="i-button icon-file-download" href="{{ definition.paper_form_url }}">
                            {% trans %}Download{% endtrans %}
                        </a>
                    </div>
                </div>
            {% endif %}
        </div>

        {% if not_sent or pending %}
            <div class="action-box highlight hide-if-locked">
                {% if not_sent %}
                    <div class="section">
                        <div class="icon icon-agreement"></div>
                        <div class="text">
                            <div class="label">
                                {% trans count=not_sent|length -%}
                                    One new agreement form to be sent
                                {%- pluralize -%}
                                    {{ count }} new agreement forms to be sent
                                {%- endtrans %}
                            </div>
                            <div>
                                {% trans %}You can send all agreement requests{% endtrans %}
                            </div>
                        </div>
                        <div class="toolbar">
                            <button class="i-button highlight icon-mail js-ajax-dialog"
                                    data-href="{{ url_for('.event_agreements_details_send_all', event, definition=definition.name) }}">
                                {% trans %}Send all{% endtrans %}
                            </button>
                        </div>
                    </div>
                {% endif %}
                {% if pending %}
                    <div class="section">
                        <div class="icon icon-time"></div>
                        <div class="text">
                            <div class="label">
                                {% trans count=pending|length -%}
                                    {{ count }} agreement pending to be signed
                                {%- pluralize -%}
                                    {{ count }} agreements pending to be signed
                                {%- endtrans %}
                            </div>
                            <div>
                                {% trans %}You can send reminders for all pending agreements{% endtrans %}
                            </div>
                        </div>
                        <div class="toolbar">
                            <button class="i-button icon-alarm js-ajax-dialog"
                                    data-href="{{ url_for('.event_agreements_details_remind_all', event, definition=definition.name) }}">
                                {% trans %}Remind all{% endtrans %}
                            </button>
                        </div>
                    </div>
                {% endif %}
            </div>
        {% endif %}

        <div class="agreement-list i-box{% if not (not_sent or pending) %} titled{% endif %}">
            {% if not_sent %}
                <div class="toolbar hide-if-locked">
                    <button class="i-button icon-checkbox-checked arrow js-dropdown" data-toggle="dropdown"></button>
                    <ul class="i-dropdown">
                        <li>
                            <a href="#" id="select-new">{% trans 'Selection' %}All{% endtrans %}</a>
                        </li>
                        <li>
                            <a href="#" id="select-new-none">{% trans 'Selection' %}None{% endtrans %}</a>
                        </li>
                    </ul>
                    <button id="send-new"
                            class="i-button icon-mail disabled js-ajax-dialog-send"
                            data-href="{{ url_for('.event_agreements_details_send', event, definition=definition.name) }}">
                        {%- trans %}Send new{% endtrans -%}
                    </button>
                </div>
                <div class="titled-rule">
                    {% trans %}Agreement requests to be sent{% endtrans %}
                </div>
                <table class="agreement-table">
                    {% for person in not_sent|sort(attribute='name') %}
                        <tr>
                            <td class="agreement-checkbox hide-if-locked" {%- if not person.email %} title="{% trans -%}
                                This person has no email address set so he cannot receive an agreement email.
                                However, you can upload a signed agreement form for him.
                                {%- endtrans %}"{% endif %}>
                                <input type="checkbox" class="new-agreement" autocomplete="off"
                                       {%- if not person.email %} disabled{% endif %}
                                       id="person-{{ person.identifier }}" value="{{ person.identifier }}">
                            </td>
                            <td class="person-name">
                                <label for="person-{{ person.identifier }}">
                                    {{ person.name }}
                                </label>
                            </td>
                            <td class="person-email">
                                {%- if person.email -%}
                                    {{ person.email }}
                                {%- else -%}
                                    <em>{% trans %}No email{% endtrans %}</em>
                                {%- endif -%}
                            </td>
                            {{ extra_columns(person.data) }}
                            <td class="agreement-actions hide-if-locked">
                                <span class="right">
                                    {% if person.email %}
                                        <a class="icon-mail js-ajax-dialog-email-one" href="#"
                                           data-href="{{ url_for('.event_agreements_details_send', event, definition=definition.name) }}"
                                           data-reference="{{ person.identifier }}"
                                           title="{% trans %}Send the agreement request to this person{% endtrans %}"></a>
                                    {% endif %}
                                    <a class="icon-quill js-ajax-dialog" href="#"
                                       data-href="{{ url_for('.event_agreements_details_submit_answer', event, definition, identifier=person.identifier) }}"
                                       title="{% trans %}Answer manually for this person{% endtrans %}"></a>
                                </span>
                            </td>
                        </tr>
                    {% endfor %}
                </table>
            {% endif %}

            {% if pending %}
                <div class="toolbar hide-if-locked">
                    <button class="i-button icon-checkbox-checked arrow js-dropdown" data-toggle="dropdown"></button>
                    <ul class="i-dropdown">
                        <li>
                            <a href="#" id="select-pending">{% trans 'Selection' %}All{% endtrans %}</a>
                        </li>
                        <li>
                            <a href="#" id="select-pending-none">{% trans 'Selection' %}None{% endtrans %}</a>
                        </li>
                    </ul>
                    <button id="send-pending"
                            class="i-button icon-alarm disabled js-ajax-dialog-remind"
                            data-href="{{ url_for('.event_agreements_details_remind', event, definition=definition.name) }}">
                        {%- trans %}Remind pending{% endtrans -%}
                    </button>
                </div>
                <div class="titled-rule">
                    {% trans %}Pending agreements{% endtrans %}
                </div>
                <table class="agreement-table">
                    {% for agreement in pending|sort(attribute='person_name') %}
                        <tr>
                            <td class="agreement-checkbox hide-if-locked">
                                <input type="checkbox" class="pending-agreement" autocomplete="off"
                                       id="agreement-{{ agreement.id }}" value="{{ agreement.id }}">
                            </td>
                            <td class="person-name">
                                <label for="agreement-{{ agreement.id }}">
                                    {{ agreement.person_name }}
                                </label>
                            </td>
                            <td class="person-email">
                                {%- if agreement.person_email -%}
                                    {{ agreement.person_email }}
                                {%- else -%}
                                    <em>{% trans %}No email{% endtrans %}</em>
                                {%- endif -%}
                            </td>
                            {{ extra_columns(agreement.data) }}
                            <td class="agreement-actions hide-if-locked">
                                <span class="right">
                                    {% if agreement.person_email %}
                                        <a class="icon-alarm js-ajax-dialog-email-one" href="#"
                                           data-href="{{ url_for('.event_agreements_details_remind', event, definition=definition.name) }}"
                                           data-reference="{{ agreement.id }}"
                                           title="{% trans %}Remind this person{% endtrans %}">
                                        </a>
                                    {% endif %}
                                    <a class="icon-quill js-ajax-dialog" href="#"
                                       data-href="{{ url_for('.event_agreements_details_submit_answer', event, definition, agreement) }}"
                                       title="{% trans %}Answer manually for this person{% endtrans %}"></a>
                                </span>
                            </td>
                        </tr>
                    {% endfor %}
                </table>
            {% endif %}

            {% if signed %}
                <div class="titled-rule">
                    {% trans %}Agreements signed{% endtrans %}
                </div>
                <table class="agreement-table">
                    {% for agreement in signed|sort(attribute='person_name') %}
                        <tr>
                            <td class="agreement-answer">
                                {% if agreement.accepted %}
                                    <i class="icon-checkmark"></i>
                                {% else %}
                                    <i class="icon-disable"
                                       {% if agreement.reason %}title="{% trans reason=agreement.reason %}Rejection reason: {{ reason }}{% endtrans %}"{% endif %}
                                    ></i>
                                {% endif %}
                            </td>
                            <td class="person-name">
                                {{ agreement.person_name }}
                            </td>
                            <td class="person-email">
                                {%- if agreement.person_email -%}
                                    {{ agreement.person_email }}
                                {%- else -%}
                                    <em>{% trans %}No email{% endtrans %}</em>
                                {%- endif -%}
                            </td>
                            {{ extra_columns(agreement.data) }}
                            <td class="agreement-actions">
                                {% if agreement.state.name == 'accepted_on_behalf' %}
                                    <span class="right">
                                        <a class="icon-file-download" target="_blank"
                                           href="{{ url_for('.download_file', event, definition, agreement, filename=agreement.attachment_filename) }}"
                                           title="{% trans %}Download scanned agreement form{% endtrans %} ({{ agreement.attachment_filename }})"></a>
                                    </span>
                                {% endif %}
                            </td>
                        </tr>
                    {% endfor %}
                </table>
            {% endif %}
        </div>
    </div>

    <script>
        (function() {
            'use strict';

            $(function() {
                $(window).scroll(function(){
                    IndicoUI.Effect.followScroll();
                });
            });

            $('#toggle-manager-notifications').ajaxCheckbox();

            function close(data) {
                if (data) {
                    location.reload();
                }
            }

            $('#select-new').on('click', function() {
                $('input.new-agreement:not([disabled])').prop('checked', true).trigger('change');
            });

            $('#select-new-none').on('click', function() {
                $('input.new-agreement:not([disabled])').prop('checked', false).trigger('change');
            });

            $('#select-pending').on('click', function() {
                $('input.pending-agreement:not([disabled])').prop('checked', true).trigger('change');
            });

            $('#select-pending-none').on('click', function() {
                $('input.pending-agreement:not([disabled])').prop('checked', false).trigger('change');
            });

            $("input").on('change', function() {
                $('#send-new').toggleClass('disabled', !$('input.new-agreement:checked').length);
                $('#send-pending').toggleClass('disabled', !$('input.pending-agreement:checked').length);
            });

            $('.js-ajax-dialog').ajaxDialog({
                onClose: close
            });

            $('.js-ajax-dialog-send').ajaxDialog({
                onClose: close,
                getExtraData: function() {
                    var identifiers = $('input.new-agreement:checked').map(function() {
                        return $(this).val();
                    }).get();
                    return {references: identifiers};
                }
            });

            $('.js-ajax-dialog-remind').ajaxDialog({
                onClose: close,
                getExtraData: function() {
                    var ids = $('input.pending-agreement:checked').map(function() {
                        return $(this).val();
                    }).get();

                    return {references: ids}
                }
            });

            $('.js-ajax-dialog-email-one').ajaxDialog({
                onClose: close,
                getExtraData: function(trigger) {
                    return {references: $(trigger).data('reference')};
                }
            });
        })();
    </script>
{% endblock %}
