{% from 'events/registration/display/_registration_summary_blocks.html' import render_registration_summary,
                                                                               render_invoice %}

{% set class_mapping = {'complete': 'accept', 'pending': 'warning', 'rejected': 'danger', 'unpaid': 'warning',
                        'withdrawn': 'danger'} %}

<div class="action-box {{ class_mapping[registration.state.name] }}">
    <div class="section">
        {% set regform = registration.registration_form %}
        <div class="icon icon-quill"></div>
        <div class="text">
            {% if registration.state.name == 'complete' %}
                <div class="label">
                    {% trans %}Your registration has been completed{% endtrans %}
                </div>
            {% elif registration.state.name == 'rejected' %}
                <div class="label">
                    {% trans %}Your registration has been rejected{% endtrans %}
                </div>
                {% trans -%}
                    Contact an event manager if you think this is an error.
                {%- endtrans %}
            {% elif registration.state.name == 'withdrawn' %}
                <div class="label">
                    {% trans %}You have withdrawn your registration{% endtrans %}
                </div>
                {% trans -%}
                    Contact an event manager if you changed your mind.
                {%- endtrans %}
            {% elif registration.state.name == 'pending' %}
                <div class="label">
                    {% trans %}Your registration is awaiting approval{% endtrans %}
                </div>
                {% block registration_pending_explanation %}
                    {% trans -%}
                        An event manager will manually validate it.
                    {%- endtrans %}
                {% endblock %}
            {% elif registration.state.name == 'unpaid' %}
                <div class="label">
                    {% trans %}Your registration is awaiting payment{% endtrans %}
                </div>
                {% if not registration.transaction or registration.transaction.status.name not in ('successful', 'pending') %}
                    {% trans %}Awaiting your payment.{% endtrans %}
                {% elif registration.transaction.status.name == 'pending' %}
                    {% trans %}Awaiting payment confirmation.{% endtrans %}
                {% endif %}
                <a href="#payment-summary" class="js-highlight-payment">
                    {%- trans %}Proceed to checkout{% endtrans -%}
                </a>.
            {% endif %}
            {% if registration.can_be_modified and not regform.is_modification_open %}
                <div class="text">
                    {% trans modification_end_dt=registration.modification_end_dt|format_date(timezone=registration.event.tzinfo) -%}
                        You are allowed to modify your registration until: {{ modification_end_dt }}
                    {%- endtrans %}
                </div>
            {% endif %}
        </div>
        <div class="toolbar">
            {% if registration.is_active and (regform.modification_mode.name != 'not_allowed' or
                                              not registration.modification_deadline_passed) %}
                <div class="group">
                    <a href="{{ url_for('.edit_registration_display', registration.locator.registrant) }}"
                       class="i-button icon-edit {% if not registration.can_be_modified %}disabled{% endif %}"
                        {% if not registration.can_be_modified %}
                            {% if not regform.is_modification_open -%}
                                title="{% trans %}The modification period is over{% endtrans %}"
                            {%- elif regform.modification_mode.name == 'allowed_until_payment' -%}
                                title="{% trans %}Modifications are not allowed after payment{% endtrans %}"
                            {%- elif regform.modification_mode.name == 'allowed_until_approved' -%}
                                title="{% trans %}Modifications are not allowed once approved{% endtrans %}"
                            {%- endif %}
                        {% endif %}>
                        {% trans %}Modify{% endtrans %}
                    </a>
                    {% if registration.can_be_withdrawn %}
                        <button class="i-button icon-exit"
                                data-href="{{ url_for('.withdraw_registration', registration.locator.registrant) }}"
                                data-method="POST"
                                data-title="{% trans %}Withdraw{% endtrans %}"
                                data-confirm="{% trans %}Are you sure that you want to withdraw your registration? This action cannot be undone.{% endtrans %}">
                            {% trans %}Withdraw{% endtrans %}
                        </button>
                    {% else %}
                        <button class="i-button icon-exit disabled"
                                title="{% trans %}The registration can no longer be withdrawn. Please contact an organizer.{% endtrans %}">
                            {% trans %}Withdraw{% endtrans %}
                        </button>
                    {% endif %}
                </div>
            {% endif %}
            {% set ticket_template = regform.get_ticket_template() %}
            {% if registration.state.name == 'complete' and regform.tickets_enabled and
                  regform.ticket_on_summary_page and (not ticket_template.is_ticket or not registration.is_ticket_blocked) %}
                <div class="group">
                    <a href="{{ url_for('.ticket_download', registration.locator.registrant) }}" class="i-button accept icon-ticket">
                        {% trans %}Get ticket{% endtrans %}
                    </a>
                    {% if regform.is_google_wallet_available and regform.is_apple_wallet_available %}
                        <button class="i-button icon-file-download js-dropdown arrow" data-toggle="dropdown">
                            {%- trans %}Add to Wallet{% endtrans -%}
                        </button>
                        <ul class="i-dropdown">
                            {% if regform.is_google_wallet_available %}
                                <li>
                                    <a href="{{ url_for('event_registration.ticket_google_wallet', regform) }}">
                                        {% trans %}Google Wallet{% endtrans %}
                                    </a>
                                </li>
                            {% endif %}
                            {% if regform.is_apple_wallet_available %}
                                <li>
                                    <a href="{{ url_for('event_registration.ticket_apple_wallet', regform) }}">
                                        {% trans %}Apple Wallet/Passbook{% endtrans %}
                                    </a>
                                </li>
                            {% endif %}
                        </ul>
                    {% else %}
                        {% if regform.is_google_wallet_available %}
                            <a href="{{ url_for('.ticket_google_wallet', registration.locator.registrant) }}"
                               class="i-button accept icon-ticket">
                                {% trans %}Save to Google Wallet{% endtrans %}
                            </a>
                        {% endif %}
                        {% if regform.is_apple_wallet_available %}
                            <a href="{{ url_for('.ticket_apple_wallet', registration.locator.registrant) }}" class="i-button accept icon-ticket">
                                {% trans %}Save to Apple Wallet/Passbook{% endtrans %}
                            </a>
                        {% endif %}
                    {% endif %}
                </div>
            {% endif %}
        </div>
    </div>
