{% extends 'inventory/base_inventory_minimal.html' %}
{% load inventory_filters %}

{% block inventory_title %}История продаж{% endblock %}
{% block breadcrumb_current %}Продажи{% endblock %}

{% block inventory_content %}
<div class="card">
    <div class="card-header d-flex justify-content-between align-items-center">
        <h4 class="mb-0">Продажи товара (FIFO)</h4>
    </div>

    <div class="card-body">
        {% if sales %}
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead class="table-light">
                        <tr>
                            <th>Товар</th>
                            <th>Склад</th>
                            <th>Количество</th>
                            <th>Цена продажи</th>
                            <th>Сумма</th>
                            <th>Заказ</th>
                            <th>Статус</th>
                            <th>Дата</th>
                            <th class="text-end">Действия</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for sale in sales %}
                        <tr>
                            <td><strong>{{ sale.product.name }}</strong></td>
                            <td>{{ sale.warehouse.name }}</td>
                            <td>{{ sale.quantity|smart_quantity }} шт</td>
                            <td>{{ sale.sale_price }} руб.</td>
                            <td><strong>{{ sale.quantity|multiply:sale.sale_price|format_decimal:2 }} руб.</strong></td>
                            <td>
                                {% if sale.order %}
                                    <code>{{ sale.order.order_number }}</code>
                                {% else %}
                                    <span class="text-muted">—</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if sale.processed %}
                                    <span class="badge bg-success">Обработана</span>
                                {% else %}
                                    <span class="badge bg-warning">Ожидает</span>
                                {% endif %}
                            </td>
                            <td>{{ sale.date|date:"d.m.Y H:i" }}</td>
                            <td class="text-end">
                                <a href="{% url 'inventory:sale-detail' sale.pk %}" class="btn btn-sm btn-outline-info" title="Просмотр деталей">
                                    <i class="bi bi-eye"></i>
                                </a>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>

            <!-- Пагинация -->
            {% if is_paginated %}
            <nav aria-label="Page navigation">
                <ul class="pagination justify-content-center">
                    {% if page_obj.has_previous %}
                        <li class="page-item">
                            <a class="page-link" href="?page=1">Первая</a>
                        </li>
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.previous_page_number }}">Предыдущая</a>
                        </li>
                    {% endif %}

                    {% for num in page_obj.paginator.page_range %}
                        {% if page_obj.number == num %}
                            <li class="page-item active">
                                <span class="page-link">{{ num }}</span>
                            </li>
                        {% else %}
                            <li class="page-item">
                                <a class="page-link" href="?page={{ num }}">{{ num }}</a>
                            </li>
                        {% endif %}
                    {% endfor %}

                    {% if page_obj.has_next %}
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.next_page_number }}">Следующая</a>
                        </li>
                        <li class="page-item">
                            <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}">Последняя</a>
                        </li>
                    {% endif %}
                </ul>
            </nav>
            {% endif %}
        {% else %}
            <div class="alert alert-info">
                <i class="bi bi-info-circle"></i> Продаж не найдено.
            </div>
        {% endif %}
    </div>
</div>
{% endblock %}
