{% do setAriane(1, 'Paramétrage API données identitaires' | trad) %}
{% extends 'layout.twig' %}
     
{% block body %}

<section class="contenu">
    <h1>{{ 'Paramétrage' | trad }}</h1>

    {{ ariane() }}

    <form method="POST" action="{{ path('Parametrage.ApiExt.updateMapping') }}"
        class="box-form form-default",
        onsubmit="return ajaxSubmit(event)"
        data-onsuccess="mainFormSuccess"
    >
        {{ inputAdvance('SELECT', {
            titre: 'Source d’information' | trad,
            name: 'ExtEntSrc',
            listeValue: listPartnChoice,
            value: config.source,
            inputAttr: {
                onchange: 'updateFormMapping(this.value)'
            }
        }) }}
        <div id="wrapper-form-mapping">
            {% if form is not empty %}
                {% include '@ParamAPI/_form_mapping.twig' with {
                    form: form,
                    defaultCountry: defaultCountry,
                    subject: config.mapping,
                    configTaxe: configTaxe,
                    mapCountry: mapCountry,
                } only %}
            {% endif %}
        </div>
        <div class="text-right">
            <button type="submit" class="button principal m-0">
                {{- 'Enregistrer' | trad -}}
            </button>
        </div>
    </form>

</section>
<script>

let flashbag = localStorage.getItem('flashbag');
if (flashbag) {
    activePopupInfo('', flashbag, 'modal_small');
    localStorage.removeItem('flashbag');
}
window.action.mainFormSuccess = function() {
    localStorage.setItem(
        'flashbag',
        {{ 'Vos modifications ont été prises en compte.' | trad | json }}
    );
    window.action.pageRefresh();
}

//____________________

function updateFormMapping(codePartn) {
    let wrapper = document.getElementById('wrapper-form-mapping');
    if (codePartn.length == 0) {
        wrapper.innerHTML = '';
        return;
    }
    ajaxHtmlUpdate(
        wrapper,
        {{ path('Parametrage.ApiExt.formMapping') | json }},
        {partn: codePartn}
    );
}
</script>
{% endblock %}