{% extends "base.html" %}

{% block body %}
{% set group_name = request.groups.groups.get(person.name, person).name %}

<div class="header">
    <h1>{{ delivery.name }}</h1>
    <h4>Commande pour {{ group_name }}
        {% if order.phone_number %}
        (<a href="tel:{{ order.phone_number }}">{{ order.phone_number }}</a>)
        {% endif %}
    </h4>
{% include "includes/delivery_head.html" %}
</div>

<form method="post">
    {% for producer in delivery.producers.values() %}
    {% if producer.has_active_products(delivery) %}
        <h2>{{ producer.name }}</h2>
        <table class="order pure-table">
            <thead>
            <tr>
                <th class="product">Produit</th>
                <th class="price">Prix</th>
                {% if delivery.has_packing %}
                    <th class="packing">Conditionnement</th>
                {% endif %}
                <th class="amount">Commande</th>
                {% if delivery.status == delivery.ADJUSTMENT or force_adjustment %}
                    <th class="amount">Ajustement +/−</th>
                {% endif %}
            </tr>
            </thead>
            <tbody>
            {% for product in delivery.get_products_by(producer.id) %}
            {% if not product.rupture %}
                <tr>
                    <th class="product {% if product.rupture %}rupture{% endif %}">{{ product }} {% if product.rupture %}(RUPTURE !){% endif %}
                        {% if product.description %}
                        <br /><small title="{{ product.description }}">{{ product.description }}</small>
                        {% endif %}
                    </th>
                    <td>{{ product.price | round(2) }} €</td>
                    {% if delivery.has_packing %}
                        <td {% if delivery.status == delivery.ADJUSTMENT and delivery.product_missing(product) %} class="missing" title="Les commandes individuelles ne correspondent pas aux conditionnements"{% endif %}>{{ product.packing or "—" }}{% if delivery.status == delivery.ADJUSTMENT and delivery.product_missing(product) %} (manque {{ delivery.product_missing(product) }}){% endif %}</td>
                    {% endif %}
                    <td class="with-input"><input {% if not request.user.is_referent(delivery) and delivery.status != delivery.OPEN or product.rupture %}type="text" readonly{% else%}type="number"{% endif%} min=0 name="wanted:{{ product.ref }}" value="{{ order[product].wanted }}">{% if not (delivery.status == delivery.ADJUSTMENT or force_adjustments) %} x {{ product.unit }}{% endif %}</td>
                    {% if delivery.status == delivery.ADJUSTMENT or force_adjustments %}
                    <td class="with-input"><input type="number" name="adjustment:{{ product.ref }}" value="{{ order[product].adjustment }}" min="{{ order[product].wanted * -1 }}" {% if not (delivery.product_missing(product) or force_adjustment) %}readonly{% endif %}> x {{ product.unit }}</td>
                    {% endif %}
                </tr>
            {%- endif %}
            {% endfor %}
            </tbody>
        </table>
    {%- endif %}
    {% endfor %}
    <p>On y est presque ! Est-ce que tu peux entrer un numéro de téléphone au cas où on ait besoin de vous joindre ?</p>
    <input id="phone" type="text" onInput="prettifyPhoneNumber('phone')" name="phone_number" value="{{ order.phone_number }}" placeholder="Ton numéro de téléphone" required>
    <input type="hidden" name="email" value="{{ person.email }}">
    {% if delivery.status != delivery.CLOSED or request.user.is_staff %}
    <input type="submit" value="Enregistrer la commande" class="primary">
    {% endif %}
    {% if request.user.is_staff and delivery.status == delivery.CLOSED %}
    <a class="button danger" href="{{ url_for('place_order', id=delivery.id) }}?email={{ person.email }}&adjust">Ajuster</a>
    {% endif %}
</form>
</article>
{% endblock body %}
