{% extends 'base.html' %}

{% block title %}{{ title }}{% endblock %}

{% block content %}
<div class="container mt-4">
    <div class="row justify-content-center">
        <div class="col-lg-8">
            <div class="card">
                <div class="card-header">
                    <h4 class="mb-0">{{ title }}</h4>
                </div>
                <div class="card-body">
                    <form method="post">
                        {% csrf_token %}

                        {% if form.non_field_errors %}
                            <div class="alert alert-danger">
                                {{ form.non_field_errors }}
                            </div>
                        {% endif %}

                        <!-- Товар -->
                        <div class="mb-3">
                            <label for="{{ form.product.id_for_label }}" class="form-label">
                                {{ form.product.label }} <span class="text-danger">*</span>
                            </label>
                            {{ form.product }}
                            {% if form.product.help_text %}
                                <small class="form-text text-muted">{{ form.product.help_text }}</small>
                            {% endif %}
                            {% if form.product.errors %}
                                <div class="text-danger">{{ form.product.errors }}</div>
                            {% endif %}
                        </div>

                        <!-- Название -->
                        <div class="mb-3">
                            <label for="{{ form.name.id_for_label }}" class="form-label">
                                {{ form.name.label }} <span class="text-danger">*</span>
                            </label>
                            {{ form.name }}
                            {% if form.name.help_text %}
                                <small class="form-text text-muted">{{ form.name.help_text }}</small>
                            {% endif %}
                            {% if form.name.errors %}
                                <div class="text-danger">{{ form.name.errors }}</div>
                            {% endif %}
                        </div>

                        <!-- Коэффициент конверсии -->
                        <div class="mb-3">
                            <label for="{{ form.conversion_factor.id_for_label }}" class="form-label">
                                {{ form.conversion_factor.label }} <span class="text-danger">*</span>
                            </label>
                            {{ form.conversion_factor }}
                            {% if form.conversion_factor.help_text %}
                                <small class="form-text text-muted d-block">
                                    <i class="bi bi-info-circle"></i> {{ form.conversion_factor.help_text }}
                                </small>
                            {% endif %}
                            {% if form.conversion_factor.errors %}
                                <div class="text-danger">{{ form.conversion_factor.errors }}</div>
                            {% endif %}
                        </div>

                        <!-- Цена и Цена со скидкой -->
                        <div class="row mb-3">
                            <div class="col-md-6">
                                <label for="{{ form.price.id_for_label }}" class="form-label">
                                    {{ form.price.label }} <span class="text-danger">*</span>
                                </label>
                                <div class="input-group">
                                    {{ form.price }}
                                    <span class="input-group-text">₽</span>
                                </div>
                                {% if form.price.help_text %}
                                    <small class="form-text text-muted">{{ form.price.help_text }}</small>
                                {% endif %}
                                {% if form.price.errors %}
                                    <div class="text-danger">{{ form.price.errors }}</div>
                                {% endif %}
                            </div>
                            <div class="col-md-6">
                                <label for="{{ form.sale_price.id_for_label }}" class="form-label">
                                    {{ form.sale_price.label }}
                                </label>
                                <div class="input-group">
                                    {{ form.sale_price }}
                                    <span class="input-group-text">₽</span>
                                </div>
                                {% if form.sale_price.help_text %}
                                    <small class="form-text text-muted">{{ form.sale_price.help_text }}</small>
                                {% endif %}
                                {% if form.sale_price.errors %}
                                    <div class="text-danger">{{ form.sale_price.errors }}</div>
                                {% endif %}
                            </div>
                        </div>

                        <!-- Минимальное количество и Шаг -->
                        <div class="row mb-3">
                            <div class="col-md-6">
                                <label for="{{ form.min_quantity.id_for_label }}" class="form-label">
                                    {{ form.min_quantity.label }} <span class="text-danger">*</span>
                                </label>
                                {{ form.min_quantity }}
                                {% if form.min_quantity.help_text %}
                                    <small class="form-text text-muted">{{ form.min_quantity.help_text }}</small>
                                {% endif %}
                                {% if form.min_quantity.errors %}
                                    <div class="text-danger">{{ form.min_quantity.errors }}</div>
                                {% endif %}
                            </div>
                            <div class="col-md-6">
                                <label for="{{ form.quantity_step.id_for_label }}" class="form-label">
                                    {{ form.quantity_step.label }} <span class="text-danger">*</span>
                                </label>
                                {{ form.quantity_step }}
                                {% if form.quantity_step.help_text %}
                                    <small class="form-text text-muted">{{ form.quantity_step.help_text }}</small>
                                {% endif %}
                                {% if form.quantity_step.errors %}
                                    <div class="text-danger">{{ form.quantity_step.errors }}</div>
                                {% endif %}
                            </div>
                        </div>

                        <!-- Флаги -->
                        <div class="mb-3">
                            <div class="form-check mb-2">
                                {{ form.is_default }}
                                <label class="form-check-label" for="{{ form.is_default.id_for_label }}">
                                    {{ form.is_default.label }}
                                </label>
                                {% if form.is_default.help_text %}
                                    <br><small class="form-text text-muted">{{ form.is_default.help_text }}</small>
                                {% endif %}
                            </div>
                            <div class="form-check mb-2">
                                {{ form.is_active }}
                                <label class="form-check-label" for="{{ form.is_active.id_for_label }}">
                                    {{ form.is_active.label }}
                                </label>
                            </div>
                        </div>

                        <!-- Позиция -->
                        <div class="mb-3">
                            <label for="{{ form.position.id_for_label }}" class="form-label">
                                {{ form.position.label }}
                            </label>
                            {{ form.position }}
                            <small class="form-text text-muted">Чем меньше число, тем выше в списке</small>
                        </div>

                        <!-- Кнопки -->
                        <div class="d-flex gap-2">
                            <button type="submit" class="btn btn-primary">
                                <i class="bi bi-check-circle"></i> {{ submit_text }}
                            </button>
                            <a href="{% url 'products:sales-unit-list' %}" class="btn btn-secondary">
                                <i class="bi bi-x-circle"></i> Отменить
                            </a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
