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

{% block title %}{{ repas.get_type_repas_display }} - Carb Cycling{% endblock %}

{% block content %}
<div class="row">
    <div class="col-md-8 mx-auto">
        <div class="card">
            <div class="card-header bg-primary text-white">
                <h4 class="mb-0">
                    <i class="bi bi-egg-fried"></i> {{ repas.get_type_repas_display }}
                </h4>
                <small>
                    {{ repas.journee.date|date:"l d F Y" }}
                    {% if repas.heure %}| {{ repas.heure|time:"H:i" }}{% endif %}
                </small>
            </div>
            <div class="card-body">
                <div class="mb-4">
                    <span class="badge badge-{{ repas.journee.type_journee }} fs-6">
                        Journée {{ repas.journee.get_type_journee_display }}
                    </span>
                </div>
                
                <h5 class="mb-3">
                    <i class="bi bi-list-check"></i> Composition du repas
                </h5>
                
                {% if compositions %}
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead>
                            <tr>
                                <th>Aliment</th>
                                <th>Catégorie</th>
                                <th class="text-center">Portions</th>
                                <th>Détails</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for comp in compositions %}
                            <tr>
                                <td class="fw-bold">{{ comp.aliment.nom }}</td>
                                <td>
                                    <span class="badge bg-secondary">
                                        {{ comp.aliment.get_categorie_display }}
                                    </span>
                                </td>
                                <td class="text-center">
                                    <span class="badge bg-primary">{{ comp.nombre_portions }}</span>
                                </td>
                                <td class="small text-muted">
                                    {{ comp.aliment.portion_standard }}
                                    {% if comp.notes %}
                                    <br><em>{{ comp.notes }}</em>
                                    {% endif %}
                                </td>
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
                
                <div class="alert alert-info mt-3">
                    <strong><i class="bi bi-info-circle"></i> Résumé par catégorie :</strong>
                    <ul class="mb-0 mt-2">
                        {% regroup compositions by aliment.get_categorie_display as categories %}
                        {% for categorie in categories %}
                        <li>
                            <strong>{{ categorie.grouper }} :</strong>
                            {% for comp in categorie.list %}
                            {{ comp.nombre_portions }}{% if not forloop.last %} + {% endif %}
                            {% endfor %}
                            = {{ categorie.list|length }} apport{{ categorie.list|length|pluralize }}
                        </li>
                        {% endfor %}
                    </ul>
                </div>
                {% else %}
                <div class="alert alert-warning">
                    <i class="bi bi-exclamation-triangle"></i>
                    Aucun aliment n'a encore été ajouté à ce repas
                </div>
                {% endif %}
                
                {% if repas.notes %}
                <div class="alert alert-secondary mt-3">
                    <strong><i class="bi bi-sticky"></i> Notes :</strong><br>
                    {{ repas.notes }}
                </div>
                {% endif %}
                
                <div class="d-flex justify-content-between mt-4">
                    <a href="{% url 'detail_journee' repas.journee.pk %}" class="btn btn-outline-secondary">
                        <i class="bi bi-arrow-left"></i> Retour à la journée
                    </a>
                    <a href="{% url 'ajouter_aliment_repas' repas.pk %}" class="btn btn-success">
                        <i class="bi bi-plus-circle"></i> Ajouter un aliment
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
