{% import "helper.twig" as helper %}
{% import "label_system/dropdown_macro.html.twig" as dropdown %}

{% include "parts/info/_withdraw_modal.html.twig" %}
{% include "parts/info/_stocktake_modal.html.twig" %}

<div class="table-responsive">
    <table class="table table-striped table-hover">
        <thead>
        <tr>
            <th>{% trans %}part_lots.description{% endtrans %}</th>
            <th>{% trans %}part_lots.storage_location{% endtrans %}</th>
            <th>{% trans %}part_lots.amount{% endtrans %}</th>
            <th></th> {# Tags row #}
            <th></th>
            <th></th> {# Button row #}
            <th></th>
        </tr>
        </thead>

        <tbody>
        {% for lot in part.partLots %}
            {# @var lot App\Entity\Parts\PartLot #}
            <tr {% if lot.id == highlightLotId %}class="table-primary row-highlight row-pulse"{% endif %}>
                <td>{{ lot.description }}</td>
                <td>
                    {% if lot.storageLocation %}
                        {{ helper.structural_entity_link(lot.storageLocation) }}
                    {% else %}
                        <span class="badge rounded-pill text-bg-warning">
                        <i class="fas fa-question-circle fa-fw"></i> {% trans %}part_lots.location_unknown{% endtrans %}
                    </span>
                    {% endif %}
                </td>
                <td>
                    {% if lot.instockUnknown %}
                        <span class="badge rounded-pill text-bg-warning">
                        <i class="fas fa-question-circle fa-fw"></i> {% trans %}part_lots.instock_unknown{% endtrans %}
                    </span>
                    {% else %}
                        {{ lot.amount | format_amount(part.partUnit, {'decimals': 5}) }}
                    {% endif %}
                </td>
                <td >
                    <div class="d-flex flex-column align-items-start">
                        {% if lot.owner  %}
                            <span class="badge text-bg-light mb-1" title="{% trans %}part_lot.owner{% endtrans %}">
                                {{ helper.user_icon_link(lot.owner) }}
                            </span>
                        {% endif %}
                        {% if lot.expirationDate %}
                            <span class="badge text-bg-info mb-1" title="{% trans %}part_lots.expiration_date{% endtrans %}">
                            <i class="fas fa-calendar-alt fa-fw"></i> {{ lot.expirationDate | format_date() }}<br>
                        </span>
                        {% endif %}
                        {% if lot.expired %}
                            <span class="badge text-bg-warning mb-1">
                            <i class="fas fa-exclamation-circle fa-fw"></i>
                            {% trans %}part_lots.is_expired{% endtrans %}
                        </span>
                        {% endif %}
                        {% if lot.needsRefill %}
                            <span class="badge text-bg-warning mb-1">
                                <i class="fas fa-dolly fa-fw"></i>
                                {% trans %}part_lots.need_refill{% endtrans %}
                            </span>
                        {% endif %}
                        {% if lot.lastStocktakeAt %}
                            <span class="badge text-bg-secondary" title="{% trans %}part_lot.edit.last_stocktake_at{% endtrans %}">
                                <i class="fas fa-clipboard-check fa-fw"></i>
                                {{ lot.lastStocktakeAt | format_datetime("short") }}
                            </span>
                        {% endif %}
                    </div>
                </td>
                <td>
                    <div class="btn-group" role="group">
                        <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#withdraw-modal"
                                data-action="withdraw" data-lot-id="{{ lot.id }}" data-lot-amount="{{ lot.amount }}"
                                title="{% trans %}part.info.withdraw_modal.title.withdraw{% endtrans %}"
                                {% if not is_granted('withdraw', lot) or not withdraw_add_helper.canWithdraw(lot) %}disabled{% endif %}
                        >
                            <i class="fa-solid fa-minus fa-fw"></i>
                        </button>
                        <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#withdraw-modal"
                                data-action="add" data-lot-id="{{ lot.id }}" data-lot-amount="{{ lot.amount }}"
                                title="{% trans %}part.info.withdraw_modal.title.add{% endtrans %}"
                                {% if not is_granted('add', lot) or not withdraw_add_helper.canAdd(lot) %}disabled{% endif %}
                        >
                            <i class="fa-solid fa-plus fa-fw"></i>
                        </button>
                        <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#withdraw-modal"
                                data-action="move" data-lot-id="{{ lot.id }}" data-lot-amount="{{ lot.amount }}"
                                title="{% trans %}part.info.withdraw_modal.title.move{% endtrans %}"
                                {% if not is_granted('move', lot) or not withdraw_add_helper.canWithdraw(lot) or part.partLots.count == 1 %}disabled{% endif %}
                        >
                            <i class="fa-solid fa-right-left fa-fw"></i>
                        </button>

                        <button type="button" class="btn-primary btn" data-bs-toggle="modal" data-bs-target="#stocktake-modal" title="{% trans %}part.info.stocktake_modal.title{% endtrans %}"
                                {% if not is_granted('stocktake', lot) %}disabled{% endif %}
                                data-lot-id="{{ lot.id }}" data-lot-amount="{{ lot.amount }}"><i class="fas fa-clipboard-check fa-fw"></i></button>
                    </div>
                </td>
                <td>
                    {{ dropdown.profile_dropdown('part_lot', lot.id, false) }}
                </td>
                <td> {# Action for order information #}
                    <div class="btn-group">
                        <button type="button" class="btn btn-outline-secondary dropdown-toggle" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                            <i class="fa-solid fa-lightbulb fa-fw"></i>
                        </button>
                        <div class="dropdown-menu">
                            <span class="text-muted dropdown-item-text" ><i class="fas fa-lightbulb fa-fw"></i> <b>{% trans %}id.label{% endtrans %}:</b> {{ lot.iD }}</span>
                            <span class="text-muted dropdown-item-text" ><i class="fas fa-history fa-fw"></i> <b>{% trans %}lastModified{% endtrans %}:</b> {{ helper.entity_last_modified(lot) }}</span>
                            <span class="text-muted dropdown-item-text" ><i class="fas fa-calendar-plus fa-fw"></i> <b>{% trans %}createdAt{% endtrans %}:</b> {{ helper.entity_created_at(lot) }}</span>
                            {% if lot.comment is not empty %}
                                <span class="text-muted dropdown-item-text" ><i class="fas fa-sticky-note fa-fw"></i> <b>{% trans %}part_lot.edit.comment{% endtrans %}:</b> {{ lot.comment }}</span>
                            {% endif %}
                        </div>
                    </div>
                </td>
            </tr>
        {% endfor %}
        </tbody>

    </table>
</div>
