{% extends "postman/base.html" %}
{% load i18n %}{% load postman_tags df_site django_bootstrap5 %}
{% block content %}
    <div id="postman">
        <h1 class="card-header text-center">{% if pm_messages|length > 1 %}{% trans "Conversation" %}{% else %}{% trans "Message" %}{% endif %}</h1>
        {% paginate_qs pm_messages per_page=10 as pm_messages_p %}
        {% for message in pm_messages_p %}
            <div
                class="card m-2 pm_message {% if forloop.last %}text-body-emphasis {% else %}text-body-tertiary {% endif %}{% if message.is_pending %} pm_pending{% endif %}{% if message.is_rejected %} pm_rejected{% endif %}{% if message.sender == user and message.sender_archived or message.recipient == user and message.recipient_archived %} pm_archived{% endif %}{% if message.sender == user and message.sender_deleted_at or message.recipient == user and message.recipient_deleted_at %} pm_deleted{% endif %}{% if message.recipient == user and not message.read_at %} pm_unread{% endif %}">
                <div class="pm_header card-header">
                    {% if message.sender == request.user %}{% fa6_icon 'sign-out-alt' %}{% else %}{% fa6_icon 'sign-in-alt' %}{% endif %}
                    <strong class="pm_subject">{{ message.subject|safe }}</strong>
                    {% if message.sender == request.user %} {% else %}
                        <small class="pm_sender">{{ message.obfuscated_sender|or_me:user }}</small>
                    {% endif %}
                    {% if message.sender == request.user %}<small class="pm_recipient">{{ message.obfuscated_recipient|or_me:user }}</small> {% else %}

                    {% endif %}
                    <small class="pm_date float-end ">{{ message.sent_at|date:"DATETIME_FORMAT" }}</small>
                </div>
                {% if message.is_rejected %}
                    <div class="pm_status alert alert-danger px-3 py-1 border-bottom"><strong>{% trans "Rejected" %}</strong>{% if message.moderation_reason %}{% trans ":" %}
                        {{ message.moderation_reason }}{% endif %}</div>
                {% endif %}
                <div class="card-body p-3 pm_body ">{{ message.body|safe }}</div>
            </div>
            {% if forloop.last %}
                {% bootstrap_pagination pm_messages_p size='sm' justify_content='center' %}
                <form class="card-body" method="post" action="

                    {% block pm_form_action %}{% url 'postman:update-message' %}?next={{ next_url|urlencode }}{% endblock %}">{% csrf_token %}
                    <input type="hidden" {% if pm_messages_p|length > 1 and message.thread_id %}name="tpks"
                           value="{{ message.thread_id }}" {% else %}name="pks" value="{{ message.pk }}"{% endif %}>
                    <div id="pm_buttons" class="btn-group">
                        <a href="{{ next_url }}"
                           class="pm_action pm_action-back btn btn-sm btn-primary">{% trans "Back" %}</a>
                        <button name="action" value="delete" type="submit"
                                class="pm_btn pm_btn-delete btn btn-sm btn-danger">{% trans "Delete" %}</button>
                        {% if not archived %}
                            <button name="action" value="archive" type="submit"
                                    class="pm_btn pm_btn-archive btn btn-sm btn-info">{% trans "Archive" %}</button>{% endif %}
                        {% if reply_to_pk %}
                            <a href="{% url 'postman:reply' reply_to_pk %}?next={{ next_url|urlencode }}"
                               class="pm_action pm_action-reply btn btn-sm btn-primary">{% trans "Reply" %}</a>{% endif %}
                    </div>
                </form>

                {% if reply_to_pk %}
                    <h2 class="card-header border-top text-center">{% trans 'Reply' %}</h2>
                    <form class="card-body"
                          action="{% url 'postman:reply' reply_to_pk %}?next={{ next_url|urlencode }}"
                          method="post">{% csrf_token %}
                        <div id="pm_reply">{% bootstrap_form form layout='floating' %}</div>

                        <div class="btn-group">
                            <button type="submit"
                                    class="pm_btn pm_btn-reply btn btn-sm btn-primary">{% trans 'Reply' %}</button>
                        </div>
                    </form>{% endif %}
            {% endif %}
        {% empty %}
        {% endfor %}
    </div>
{% endblock %}
