{% extends 'base.html' %}

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

{% block content %}
<div class="container mt-4">
    <div class="row justify-content-center">
        <div class="col-lg-6">
            <!-- Заголовок -->
            <div class="d-flex align-items-center justify-content-between mb-4">
                <h4 class="mb-0"><i class="bi bi-rulers text-primary"></i> {{ title }}</h4>
                <a href="{% url 'products:unit-list' %}" class="btn btn-outline-secondary btn-sm">
                    <i class="bi bi-arrow-left"></i> К списку
                </a>
            </div>

            <!-- Форма -->
            <div class="card">
                <div class="card-body">
                    <form method="post" novalidate>
                        {% csrf_token %}
                        
                        {% if form.non_field_errors %}
                        <div class="alert alert-danger">
                            {% for error in form.non_field_errors %}
                            <p class="mb-0">{{ error }}</p>
                            {% endfor %}
                        </div>
                        {% endif %}

                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="{{ form.code.id_for_label }}" class="form-label">
                                    Код <span class="text-danger">*</span>
                                </label>
                                {{ form.code }}
                                {% if form.code.help_text %}
                                <div class="form-text">{{ form.code.help_text }}</div>
                                {% endif %}
                                {% if form.code.errors %}
                                <div class="invalid-feedback d-block">
                                    {% for error in form.code.errors %}{{ error }}{% endfor %}
                                </div>
                                {% endif %}
                            </div>

                            <div class="col-md-6 mb-3">
                                <label for="{{ form.short_name.id_for_label }}" class="form-label">
                                    Сокращение
                                </label>
                                {{ form.short_name }}
                                {% if form.short_name.help_text %}
                                <div class="form-text">{{ form.short_name.help_text }}</div>
                                {% endif %}
                            </div>
                        </div>

                        <div class="mb-3">
                            <label for="{{ form.name.id_for_label }}" class="form-label">
                                Название <span class="text-danger">*</span>
                            </label>
                            {{ form.name }}
                            {% if form.name.help_text %}
                            <div class="form-text">{{ form.name.help_text }}</div>
                            {% endif %}
                            {% if form.name.errors %}
                            <div class="invalid-feedback d-block">
                                {% for error in form.name.errors %}{{ error }}{% endfor %}
                            </div>
                            {% endif %}
                        </div>

                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="{{ form.position.id_for_label }}" class="form-label">
                                    Позиция
                                </label>
                                {{ form.position }}
                                {% if form.position.help_text %}
                                <div class="form-text">{{ form.position.help_text }}</div>
                                {% endif %}
                            </div>

                            <div class="col-md-6 mb-3 d-flex align-items-end">
                                <div class="form-check">
                                    {{ form.is_active }}
                                    <label class="form-check-label" for="{{ form.is_active.id_for_label }}">
                                        Активна
                                    </label>
                                </div>
                            </div>
                        </div>

                        <hr>

                        <div class="d-flex justify-content-between">
                            <a href="{% url 'products:unit-list' %}" class="btn btn-outline-secondary">
                                Отмена
                            </a>
                            <button type="submit" name="submit" class="btn btn-primary">
                                <i class="bi bi-check-lg"></i> {{ submit_text }}
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
