{% extends 'base.html' %}

{% block title %}Единицы измерения{% endblock %}

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

            <!-- Поиск и фильтры -->
            <form method="get" class="d-flex gap-2 mb-3">
                <input type="text" class="form-control form-control-sm" name="q" value="{{ search_query }}" placeholder="Поиск по коду, названию...">
                <select class="form-select form-select-sm" name="is_active" style="width: auto;" 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>
                <button type="submit" class="btn btn-outline-secondary btn-sm"><i class="bi bi-search"></i></button>
            </form>

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

            <!-- Список единиц измерения -->
            {% if page_obj %}
            <div class="table-responsive">
                <table class="table table-hover align-middle">
                    <thead class="table-light">
                        <tr>
                            <th style="width: 100px;">Код</th>
                            <th>Название</th>
                            <th style="width: 120px;">Краткое</th>
                            <th style="width: 100px;" class="text-center">Позиция</th>
                            <th style="width: 120px;" class="text-center">Использований</th>
                            <th style="width: 100px;" class="text-center">Статус</th>
                            <th style="width: 120px;" class="text-end">Действия</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for unit in page_obj %}
                        <tr>
                            <td><code>{{ unit.code }}</code></td>
                            <td>{{ unit.name }}</td>
                            <td><span class="badge bg-secondary">{{ unit.short_name }}</span></td>
                            <td class="text-center">{{ unit.position }}</td>
                            <td class="text-center">
                                <span class="badge {% if unit.usage_count > 0 %}bg-success{% else %}bg-secondary{% endif %}" title="Единицы продажи">
                                    {{ unit.usage_count }}
                                </span>
                                <span class="badge {% if unit.products_count > 0 %}bg-primary{% else %}bg-secondary{% endif %}" title="Товаров">
                                    {{ unit.products_count }}
                                </span>
                            </td>
                            <td class="text-center">
                                {% if unit.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:unit-update' unit.pk %}" class="btn btn-outline-secondary" title="Изменить">
                                        <i class="bi bi-pencil"></i>
                                    </a>
                                    <a href="{% url 'products:unit-delete' unit.pk %}" class="btn btn-outline-danger" 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 %}">&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 %}">&raquo;</a>
                    </li>
                    {% endif %}
                </ul>
            </nav>
            {% endif %}

            {% else %}
            <div class="text-center text-muted py-5">
                <i class="bi bi-rulers fs-1 opacity-25"></i>
                <p class="mb-0 mt-2">Единиц измерения пока нет</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-box-seam"></i> Единицы продажи товаров</h6>
                        <small class="text-muted">Настройка единиц продажи для конкретных товаров</small>
                    </div>
                    <a href="{% url 'products:sales-unit-list' %}" class="btn btn-outline-primary btn-sm">
                        Перейти <i class="bi bi-arrow-right"></i>
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
