{% extends "base.html" %}
{% block toplink %}<a href="{{ url_for('list_products', id=delivery.id) }}">↶ Retourner aux produits</a>{% endblock %}

{% block body %}
<div class="header">
{% if producer and producer.id %}
    <h1>Modifier les informations pour « {{ producer.name }} »</h1>
    <div class="pure-menu pure-menu-horizontal">
        <ul class="pure-menu-list">
            <li class="pure-menu-item">
                <a class="pure-menu-link" href="{{ url_for('create_product', delivery_id=delivery.id, producer_id=producer.id) }}">Ajouter un produit</a>
            </li>
            <li class="pure-menu-item">
                <a class="pure-menu-link" href="{{ url_for('delete_producer', delivery_id=delivery.id, producer_id=producer.id) }}">Supprimer</a>
            </li>
            <li class="pure-menu-list">
                <a class="pure-menu-link" href="{{ url_for('edit_shipping_price', delivery_id=delivery.id, producer_id=producer.id) }}">Gérer des frais de livraison</a>
            </li>
            {% if producer.needs_price_update(delivery) %}
            <li class="pure-menu-item">
                <a class="pure-menu-link" href="{{ url_for('validate_producer_prices', delivery_id=delivery.id, producer_id=producer.id) }}">Marquer les prix comme OK</a>
            </li>
            {% endif %}
        </ul>
    </div>
{% else %}
    <h1>Ajouter un⋅e producteur⋅rice</h1>
{% endif %}
</div>
{% if producer and producer.needs_price_update(delivery) %}
<p class="notification info">Les prix pour {{ producer.name }} ont besoin d'être validés.</p>
{% endif %}
<form method="post" class="pure-form pure-form-aligned">
    <fieldset>
    {% if producer.id %}
    <input type="hidden" name="id" value="{{ producer.id }}">
    {% else %}
    <div class="pure-control-group">
        <label for="name">Nom</label>
        <input id="name" type="text" name="name" value="{{ producer.name or '' }}" required>
    </div>
    {% endif %}
    <div class="pure-control-group">
        <label>Description</label>
        <input type="text" name="description" value="{{ producer.description or '' }}">
    </div>
    <div class="pure-control-group">
        <label>Email référent⋅e</label>
        <input type="email" name="referent" value="{{ producer.referent or '' }}" required>
    </div>
    <div class="pure-control-group">
        <label>Nom référent⋅e</label>
        <input type="text" name="referent_name" value="{{ producer.referent_name or '' }}" required>
    </div>
    <div class="pure-control-group">
        <label>Téléphone référent⋅e</label>
        <input onInput="prettifyPhoneNumber('referent_tel')" id="referent_tel" type="tel" name="referent_tel" value="{{ producer.referent_tel or '' }}" required>
    </div>
    <div class="pure-control-group">
        <label>Contact producteur⋅rice</label>
        <input type="text" name="contact" value="{{ producer.contact or '' }}">
    </div>
    <div class="pure-controls">
        <input type="submit" name="submit" value="Valider" class="primary">
    </div>
    </fieldset>
</form>

{% if delivery.shipping.get(producer.id) %}
<h3>Frais de livraison</h3>
Des frais de livraison de {{ delivery.shipping[producer.id] }}€ ont été enregistrés pour ce producteur.
<a class="button" href="{{ url_for('edit_shipping_price', delivery_id=delivery.id, producer_id=producer.id) }}">Modifier</a>
{% endif %}

{% if products %}
<h3>Produits
    <a class="button" href="{{ url_for('create_product', delivery_id=delivery.id, producer_id=producer.id) }}">Ajouter un produit</a>
    {% if producer.needs_price_update(delivery) %}
        <a class="button" href="{{ url_for('validate_producer_prices', delivery_id=delivery.id, producer_id=producer.id) }}">Marquer les prix comme OK</a>
    {% endif %}
</h3>
<table id="products" class="pure-table">
    <thead>
        <tr>
            <th>Produit</th>
            <th>Prix</th>
            <th>Unité</th>
            <th>Description</th>
            <th>Packaging</th>
            <th>Rupture ?</th>
            <th></th>
            <th></th>
        </tr>
    </thead>
{% for product in products %}
<tr>
    <td><a href="{{ url_for('edit_product', delivery_id=delivery.id, producer_id=producer.id, product_ref=product.ref) }}">{{ product.name }}</a></td>
    <td>{{ product.price }}€</td>
    <td>{{ product.unit }}</td>
    <td>{{ product.description }}</td>
    <td>{% if product.packing %}{{ product.packing }}{% endif %}</td>
    <td>{% if product.rupture %}RUPTURE !!{% endif %}</td>
    <td><a href="{{ url_for('edit_product', delivery_id=delivery.id, producer_id=producer.id, product_ref=product.ref) }}">modifier</a></td>
    <td><a href="{{ url_for('delete_product', delivery_id=delivery.id, producer_id=producer.id, product_ref=product.ref) }}">supprimer</a></td>
</tr>
{% endfor %}
</table>
{% endif %}

<hr>
{% endblock body %}
