{% extends 'inventory/base_inventory_minimal.html' %}
{% load inventory_filters %}
{% block inventory_title %}Партия {{ batch.document_number }}{% endblock %}
{% block breadcrumb_current %}Партии поступлений{% endblock %}
{% block inventory_content %}
<div class="card">
    <div class="card-header">
        <div class="d-flex justify-content-between align-items-center">
            <h4 class="mb-0">Партия: <strong>{{ batch.document_number }}</strong></h4>
            <a href="{% url 'inventory:incoming-batch-list' %}" class="btn btn-secondary btn-sm">
                <i class="bi bi-arrow-left"></i> Назад
            </a>
        </div>
    </div>
    <div class="card-body">
        <div class="row mb-4">
            <div class="col-md-6">
                <h5>Основная информация</h5>
                <table class="table table-sm table-borderless">
                    <tr>
                        <th>Номер:</th>
                        <td><strong>{{ batch.document_number }}</strong></td>
                    </tr>
                    <tr>
                        <th>Склад:</th>
                        <td>{{ batch.warehouse.name }}</td>
                    </tr>
                    <tr>
                        <th>Поставщик:</th>
                        <td>{{ batch.supplier_name|default:"—" }}</td>
                    </tr>
                    <tr>
                        <th>Создана:</th>
                        <td>{{ batch.created_at|date:"d.m.Y H:i" }}</td>
                    </tr>
                </table>
            </div>
            <div class="col-md-6">
                <h5>Статистика</h5>
                <table class="table table-sm table-borderless">
                    <tr>
                        <th>Товаров:</th>
                        <td><span class="badge bg-info">{{ items.count }}</span></td>
                    </tr>
                    <tr>
                        <th>Общее количество:</th>
                        <td>
                            {% with total=items.all|length %}
                                <strong>{{ total }} шт</strong>
                            {% endwith %}
                        </td>
                    </tr>
                </table>
            </div>
        </div>

        <h5 class="mt-4 mb-3">Товары в партии</h5>
        {% if items %}
        <div class="table-responsive">
            <table class="table table-hover table-sm">
                <thead>
                    <tr>
                        <th>Товар</th>
                        <th>Количество</th>
                        <th>Цена</th>
                        <th>Сумма</th>
                        <th>StockBatch ID</th>
                        <th>Дата</th>
                        <th>Действие</th>
                    </tr>
                </thead>
                <tbody>
                    {% for item in items %}
                    <tr>
                        <td>{{ item.product.name }}</td>
                        <td>{{ item.quantity|smart_quantity }}</td>
                        <td>{{ item.cost_price }} руб.</td>
                        <td>
                            {% widthratio item.quantity 1 item.cost_price as total_price %}
                            <strong>{{ total_price|floatformat:2 }} руб.</strong>
                        </td>
                        <td>
                            {% if item.stock_batch %}
                            <strong>#{{ item.stock_batch.pk }}</strong>
                            {% else %}
                            <span class="badge bg-warning">Не назначена</span>
                            {% endif %}
                        </td>
                        <td>{{ item.created_at|date:"d.m.Y" }}</td>
                        <td>
                            {% if item.stock_batch %}
                            <a href="{% url 'inventory:batch-detail' item.stock_batch.pk %}" class="btn btn-sm btn-outline-info" title="Просмотр партии на складе">
                                <i class="bi bi-arrow-right"></i>
                            </a>
                            {% endif %}
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
        {% else %}
        <div class="alert alert-info">В этой партии нет товаров.</div>
        {% endif %}

        {% if batch.notes %}
        <h5 class="mt-4 mb-3">Примечания</h5>
        <p>{{ batch.notes }}</p>
        {% endif %}
    </div>
</div>
{% endblock %}
