{% from 'batches/form.html' import batch_add_form %}


{% macro pane_details(batch, form) %}

    {% if batch.is_empty %}
        <div class="rounded border p-3 mb-3 opacity-40">
    {% else %}
        <div class="rounded border p-3 mb-3">
    {% endif %}

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

    <div class="flex-fill">
        <h5 class="mb-1">{{ batch.supplier }} {{ batch.article_number }}</h5>
        <p>Ordered on {{ batch.date_ordered | format_date }}</p>
    </div>

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

        {% if current_user.has_permission('Add consumable batches') %}

            <a class="text-primary me-2"
               data-bs-toggle="modal"
               data-bs-target="#modal-edit-batch-{{ batch.id }}">
                <i class="fa-solid fa-pen"></i></a>

            {% if (not batch.in_use) and (batch.date_emptied is none) %}
                <a class="text-info me-2"
                   onclick="batch_action('{{ url_for("batches.in_use", id_=batch.id) }}')">
                    <i class="fa-solid fa-box-open"></i></a>
            {% endif %}

            {% if batch.in_use and batch.date_emptied is none %}
                <a class="text-danger me-2"
                   onclick="batch_action('{{ url_for("batches.emptied", id_=batch.id) }}')">
                    <i class="fa-solid fa-battery-empty"></i></a>
            {% endif %}

            <a class="text-danger"
               onclick="delete_entity('{{ url_for("batches.delete", id_=batch.id) }}')">
                <i class="fa-solid fa-trash"></i></a>

        {% endif %}

    </div>

</div>

<div class="d-flex align-items-stretch">
    <div class="flex-fill pr-2 border-end">
        <p class="mb-0 text-center">Storage place</p>
        <p class="text-info fw-bold text-center">{{ batch.storage_place }}</p>
    </div>
    <div class="flex-fill pl-2 pr-2 border-end">
        <p class="mb-0 text-center">Lot number</p>
        <p class="fw-bold text-center">{{ batch.lot }}</p>
    </div>
    <div class="flex-fill pl-2 pr-2 border-end">
        <p class="mb-0 text-center">Amount</p>
        <p class="fw-bold text-center">{{ batch.amount }}</p>
    </div>
    <div class="flex-fill pl-2">
        <p class="mb-0 text-center">Price</p>
        <p class="fw-bold text-center">{{ batch.price }}€</p>
    </div>
</div>

</div>

    {{ batch_add_form(form(None, obj=batch), "batches.edit", "PUT", batch.id, batch.consumable_id) }}

{% endmacro %}


{% macro batch_pane(status, pane_id, consumable, 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">Batches</h2>
            <div class="flex-shrink-0">

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

            </div>
        </div>

        {% for batch in consumable.batches %}
            {{ pane_details(batch, form) }}
        {% else %}
            <div class="fw-bold text-center mb-3">No batches found!</div>
        {% endfor %}

        {{ batch_add_form(form(), "batches.add", "POST", -1, consumable.id) }}

    </div>
{% endmacro %}