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

{% block content %}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        {% if product %}
            <h1><i class="fas fa-edit me-2"></i> Modifier un produit</h1>
        {% else %}
            <h1><i class="fas fa-plus-circle me-2"></i> Ajouter un produit</h1>
        {% endif %}
        <a href="{{ url_for('admin.products') }}" 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="row mb-3">
                            <div class="col-md-6">
                                <div class="form-group">
                                    {{ 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>
                            <div class="col-md-6">
                                <div class="form-group">
                                    {{ form.category_id.label(class="form-label") }}
                                    {{ form.category_id(class="form-select" + (" is-invalid" if form.category_id.errors else "")) }}
                                    {% if form.category_id.errors %}
                                        <div class="invalid-feedback">
                                            {% for error in form.category_id.errors %}
                                                {{ error }}
                                            {% endfor %}
                                        </div>
                                    {% endif %}
                                </div>
                            </div>
                        </div>
                        
                        <div class="row mb-3">
                            <div class="col-md-6">
                                <div class="form-group">
                                    {{ form.price.label(class="form-label") }}
                                    {{ form.price(class="form-control" + (" is-invalid" if form.price.errors else "")) }}
                                    {% if form.price.errors %}
                                        <div class="invalid-feedback">
                                            {% for error in form.price.errors %}
                                                {{ error }}
                                            {% endfor %}
                                        </div>
                                    {% endif %}
                                    <div class="form-text">Laissez vide si le prix n'est pas applicable</div>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    {{ form.condition.label(class="form-label") }}
                                    {{ form.condition(class="form-select" + (" is-invalid" if form.condition.errors else "")) }}
                                    {% if form.condition.errors %}
                                        <div class="invalid-feedback">
                                            {% for error in form.condition.errors %}
                                                {{ error }}
                                            {% endfor %}
                                        </div>
                                    {% endif %}
                                </div>
                            </div>
                        </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=5) }}
                            {% 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_available(class="form-check-input") }}
                            {{ form.is_available.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 produit</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 product and product.image_url %}
                                        <img src="{{ url_for('static', filename=product.image_url) }}" alt="{{ product.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.products') }}" class="btn btn-secondary">Annuler</a>
                    {{ form.submit(class="btn btn-primary") }}
                </div>
            </form>
        </div>
    </div>
</div>
{% endblock %}
