{% extends 'admin/base.html' %}

{% block content %}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        {% if service %}
            <h1><i class="fas fa-edit me-2"></i> Modifier un service</h1>
        {% else %}
            <h1><i class="fas fa-plus-circle me-2"></i> Ajouter un service</h1>
        {% endif %}
        <a href="{{ url_for('admin.services') }}" class="btn btn-secondary">
            <i class="fas fa-arrow-left me-2"></i> Retour à la liste
        </a>
    </div>
    
    <div class="card border-0 shadow-sm">
        <div class="card-body p-4">
            <form method="post" enctype="multipart/form-data" class="needs-validation" novalidate>
                {{ form.hidden_tag() }}
                
                <div class="row">
                    <div class="col-md-8">
                        <div class="form-group mb-3">
                            {{ form.name.label(class="form-label") }}
                            {{ form.name(class="form-control" + (" is-invalid" if form.name.errors else "")) }}
                            {% if form.name.errors %}
                                <div class="invalid-feedback">
                                    {% for error in form.name.errors %}
                                        {{ error }}
                                    {% endfor %}
                                </div>
                            {% endif %}
                        </div>
                        
                        <div class="form-group mb-3">
                            {{ form.description.label(class="form-label") }}
                            {{ form.description(class="form-control" + (" is-invalid" if form.description.errors else ""), rows=8) }}
                            {% if form.description.errors %}
                                <div class="invalid-feedback">
                                    {% for error in form.description.errors %}
                                        {{ error }}
                                    {% endfor %}
                                </div>
                            {% endif %}
                        </div>
                        
                        <div class="form-check mb-3">
                            {{ form.is_active(class="form-check-input") }}
                            {{ form.is_active.label(class="form-check-label") }}
                        </div>
                    </div>
                    
                    <div class="col-md-4">
                        <div class="card mb-3">
                            <div class="card-header bg-light">
                                <h5 class="mb-0">Image du service</h5>
                            </div>
                            <div class="card-body">
                                <div class="form-group mb-3">
                                    {{ form.image.label(class="form-label") }}
                                    {{ form.image(class="form-control" + (" is-invalid" if form.image.errors else ""), data_preview="imagePreview") }}
                                    {% if form.image.errors %}
                                        <div class="invalid-feedback">
                                            {% for error in form.image.errors %}
                                                {{ error }}
                                            {% endfor %}
                                        </div>
                                    {% endif %}
                                    <div class="form-text">Format recommandé: JPG, PNG (max 2MB)</div>
                                </div>
                                
                                <div class="text-center mt-3">
                                    {% if service and service.image_url %}
                                        <img src="{{ url_for('static', filename=service.image_url) }}" alt="{{ service.name }}" id="imagePreview" class="img-preview img-fluid rounded" style="max-height: 200px;">
                                    {% else %}
                                        <img src="" alt="Aperçu de l'image" id="imagePreview" class="img-preview img-fluid rounded" style="max-height: 200px; display: none;">
                                    {% endif %}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="d-flex justify-content-between mt-4">
                    <a href="{{ url_for('admin.services') }}" class="btn btn-secondary">Annuler</a>
                    {{ form.submit(class="btn btn-primary") }}
                </div>
            </form>
        </div>
    </div>
</div>
{% endblock %}
