{% from 'requests/form.html' import requests_add_form %}


{% macro pane_details(request, form) %}

    <div class="rounded border p-3 mb-3">

        <div class="d-flex flex-start align-items-start m3-2">

            <div class="flex-grow-1">
                <h5 class="mb-1">{{ request.requested_by }}</h5>
                <p class="text-muted mb-1">
                    Requested on {{ request.timestamp | format_date }}
                    {% if request.timestamp_sent %}
                        (sent on {{ request.timestamp_sent | format_date }})
                    {% else %}
                        (not yet sent)
                    {% endif %}
                </p>
            </div>

            <div class="flex-shrink-0">

                {% if current_user.has_permission('Add requests') %}
                    <a class="text-primary mx-2"
                       data-bs-toggle="modal"
                       data-bs-target="#modal-edit-request-{{ request.id }}">
                        <i class="fa-solid fa-pen"></i></a>
                    <a class="text-danger"
                       onclick="delete_entity('{{ url_for("requests.delete", id_=request.id) }}')">
                        <i class="fa-solid fa-trash"></i></a>
                {% endif %}

            </div>
        </div>

        <p class="mb-0">
            {{ request.note }}
        </p>

    </div>
    </div>

    {{ requests_add_form(form(None, obj=request), "requests.edit", "PUT", request.id, request.entity_id) }}

{% endmacro %}


{% macro request_pane(status, pane_id, entity, form) %}
    <div class="tab-pane container {{ status }}" id="{{ pane_id }}">

        <div class="d-flex justify-content-between align-items-center mb-3">
            <h2 class="flex-grow-1 mb-0">Requests</h2>
            <div class="flex-shrink-0">

                {% if current_user.has_permission('Add requests') %}
                    <button
                        type="button"
                        class="btn btn-success btn-sm"
                        data-bs-toggle="modal"
                        data-bs-target="#modal-edit-request--1">
                        <i class="fa-solid fa-plus"></i>
                    </button>
                {% endif %}

            </div>
        </div>


        {% for request in entity.requests %}
            {{ pane_details(request, form) }}
        {% else %}
            <div class="fw-bold text-center mb-3">Not requested yet!</div>
        {% endfor %}

        {{ requests_add_form(form(), "requests.add", "POST", -1, entity.id) }}

    </div>
{% endmacro %}
