{% extends 'base.html' %}

{% block title %}Документы поступления{% endblock %}

{% block content %}
<div class="container-fluid px-4 py-3">
    <!-- Header -->
    <div class="d-flex justify-content-between align-items-center mb-3">
        <h4 class="mb-0">
            <i class="bi bi-file-earmark-plus me-2"></i>Документы поступления
        </h4>
        <a href="{% url 'inventory:incoming-create' %}" class="btn btn-primary">
            <i class="bi bi-plus-lg me-1"></i>Создать документ
        </a>
    </div>

    <!-- Messages -->
    {% if messages %}
    {% for message in messages %}
    <div class="alert alert-{{ message.tags }} alert-dismissible fade show" role="alert">
        {{ message }}
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    {% endfor %}
    {% endif %}

    <!-- Table -->
    <div class="card border-0 shadow-sm">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead class="table-light">
                        <tr>
                            <th scope="col" class="px-3 py-2">Номер</th>
                            <th scope="col" class="px-3 py-2">Дата</th>
                            <th scope="col" class="px-3 py-2">Склад</th>
                            <th scope="col" class="px-3 py-2">Тип</th>
                            <th scope="col" class="px-3 py-2">Статус</th>
                            <th scope="col" class="px-3 py-2 text-end">Позиций</th>
                            <th scope="col" class="px-3 py-2 text-end">Количество</th>
                            <th scope="col" class="px-3 py-2">Создал</th>
                            <th scope="col" class="px-3 py-2"></th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for doc in documents %}
                        <tr>
                            <td class="px-3 py-2">
                                <a href="{% url 'inventory:incoming-detail' doc.pk %}" class="fw-semibold text-decoration-none">
                                    {{ doc.document_number }}
                                </a>
                            </td>
                            <td class="px-3 py-2">{{ doc.date|date:"d.m.Y" }}</td>
                            <td class="px-3 py-2">{{ doc.warehouse.name }}</td>
                            <td class="px-3 py-2">
                                <span class="badge bg-info">{{ doc.get_receipt_type_display }}</span>
                            </td>
                            <td class="px-3 py-2">
                                {% if doc.status == 'draft' %}
                                <span class="badge bg-warning text-dark">Черновик</span>
                                {% elif doc.status == 'confirmed' %}
                                <span class="badge bg-success">Проведён</span>
                                {% elif doc.status == 'cancelled' %}
                                <span class="badge bg-secondary">Отменён</span>
                                {% endif %}
                            </td>
                            <td class="px-3 py-2 text-end">{{ doc.items.count }}</td>
                            <td class="px-3 py-2 text-end">{{ doc.total_quantity }}</td>
                            <td class="px-3 py-2">
                                {% if doc.created_by %}{{ doc.created_by.name|default:doc.created_by.email }}{% else %}-{% endif %}
                            </td>
                            <td class="px-3 py-2 text-end">
                                <a href="{% url 'inventory:incoming-detail' doc.pk %}" class="btn btn-sm btn-outline-primary">
                                    <i class="bi bi-eye"></i>
                                </a>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="9" class="px-3 py-4 text-center text-muted">
                                <i class="bi bi-inbox fs-1 d-block mb-2"></i>
                                Документов поступления пока нет
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Pagination -->
    {% if page_obj.has_other_pages %}
    <nav class="mt-3">
        <ul class="pagination justify-content-center">
            {% if page_obj.has_previous %}
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.previous_page_number }}">Назад</a>
            </li>
            {% endif %}
            <li class="page-item disabled">
                <span class="page-link">{{ page_obj.number }} из {{ page_obj.paginator.num_pages }}</span>
            </li>
            {% if page_obj.has_next %}
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.next_page_number }}">Вперёд</a>
            </li>
            {% endif %}
        </ul>
    </nav>
    {% endif %}
</div>
{% endblock %}

