{% extends 'base.html' %}
{% load static %}

{% block title %}
    {% if object %}Редактирование {{ object.name }}{% else %}Добавление техники{% endif %} | Fleet Manager
{% endblock %}

{% block content %}
<div class="row justify-content-center">
    <div class="col-lg-8">
        <div class="mb-4">
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb mb-2">
                    <li class="breadcrumb-item"><a href="{% url 'index' %}" class="text-decoration-none">Дашборд</a></li>
                    <li class="breadcrumb-item"><a href="{% url 'fleet_list' %}" class="text-decoration-none">Техника</a></li>
                    <li class="breadcrumb-item active" aria-current="page">
                        {% if object %}Редактирование{% else %}Добавление{% endif %}
                    </li>
                </ol>
            </nav>
            <h2 class="mb-0">{% if object %}Редактирование {{ object.name }}{% else %}Новая единица техники{% endif %}</h2>
        </div>

        <div class="card shadow-sm">
            <div class="card-body p-4">
                <form method="post" enctype="multipart/form-data">
                    {% csrf_token %}
                    
                    <div class="row g-3">
                        <div class="col-md-12">
                            <label for="{{ form.name.id_for_label }}" class="form-label fw-medium">{{ form.name.label }}</label>
                            {{ form.name }}
                            {% if form.name.errors %}<div class="text-danger small">{{ form.name.errors }}</div>{% endif %}
                        </div>
                        
                        <div class="col-md-6">
                            <label for="{{ form.category.id_for_label }}" class="form-label fw-medium">{{ form.category.label }}</label>
                            {{ form.category }}
                            {% if form.category.errors %}<div class="text-danger small">{{ form.category.errors }}</div>{% endif %}
                        </div>
                        
                        <div class="col-md-6">
                            <label for="{{ form.model_name.id_for_label }}" class="form-label fw-medium">{{ form.model_name.label }}</label>
                            {{ form.model_name }}
                            {% if form.model_name.errors %}<div class="text-danger small">{{ form.model_name.errors }}</div>{% endif %}
                        </div>
                        
                        <div class="col-md-6">
                            <label for="{{ form.vin.id_for_label }}" class="form-label fw-medium">{{ form.vin.label }}</label>
                            {{ form.vin }}
                            {% if form.vin.errors %}<div class="text-danger small">{{ form.vin.errors }}</div>{% endif %}
                        </div>
                        
                        <div class="col-md-6">
                            <label for="{{ form.plate_number.id_for_label }}" class="form-label fw-medium">{{ form.plate_number.label }}</label>
                            {{ form.plate_number }}
                            {% if form.plate_number.errors %}<div class="text-danger small">{{ form.plate_number.errors }}</div>{% endif %}
                        </div>
                        
                        <div class="col-md-4">
                            <label for="{{ form.year.id_for_label }}" class="form-label fw-medium">{{ form.year.label }}</label>
                            {{ form.year }}
                            {% if form.year.errors %}<div class="text-danger small">{{ form.year.errors }}</div>{% endif %}
                        </div>
                        
                        <div class="col-md-4">
                            <label for="{{ form.status.id_for_label }}" class="form-label fw-medium">{{ form.status.label }}</label>
                            {{ form.status }}
                            {% if form.status.errors %}<div class="text-danger small">{{ form.status.errors }}</div>{% endif %}
                        </div>
                        
                        <div class="col-md-4">
                            <label for="{{ form.commissioning_date.id_for_label }}" class="form-label fw-medium">{{ form.commissioning_date.label }}</label>
                            {{ form.commissioning_date }}
                            <div class="form-text small">ГГГГ-ММ-ДД</div>
                            {% if form.commissioning_date.errors %}<div class="text-danger small">{{ form.commissioning_date.errors }}</div>{% endif %}
                        </div>
                        
                        <div class="col-md-12">
                            <label for="{{ form.photo.id_for_label }}" class="form-label fw-medium">{{ form.photo.label }}</label>
                            {{ form.photo }}
                            {% if form.photo.errors %}<div class="text-danger small">{{ form.photo.errors }}</div>{% endif %}
                        </div>
                        
                        <div class="col-md-12">
                            <label for="{{ form.notes.id_for_label }}" class="form-label fw-medium">{{ form.notes.label }}</label>
                            {{ form.notes }}
                            {% if form.notes.errors %}<div class="text-danger small">{{ form.notes.errors }}</div>{% endif %}
                        </div>
                    </div>
                    
                    <div class="d-flex gap-2 mt-5">
                        <button type="submit" class="btn btn-primary px-5">
                            {% if object %}Сохранить изменения{% else %}Добавить технику{% endif %}
                        </button>
                        <a href="{% if object %}{{ object.get_absolute_url }}{% else %}{% url 'fleet_list' %}{% endif %}" class="btn btn-outline-secondary">Отмена</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
    // Add bootstrap classes to form fields
    document.querySelectorAll('input, select, textarea').forEach(el => {
        if (el.type !== 'checkbox' && el.type !== 'radio') {
            el.classList.add('form-control');
        }
        if (el.tagName === 'SELECT') {
            el.classList.add('form-select');
        }
    });
</script>
{% endblock %}
