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

{% from 'message_box.html' import message_box %}
{% from 'forms/_form.html' import form_header, form_fieldset, form_footer, form_rows, form_row_static %}

{% set has_req = req and not req.state.name == 'withdrawn' %}
{% set show_management = is_manager and has_req %}
{% set button_title -%}
    {%- if not req -%}
        {% trans %}Send request{% endtrans %}
    {%- elif req.can_be_modified -%}
        {% trans %}Modify request{% endtrans %}
    {%- else -%}
        {% trans %}Send new request{% endtrans %}
    {%- endif -%}
{%- endset %}

{% block title %}{% trans %}Services{% endtrans %}{% endblock %}
{% block subtitle %}{{ definition.title }}{% endblock %}
{% block content %}
    {{ form_header(form, id='request-form') }}

    {% block warnings %}
        {%- if not req -%}
            {# nothing #}
        {%- elif req.state.name == 'pending' -%}
            {% call message_box('info') %}
                <p>
                    {%- trans -%}
                        Your request is currently pending approval. You will be notified by e-mail once it has been accepted or rejected.
                    {%- endtrans -%}
                </p>
            {% endcall %}
        {%- elif req.state.name == 'accepted' -%}
            {% call message_box('success') %}
                <p>
                    {%- trans -%}
                        Your request has been accepted.
                    {%- endtrans -%}
                </p>
                {% if req.comment %}
                    <p><strong>{% trans %}Comment{% endtrans %}:</strong> {{ req.comment }}</p>
                {% endif %}
            {% endcall %}
        {%- elif req.state.name == 'rejected' -%}
            {% call message_box('warning') %}
                <p>
                    {%- trans -%}
                        Your request has been rejected. Please check the rejection reason (if provided) and, if necessary, resubmit your request.
                    {%- endtrans -%}
                </p>
                {% if req.comment %}
                    <p><strong>{% trans %}Comment{% endtrans %}:</strong> {{ req.comment }}</p>
                {% endif %}
            {% endcall %}
        {%- elif req.state.name == 'withdrawn' -%}
            {% call message_box('warning') %}
                <p>
                    {%- trans -%}
                        You have withdrawn your request. If you need this service, please resubmit your request.
                    {%- endtrans -%}
                </p>
            {% endcall %}
        {%- endif -%}
    {% endblock %}

    {% if show_management %}
        {% call form_fieldset(legend=_('Request details'), description=_('These are the details provided by the event manager.')) %}
            {% block form %}
                {% if has_req %}
                    {% call form_row_static(_('Created by')) %}
                        <a href="mailto:{{ req.created_by_user.email }}">{{ req.created_by_user.full_name }}</a>
                        {% if req.created_by_user.phone %}
                            (Phone: {{ req.created_by_user.phone }})
                        {% endif %}
                    {% endcall %}
                    {{ form_row_static(_('Created at'), req.created_dt | format_datetime(timezone=req.event.tzinfo)) }}
                {% endif %}
                {{ form_rows(form) }}
            {% endblock %}
        {% endcall %}
    {% else %}
        {{ self.form() }}
    {% endif %}

    {% call form_footer(form) %}
        <input id="send-request" class="i-button big highlight" type="submit"
               value="{{ button_title }}">
        {% if not protection_overridden and req and req.state.name in ('pending', 'accepted') %}
            <input type="button" class="i-button big warning" id="withdraw-request"
               data-href="{{ url_for('.event_requests_withdraw', req) }}"
               data-method="POST"
               data-title="{% trans %}Withdraw request{% endtrans %}"
               data-confirm="{% trans %}Do you really want to withdraw this request?{% endtrans %}"
               value="{%- trans %}Withdraw request{% endtrans -%}">
        {% endif %}
        <a href="{{ url_for('.event_requests', event) }}" class="i-button big">{% trans %}Back{% endtrans %}</a>
    {% endcall %}

    {% if show_management %}
        {{ form_header(manager_form, id='request-manage-form', classes='management-area', action=url_for('.event_requests_process', req)) }}
        {% call form_fieldset(legend=_('Manage service request')) %}
            {% if req.processed_by_user %}
                {{ form_row_static(_('Processed by'), req.processed_by_user.full_name) }}
            {% endif %}
            {% if req.processed_dt %}
                {{ form_row_static(_('Processed at'), req.processed_dt | format_datetime(timezone=req.event.tzinfo)) }}
            {% endif %}
            {% block manager_form %}
                {{ form_rows(manager_form, skip=manager_form.action_buttons) }}
            {% endblock %}
        {% endcall %}
        {% call form_footer(manager_form) %}
            {% if req.state.name in ('accepted', 'rejected') %}
                {{ manager_form.action_save(class_='i-button big') }}
            {% endif %}
            {% if manager_form.action_accept %}
                {{ manager_form.action_accept(class_='i-button big accept') }}
            {% endif %}
            {% if manager_form.action_reject %}
                {{ manager_form.action_reject(class_='i-button big danger') }}
            {% endif %}
        {% endcall %}
    {% endif %}

    {% block scripts %}{% endblock %}
{% endblock %}
