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

{% macro render_registration_details(registration, payment_enabled, assigned_tags, all_tags) %}
    <div id="registration-details">
        {{ template_hook('before-registration-actions', registration=registration) }}
        <div class="action-box">
           <div class="section">
               <div class="icon icon-quill registration-{{ registration.state.name }}"></div>
                {% if registration.state.name == 'pending' %}
                    {{ _render_pending_actions(registration) }}
                {% elif registration.state.name == 'unpaid' %}
                    {{ _render_unpaid_actions(registration) }}
                {% elif registration.state.name == 'complete' %}
                    {{ _render_complete_actions(registration) }}
                {% elif registration.state.name in ('rejected', 'withdrawn') %}
                    {{ _render_cancelled_actions(registration) }}
                {% endif %}
            </div>
            {% block checkin_state scoped %}
                {% if registration.state.name in ('unpaid', 'complete') %}
                    {{ _render_check_in(registration) }}
                {% endif %}
            {% endblock %}
            {{ template_hook('extra-registration-actions', registration=registration) }}
        </div>
        {% block render_withdraw_actions scoped %}
            {% if registration.state.name not in ('rejected', 'withdrawn') %}
                {{ _render_withdraw_actions(registration) }}
            {% endif %}
        {% endblock %}
        {% block render_allow_modification scoped %}
            {{ _render_allow_modification(registration) if registration.is_active }}
        {% endblock %}
        {% if all_tags %}
            <div class="action-box">
                {{ _render_registration_tags(registration, assigned_tags, all_tags) }}
            </div>
        {% endif %}
        {% block render_view_logs scoped %}
            {{ _render_view_logs(registration) }}
        {% endblock %}
        {{ template_hook('before-registration-summary', registration=registration) }}
        {{ render_registration_summary(registration, from_management=True) }}
        {% call render_invoice(registration, payment_enabled) %}
            <table class="regform-done-footer registration-info">
                <tr>
                    <td>
                        {% if registration.transaction and registration.transaction.data.changed_by_name %}
                            <div class="checkbox-with-text">
                                <div class="payment-conditions-agreement">
                                    {%- trans updated_by=registration.transaction.data.changed_by_name,
                                             timestamp=registration.transaction.timestamp | format_date(timezone=registration.event.tzinfo) -%}
                                        Last updated by {{ updated_by }} on {{ timestamp }}.
                                    {% endtrans -%}
                                </div>
                            </div>
                        {% endif %}
                    </td>
                    <td class="regform-done-footer-button">
                        {% if not registration.is_paid %}
                            <button class="i-button big"
                                    data-update="#registration-details"
                                    data-method="POST"
                                    data-params="{{ dict(pay=1) | tojson | forceescape }}"
                                    {{ 'disabled' if registration.state.name != 'unpaid' }}
                                    data-href="{{ url_for('.toggle_registration_payment', registration) }}">
                                {% trans %}Mark as paid{% endtrans %}
                            </button>
                        {% else %}
                            <button class="i-button big"
                                    data-update="#registration-details"
                                    data-method="POST"
                                    data-params="{{ dict(pay=0) | tojson | forceescape }}"
                                    data-href="{{ url_for('.toggle_registration_payment', registration) }}">
                                {% trans %}Mark as unpaid{% endtrans %}
                            </button>
                        {% endif %}
                    </td>
                </tr>
            </table>
        {% endcall %}

        {% if registration.transaction %}
            <div id="transaction-summary" class="regform-done">
                <div class="i-box-header">
                    <div class="i-box-title">
                        {% trans %}Payment transaction{% endtrans %}
                    </div>
                </div>
                <div class="i-box-content">
                    {{ registration.transaction.render_details()|safe }}
                </div>
            </div>
        {% endif %}
    </div>
{% endmacro %}


{% macro _render_pending_actions(registration) %}
    <div class="text">
       <div class="label">
           {% trans %}Registration awaiting manager approval{% endtrans %}
       </div>
       {% trans -%}
           You can validate or reject this registration and the user will receive a notification.
       {%- endtrans %}
    </div>
    <div class="toolbar">
        {% block registration_pending_action_buttons scoped %}
            <div class="group">
                <button class="i-button accept"
                        data-update="#registration-details"
                        data-method="POST"
                        data-href="{{ url_for('.approve_registration', registration) }}"
                        data-confirm="{% trans %}Are you sure you want to approve this registration? This will trigger a notification email.{% endtrans %}"
                        data-title="{% trans %}Approve registration{% endtrans %}">
                    {%- trans %}Approve{% endtrans -%}
                </button>
            </div>
            <div class="group">
                <button class="i-button danger"
                        data-update="#registration-details"
                        data-href="{{ url_for('.reject_registration', registration) }}"
                        data-title="{% trans %}Reject registration{% endtrans %}"
                        data-ajax-dialog>
                    {%- trans %}Reject{% endtrans -%}
                </button>
            </div>
        {% endblock %}
    </div>
{% endmacro %}


