{% 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-arrow-repeat me-2"></i>Трансформации товаров
        </h4>
        <a href="{% url 'inventory:transformation-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">Выходной товар</th>
                            <th scope="col" class="px-3 py-2">Сотрудник</th>
                            <th scope="col" class="px-3 py-2"></th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for transformation in transformations %}
                        <tr>
                            <td class="px-3 py-2">
                                <a href="{% url 'inventory:transformation-detail' transformation.pk %}" class="fw-semibold text-decoration-none">
                                    {{ transformation.document_number }}
                                </a>
                            </td>
                            <td class="px-3 py-2">{{ transformation.date|date:"d.m.Y H:i" }}</td>
                            <td class="px-3 py-2">{{ transformation.warehouse.name }}</td>
                            <td class="px-3 py-2">
                                {% if transformation.status == 'draft' %}
                                <span class="badge bg-warning text-dark">Черновик</span>
                                {% elif transformation.status == 'completed' %}
                                <span class="badge bg-success">Проведён</span>
                                {% elif transformation.status == 'cancelled' %}
                                <span class="badge bg-secondary">Отменён</span>
                                {% endif %}
                            </td>
                            <td class="px-3 py-2">
                                {% for input in transformation.inputs.all %}
                                    <div class="small">{{ input.product.name }} - {{ input.quantity }} шт</div>
                                {% empty %}
                                    <span class="text-muted">-</span>
                                {% endfor %}
                            </td>
                            <td class="px-3 py-2">
                                {% for output in transformation.outputs.all %}
                                    <div class="small">{{ output.product.name }} - {{ output.quantity }} шт</div>
                                {% empty %}
                                    <span class="text-muted">-</span>
                                {% endfor %}
                            </td>
                            <td class="px-3 py-2">
                                {% if transformation.employee %}{{ transformation.employee.name|default:transformation.employee.email }}{% else %}-{% endif %}
                            </td>
                            <td class="px-3 py-2 text-end">
                                <a href="{% url 'inventory:transformation-detail' transformation.pk %}" class="btn btn-sm btn-outline-primary">
                                    <i class="bi bi-eye"></i>
                                </a>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="8" 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 is_paginated %}
    <nav aria-label="Page navigation" class="mt-3">
        <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 }}">
                    <i class="bi bi-chevron-left"></i>
                </a>
            </li>
            {% endif %}

            <li class="page-item active">
                <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 }}">
                    <i class="bi bi-chevron-right"></i>
                </a>
            </li>
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}">Последняя</a>
            </li>
            {% endif %}
        </ul>
    </nav>
    {% endif %}
</div>
{% endblock %}
