{% extends 'base.html' %}

{% block title %}Единицы продажи товаров{% endblock %}

{% block content %}
<div class="container mt-4">
    <div class="row justify-content-center">
        <div class="col-lg-11">
            <!-- Заголовок -->
            <div class="d-flex align-items-center justify-content-between mb-4">
                <h4 class="mb-0"><i class="bi bi-box-seam text-primary"></i> Единицы продажи товаров</h4>
                <a href="{% url 'products:sales-unit-create' %}" class="btn btn-primary btn-sm">
                    <i class="bi bi-plus-circle"></i> Добавить единицу
                </a>
            </div>

            <!-- Поиск и фильтры -->
            <form method="get" class="mb-3">
                <div class="row g-2">
                    <div class="col-md-6">
                        <input type="text" class="form-control form-control-sm" name="q" value="{{ search_query }}" placeholder="Поиск по товару, артикулу, названию...">
                    </div>
                    <div class="col-md-2">
                        <select class="form-select form-select-sm" name="is_active" onchange="this.form.submit()">
                            <option value="">Все</option>
                            <option value="true" {% if is_active_filter == 'true' %}selected{% endif %}>Активные</option>
                            <option value="false" {% if is_active_filter == 'false' %}selected{% endif %}>Неактивные</option>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <select class="form-select form-select-sm" name="is_default" onchange="this.form.submit()">
                            <option value="">Все</option>
                            <option value="true" {% if is_default_filter == 'true' %}selected{% endif %}>По умолчанию</option>
                            <option value="false" {% if is_default_filter == 'false' %}selected{% endif %}>Не по умолч.</option>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <button type="submit" class="btn btn-outline-secondary btn-sm w-100">
                            <i class="bi bi-search"></i> Найти
                        </button>
                    </div>
                </div>
            </form>

            <!-- Статистика -->
            <div class="alert alert-info mb-3">
                <i class="bi bi-info-circle"></i> Всего единиц продажи: <strong>{{ total_sales_units }}</strong>
            </div>

            <!-- Список единиц продажи -->
            {% if page_obj %}
            <div class="table-responsive">
                <table class="table table-hover table-sm align-middle">
                    <thead class="table-light">
                        <tr>
                            <th style="width: 28%;">Товар</th>
                            <th style="width: 18%;">Название единицы</th>
                            <th style="width: 10%;" class="text-end">Коэфф.</th>
                            <th style="width: 12%;" class="text-end">Цена</th>
                            <th style="width: 8%;" class="text-center">Мин.</th>
                            <th style="width: 8%;" class="text-center">Шаг</th>
                            <th style="width: 8%;" class="text-center">По умолч.</th>
                            <th style="width: 8%;" class="text-center">Статус</th>
                            <th style="width: 10%;" class="text-end">Действия</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for su in page_obj %}
                        <tr>
                            <td>
                                <a href="{% url 'products:product-detail' su.product.pk %}" class="text-decoration-none">
                                    {{ su.product.name }}
                                </a>
                                {% if su.product.sku %}
                                    <br><small class="text-muted">{{ su.product.sku }}</small>
                                {% endif %}
                            </td>
                            <td>{{ su.name }}</td>
                            <td class="text-end"><code>{{ su.conversion_factor }}</code></td>
                            <td class="text-end">
                                {% if su.sale_price %}
                                    <span class="text-decoration-line-through text-muted">{{ su.price }}</span>
                                    <span class="text-danger fw-bold">{{ su.sale_price }}</span>
                                {% else %}
                                    {{ su.price }}
                                {% endif %}
                                <small class="text-muted">руб.</small>
                            </td>
                            <td class="text-center">
                                <small>{{ su.min_quantity }}</small>
                            </td>
                            <td class="text-center">
                                <small>{{ su.quantity_step }}</small>
                            </td>
                            <td class="text-center">
                                {% if su.is_default %}
                                    <i class="bi bi-check-circle-fill text-success" title="По умолчанию"></i>
                                {% else %}
                                    <i class="bi bi-circle text-muted" title="Не по умолчанию"></i>
                                {% endif %}
                            </td>
                            <td class="text-center">
                                {% if su.is_active %}
                                    <span class="badge bg-success">Акт.</span>
                                {% else %}
                                    <span class="badge bg-secondary">Неакт.</span>
                                {% endif %}
                            </td>
                            <td class="text-end">
                                <div class="btn-group btn-group-sm">
                                    <a href="{% url 'products:sales-unit-update' su.pk %}" class="btn btn-outline-secondary btn-sm" title="Изменить">
                                        <i class="bi bi-pencil"></i>
                                    </a>
                                    <a href="{% url 'products:sales-unit-delete' su.pk %}" class="btn btn-outline-danger btn-sm" title="Удалить">
                                        <i class="bi bi-trash"></i>
                                    </a>
                                </div>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>

            <!-- Пагинация -->
            {% if page_obj.has_other_pages %}
            <nav class="mt-3">
                <ul class="pagination pagination-sm justify-content-center mb-0">
                    {% if page_obj.has_previous %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% if search_query %}&q={{ search_query }}{% endif %}{% if is_active_filter %}&is_active={{ is_active_filter }}{% endif %}{% if is_default_filter %}&is_default={{ is_default_filter }}{% endif %}">&laquo;</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 }}{% if search_query %}&q={{ search_query }}{% endif %}{% if is_active_filter %}&is_active={{ is_active_filter }}{% endif %}{% if is_default_filter %}&is_default={{ is_default_filter }}{% endif %}">&raquo;</a>
                    </li>
                    {% endif %}
                </ul>
            </nav>
            {% endif %}

            {% else %}
            <div class="text-center text-muted py-5">
                <i class="bi bi-box-seam fs-1 opacity-25"></i>
                <p class="mb-0 mt-2">Единиц продажи пока нет</p>
                <p class="small">Добавьте единицы продажи для товаров через форму выше</p>
            </div>
            {% endif %}

            <!-- Ссылка обратно на единицы измерения -->
            <div class="mt-4 p-3 bg-light rounded">
                <div class="d-flex align-items-center justify-content-between">
                    <div>
                        <h6 class="mb-1"><i class="bi bi-rulers"></i> Справочник единиц измерения</h6>
                        <small class="text-muted">Управление базовыми единицами измерения</small>
                    </div>
                    <a href="{% url 'products:unit-list' %}" class="btn btn-outline-primary btn-sm">
                        Перейти <i class="bi bi-arrow-right"></i>
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