{% macro _render_unpaid_actions(registration) %}
    <div class="text">
        <div class="label">
           {% trans %}Registration not paid yet{% endtrans %}
        </div>
        {% if registration.is_paid %}
            {% trans %}The current transaction is still pending.{% endtrans %}
        {% else %}
            {% trans %}You can mark the registration as paid manually.{% endtrans %}
        {% endif %}
    </div>
    {% if not registration.is_paid %}
        <div class="toolbar">
            <div class="group">
                <span class="i-button label icon-coins">
                    {{ registration.render_price() }}
                </span>
            </div>
            <div class="group">
                <button class="i-button"
                        data-update="#registration-details"
                        data-method="POST"
                        data-params="{{ dict(pay=1) | tojson | forceescape }}"
                        data-href="{{ url_for('.toggle_registration_payment', registration) }}">
                    {% trans %}Mark as paid{% endtrans %}
                </button>
            </div>
            {% if registration.registration_form.moderation_enabled %}
                {{ _render_reset_registration_button(registration, _('Reset approval')) }}
            {% endif %}
        </div>
    {% endif %}
{% endmacro %}


{% macro _render_complete_actions(registration) %}
    <div class="text">
        <div class="label">
            {% trans %}This registration is complete{% endtrans %}
        </div>
        {% trans submitted=registration.submitted_dt|format_date(timezone=registration.event.tzinfo) -%}
            Submitted: {{ submitted }}
        {%- endtrans %}
    </div>
    {% set can_reset = registration.registration_form.moderation_enabled and not registration.is_paid %}
    {% set ticket_template = registration.registration_form.get_ticket_template() %}
    {% set can_get_ticket = registration.registration_form.tickets_enabled and (not ticket_template.is_ticket or not registration.is_ticket_blocked) %}
    {% if can_reset or can_get_ticket %}
        <div class="toolbar">
            {% if can_reset %}
                {{ _render_reset_registration_button(registration, _('Reset approval')) }}
            {% endif %}
            {% if can_get_ticket %}
                <a href="{{ url_for('.ticket_download', registration.locator.registrant) }}" class="i-button accept icon-ticket">
                    {% trans %}Get ticket{% endtrans %}
                </a>
            {% endif %}
        </div>
    {% endif %}
{% endmacro %}


{% macro _render_cancelled_actions(registration) %}
    <div class="text">
        <div class="label">
            {% trans state=registration.state.title|lower -%}
                This registration is {{ state }}
            {%- endtrans %}
        </div>
        {% if registration.state.name == 'rejected' and registration.rejection_reason %}
            {% trans reason=registration.rejection_reason %}Reason: {{ reason }}{% endtrans %}
        {% endif %}
    </div>
    <div class="toolbar hide-if-locked">
        {% set action = _('Reset rejection') if registration.state.name == 'rejected' else _('Reset withdrawal') %}
        {{ _render_reset_registration_button(registration, action, registration.has_conflict()) }}
    </div>
{% endmacro %}


{% macro _render_registration_tags(registration, assigned_tags, all_tags) %}
    <div class="section" id="registration-detail-registration-tags-assign"
         data-event-id="{{ registration.registration_form.event.id }}"
         data-regform-id="{{ registration.registration_form.id }}"
         data-registration-id="{{ registration.id }}"
         data-assigned-tags='{{ assigned_tags|tojson|forceescape }}' data-all-tags='{{ all_tags|tojson|forceescape }}'>
    </div>
{% endmacro %}


{% macro _render_reset_registration_button(registration, action_text, conflict=false) %}
    {% set confirm_notification -%}
        {%- block reset_registration_button_confirm_notification_text scoped -%}
            {%- if registration.state.name == 'withdrawn' -%}
                {% trans %}This will trigger a notification email.{% endtrans %}
            {%- else -%}
                {% trans %}This action will not be notified to the registrant.{% endtrans %}
            {%- endif -%}
        {%- endblock -%}
    {%- endset %}
    {% block reset_registration_button scoped %}
        <button class="i-button warning {{ 'disabled' if conflict }}"
                data-update="#registration-details"
                data-method="POST"
                data-href="{{ url_for('.reset_registration', registration) }}"
                data-confirm="{% trans %}Are you sure you want to reset this registration status?{% endtrans %} {{ confirm_notification }}"
                data-title="{{ action_text }}"
                {% if conflict %}title="{% trans %}There is another valid registration for this user so it cannot be restored.{% endtrans %}"{% endif %}>
            {{ action_text }}
        </button>
    {% endblock %}
{% endmacro %}


