{% extends 'inventory/base_inventory_minimal.html' %}

{% block inventory_title %}
    {% if form.instance.pk %}
        Редактирование склада
    {% else %}
        Создание нового склада
    {% endif %}
{% endblock %}
{% block breadcrumb_current %}Склады{% endblock %}

{% block inventory_content %}
<div class="card">
    <div class="card-header">
        <h4 class="mb-0">
            {% if form.instance.pk %}
                Редактирование: {{ form.instance.name }}
            {% else %}
                Создание нового склада
            {% endif %}
        </h4>
    </div>

    <div class="card-body">
        <form method="post" class="form">
            {% csrf_token %}

            <div class="mb-3">
                <label for="{{ form.name.id_for_label }}" class="form-label">
                    {{ form.name.label }}
                </label>
                <input type="text" class="form-control {% if form.name.errors %}is-invalid{% endif %}"
                       id="{{ form.name.id_for_label }}" name="{{ form.name.html_name }}"
                       value="{{ form.name.value|default:'' }}" required>
                {% if form.name.errors %}
                    <div class="invalid-feedback">
                        {% for error in form.name.errors %}
                            {{ error }}
                        {% endfor %}
                    </div>
                {% endif %}
            </div>

            <div class="mb-3">
                <label for="{{ form.description.id_for_label }}" class="form-label">
                    {{ form.description.label }}
                </label>
                <textarea class="form-control {% if form.description.errors %}is-invalid{% endif %}"
                          id="{{ form.description.id_for_label }}" name="{{ form.description.html_name }}"
                          rows="3">{{ form.description.value|default:'' }}</textarea>
                {% if form.description.errors %}
                    <div class="invalid-feedback">
                        {% for error in form.description.errors %}
                            {{ error }}
                        {% endfor %}
                    </div>
                {% endif %}
            </div>

            <h5 class="mt-4 mb-3">Адрес</h5>

            <div class="row">
                <div class="col-md-8 mb-3">
                    <label for="{{ form.street.id_for_label }}" class="form-label">
                        {{ form.street.label }}
                    </label>
                    {{ form.street }}
                    {% if form.street.errors %}
                        <div class="invalid-feedback d-block">
                            {% for error in form.street.errors %}
                                {{ error }}
                            {% endfor %}
                        </div>
                    {% endif %}
                </div>

                <div class="col-md-4 mb-3">
                    <label for="{{ form.building_number.id_for_label }}" class="form-label">
                        {{ form.building_number.label }}
                    </label>
                    {{ form.building_number }}
                    {% if form.building_number.errors %}
                        <div class="invalid-feedback d-block">
                            {% for error in form.building_number.errors %}
                                {{ error }}
                            {% endfor %}
                        </div>
                    {% endif %}
                </div>
            </div>

            <h5 class="mt-4 mb-3">Контакты</h5>

            <div class="row">
                <div class="col-md-6 mb-3">
                    <label for="{{ form.phone.id_for_label }}" class="form-label">
                        {{ form.phone.label }}
                    </label>
                    {{ form.phone }}
                    {% if form.phone.errors %}
                        <div class="invalid-feedback d-block">
                            {% for error in form.phone.errors %}
                                {{ error }}
                            {% endfor %}
                        </div>
                    {% endif %}
                </div>

                <div class="col-md-6 mb-3">
                    <label for="{{ form.email.id_for_label }}" class="form-label">
                        {{ form.email.label }}
                    </label>
                    {{ form.email }}
                    {% if form.email.errors %}
                        <div class="invalid-feedback d-block">
                            {% for error in form.email.errors %}
                                {{ error }}
                            {% endfor %}
                        </div>
                    {% endif %}
                </div>
            </div>

            <h5 class="mt-4 mb-3">Настройки</h5>

            <div class="mb-3">
                <div class="form-check">
                    <input type="checkbox" class="form-check-input"
                           id="{{ form.is_active.id_for_label }}" name="{{ form.is_active.html_name }}"
                           {% if form.is_active.value %}checked{% endif %}>
                    <label class="form-check-label" for="{{ form.is_active.id_for_label }}">
                        {{ form.is_active.label }}
                    </label>
                </div>
            </div>

            <div class="mb-3">
                <div class="form-check">
                    <input type="checkbox" class="form-check-input"
                           id="{{ form.is_default.id_for_label }}" name="{{ form.is_default.html_name }}"
                           {% if form.is_default.value %}checked{% endif %}>
                    <label class="form-check-label" for="{{ form.is_default.id_for_label }}">
                        {{ form.is_default.label }}
                        <small class="text-muted d-block">
                            {{ form.is_default.help_text }}
                        </small>
                    </label>
                </div>
            </div>

            <div class="mb-3">
                <div class="form-check">
                    <input type="checkbox" class="form-check-input"
                           id="{{ form.is_pickup_point.id_for_label }}" name="{{ form.is_pickup_point.html_name }}"
                           {% if form.is_pickup_point.value %}checked{% endif %}>
                    <label class="form-check-label" for="{{ form.is_pickup_point.id_for_label }}">
                        {{ form.is_pickup_point.label }}
                        <small class="text-muted d-block">
                            {{ form.is_pickup_point.help_text }}
                        </small>
                    </label>
                </div>
            </div>

            <div class="d-flex gap-2">
                <button type="submit" class="btn btn-primary">
                    <i class="bi bi-check-circle"></i>
                    {% if form.instance.pk %}
                        Сохранить
                    {% else %}
                        Создать
                    {% endif %}
                </button>
                <a href="{% url 'inventory:warehouse-list' %}" class="btn btn-secondary">
                    <i class="bi bi-x-circle"></i> Отменить
                </a>
            </div>
        </form>
    </div>
</div>
{% endblock %}