</div>

{{ render_registration_summary(registration) }}
{% call render_invoice(registration, payment_enabled, payment_conditions) %}
    {% if payment_enabled and registration.state.name == 'unpaid' and
       (not registration.is_paid or registration.is_pending_transaction_expired()) %}
        <table class="regform-done-footer" width="100%" cellpadding="0" cellspacing="0">
            <tr>
                <td>
                    {% if payment_conditions %}
                        {% set link_start %}
                            </label>
                            <a href="#"
                               data-href="{{ url_for('payment.event_payment_conditions', registration.registration_form.event) }}"
                               data-title="{% trans %}Terms and conditions{% endtrans %}"
                               data-ajax-dialog>
                        {% endset %}
                        {% set link_end %}
                            </a><label for="conditions-accepted">
                        {% endset %}
                        <div class="checkbox-with-text">
                            <input type="checkbox" id="conditions-accepted">
                            <div class="payment-conditions-agreement">
                                <label for="conditions-accepted">
                                    {% trans -%}
                                        I have read and accepted the {{ link_start }}terms and conditions{{ link_end }}
                                        and understand that by confirming this order I will be entering into a binding
                                        transaction.
                                    {%- endtrans %}
                                </label>
                            </div>
                        </div>
                    {% endif %}
                </td>
                <td class="regform-done-footer-button">
                    <a href="{{ url_for('payment.event_payment', registration.locator.registrant) }}"
                       class="action-button js-check-conditions">
                        {% trans %}Checkout{% endtrans %}
                    </a>
                </td>
            </tr>
        </table>
    {% endif %}
{% endcall %}

<div class="permalink-text">
    <div>
        {% trans -%}
            Use this link to come back to this page.<br>
            Make sure to keep it private as you do not need to be logged in to access it.
        {%- endtrans %}
    </div>
    <input type="text" class="permalink" readonly
           value="{{ url_for('.display_regform', registration.locator.uuid, _external=true) }}">
</div>