{% macro _render_check_in(registration) %}
    <div class="section">
        {% if registration.checked_in %}
            <div class="icon icon-location registration-checked-in"></div>
            <div class="text">
                <div class="label">
                    {% trans %}Checked in{% endtrans %}
                </div>
                {% trans checked_in_dt=registration.checked_in_dt|format_date(timezone=registration.event.tzinfo) -%}
                    Checked in: {{ checked_in_dt }}
                {%- endtrans %}
                <span class="hide-if-locked">
                    (<a href="#"
                       title="{% trans %}Mark as not checked in{% endtrans %}"
                       data-update="#registration-details"
                       data-method="DELETE"
                       data-href="{{ url_for('.registration_check_in', registration) }}"
                       data-confirm="{% trans %}Are you sure you want to reset the check-in info? The original check-in time will be lost.{% endtrans %}"
                       data-title="{% trans %}Reset check-in info{% endtrans %}">
                       {%- trans %}reset{% endtrans -%}
                    </a>)
                </span>
            </div>
        {% else %}
            <div class="icon icon-location"></div>
            <div class="text">
                <div class="label">
                    {% trans %}Not checked in{% endtrans %}
                </div>
                {% if registration.registration_form.tickets_enabled %}
                    {% trans -%}
                        You can mark the registration as checked in manually here or with the Indico Check in app.
                    {%- endtrans %}
                {% else %}
                    {% trans -%}
                        You can mark the registration as checked in manually.
                    {%- endtrans %}
                {% endif %}
            </div>
            <div class="toolbar hide-if-locked">
                <button class="i-button"
                        data-update="#registration-details"
                        data-method="PUT"
                        data-href="{{ url_for('.registration_check_in', registration) }}">
                    {% trans %}Check-in{% endtrans %}
                </button>
            </div>
        {% endif %}
    </div>
{% endmacro %}

{% macro _render_withdraw_actions(registration) %}
    <div class="action-box">
        <div class="section">
            <div class="icon icon-exit"></div>
            <div class="text">
                <div class="label">
                    {% trans %}Withdraw registration{% endtrans %}
                </div>
                {% trans -%}
                    If the participant decided to no longer attend the event, you can withdraw their registration.
                {%- endtrans %}
            </div>
            <div class="toolbar">
                <div class="group">
                    <button class="i-button"
                            data-href="{{ url_for('.manage_withdraw_registration', registration) }}"
                            data-update="#registration-details"
                            data-method="POST"
                            data-title="{% trans %}Withdraw{% endtrans %}"
                            data-confirm="{% trans %}Are you sure that you want to withdraw this registration? This will trigger a notification email.{% endtrans %}">
                        {% trans %}Withdraw{% endtrans %}
                    </button>
                </div>
            </div>
        </div>
    </div>
{% endmacro %}

{% macro _render_allow_modification(registration) %}
    <div class="action-box">
        <div class="section">
            {% if registration.modification_end_dt is none %}
                <div class="icon icon-edit"></div>
                <div class="text">
                    <div class="label">
                        {% trans %}Allow the participant to modify their registration{% endtrans %}
                    </div>
                    {% trans -%}
                        You can exceptionally allow this participant to modify their registration, regardless of the
                        modification settings or deadline configured in the registration form.
                    {%- endtrans %}
                </div>
            {% elif registration.modification_deadline_passed %}
                <div class="icon icon-edit"></div>
                <div class="text">
                    <div class="label">
                        {% trans %}The participant can no longer exceptionally modify their registration{% endtrans %}
                    </div>
                    {% trans modification_end_dt=registration.modification_end_dt|format_date(timezone=registration.event.tzinfo) -%}
                        The specified modification deadline ({{ modification_end_dt }}) has passed.
                    {% endtrans -%}
                </div>
            {% else %}
                <div class="icon icon-edit registration-allow-modification"></div>
                <div class="text">
                    <div class="label">
                        {% trans %}The participant can exceptionally modify their registration{% endtrans %}
                    </div>
                    {% trans modification_end_dt=registration.modification_end_dt|format_date(timezone=registration.event.tzinfo) -%}
                        The participant is allowed to modify their registration until: {{ modification_end_dt }}
                    {%- endtrans %}
                </div>
            {% endif %}
            <div class="toolbar">
                <div class="group">
                    {% if registration.modification_end_dt is none %}
                        <button class="i-button icon-time"
                                data-update="#registration-details"
                                data-href="{{ url_for('.registration_schedule_modification', registration) }}"
                                data-title="{% trans %}Set the modification deadline{% endtrans %}"
                                data-ajax-dialog>
                            {% trans %}Schedule{% endtrans %}
                        </button>
                    {% else %}
                        <button class="i-button icon-edit"
                                data-update="#registration-details"
                                data-href="{{ url_for('.registration_schedule_modification', registration) }}"
                                data-ajax-dialog>
                            {% trans %}Edit{% endtrans %}
                        </button>
                        <button class="i-button"
                                data-update="#registration-details"
                                data-method="POST"
                                data-href="{{ url_for('.registration_close_modification', registration) }}">
                            {% trans %}Close{% endtrans %}
                        </button>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
{% endmacro %}

{% macro _render_view_logs(registration) %}
    <div class="action-box">
        <div class="section">
            <div class="icon icon-file-content"></div>
            <div class="text">
                <div class="label">
                    {% trans %}Logs{% endtrans %}
                </div>
                {% trans -%}
                    View all logs related to this registration.
                {%- endtrans %}
            </div>
            <div class="toolbar">
                <div class="group">
                    <a href="{{ url_for('logs.event', registration.event, **{'meta.registration_id': registration.id}) }}"
                       class="i-button">
                        {% trans %}View{% endtrans %}
                    </a>
                </div>
            </div>
        </div>
    </div>
{% endmacro %}
