{% extends 'layout.twig' %}

{% block body %}
<section class="contenu">
    <h1>{{ 'Commandes' | trad }}</h1>

    {{- ariane() -}}
    
    <h2>
        {{- strBuild(
            'Enregistrer un accusé réception sur @4@la commande N°@1@ du fournisseur @2@ (@3@)' | trad,
            [
                commande.NO_CDE,
                commande.Fourniss.RS,
                commande.Fourniss.CODE,
                commande.AVENANT | trim is not empty ? strBuild('l’avenant N°@1@ de ',[
                    commande.AVENANT | trim
                ]) : ''
            ]
        ) -}}
    </h2>
    <form method="post" class="py-base" 
        action="{{ path('Commande.AccRecep.create',{commandeId: commandeId}) }}"
        onsubmit="submitFormAccRecep(event);return false;"
        enctype="multipart/form-data"
    >
        <article class="bg_form padding_form border_form">
            {% for field in form.listField %}
            <div class="row">
                {{ inputAdvance(field.type, field) }}
            </div>
            {% endfor %}
        
            <div class="row text-right">
                {{ arianeAnnuler() }}
                <button type="submit" class="principal button">
                    {{- 'Enregistrer' | trad -}}
                </button>
            </div>
        </div>
    </form>
</section>
<script> 
function submitFormAccRecep(event) {
    let form = event.target;
	desactivePopup(); // TODO : necessary ?
	activeLoader();
    $.ajax({
    	url: form.action,
        type: 'POST',
        data: new FormData(form),
        contentType: false,
        processData: false,
        success: function (response, status, xhr) {
            if (response.suggestAvenantCreation) {
                desactiveLoader();
                ajaxPopup(
                    {{ path('Commande.AccRecep.popupCreateAvenant', {commandeId: commandeId}) | json }}, 
                    {{ (('Créer un avenant sur la commande N°' | trad) ~ commandeId ) | json }}, 
                    '', 
                    ''
                );
                return;
            }
            
            {{ arianePrevRouteJsFunc() }};;
        },
        error: function (xhr) {
            desactiveLoader();
            activePopup(
                {{ 'Erreur validation' | trad | json }},
                xhr.responseText,
                'modal_small'
            );
        }
    });
}
</script>
{% endblock %}