{% set menuActive = menuActive ?? 'Administration' %}
{% set menuLeft = menuLeft ?? 'Administration.GestionUtilisateur.listCollab' %}
{% do setAriane(
    1,
    isCreation ?
        'Approbation de l’inscription' | trad :
        'Approbation de la modification' | trad,
    {routeParam: {demId: subject.CODE}}
) %}

{% extends 'layout.twig' %}
{% block body %}
<section class="contenu">

    <h1>{{ 'Administration' | trad }}</h1>
    
    {% set isCreation = (subject.CREATMOD == 'C') %}
    
    {{ ariane() }}
    
    {% embed '@Administration/GestionUtilisateur/_formEditUser.twig' with {
        form_attr: {
            onsubmit: 'return submitApprobDemStep1(event)'
        },
    } %}
        {% block action %}
            <button type="submit" class="button principal">
                {{- 'Approuver' | trad -}}
            </button>
            <button type="button" class="button secondaire m-0"
                onclick="showPopupRefus()"
            >
                {{- 'Refuser' | trad -}}
            </button>
        {% endblock %}
    {% endembed %}

</section>
{#______________________________________________________________________________
Script Approbation #}
<script>
var hasStep2 = {{ (formStep2 is not empty) | json }};
function submitApprobDemStep1(event) {

    let formMainData = new FormData(event.target);
    if (! hasStep2) {
        return ajaxApprob(formMainData);
    }

    showPopupApprob();
    
    document
        .getElementById('form-step2')
        .onsubmit = function(event) {
            
            let formStep2Data = new FormData(event.target);
            
            // Merge formMainData + formStep2Data
            for (var pair of formStep2Data.entries()) {
                formMainData.append(pair[0], pair[1]);
            }

            return ajaxApprob(formMainData);
        }
    return false;
}

function ajaxApprob(formData) {
    activeLoader();
    $.ajax({
        url: {{ path(
            'Administration.GestionUtilisateur.approveDem',
            {demId: subject.CODE}
        ) | json }},
        type: 'POST',
        data: formData,
        contentType: false, processData: false,
        success: function (response, status, xhr) {
            window.action.arianePop();
        },
        error: function (xhr) {
            desactiveLoader();
            activePopupError(
                {{ 'Erreur validation' | trad | json }},
                xhr.responseText,
                'modal_small'
            );
        }
    });
    return false;
}

function showPopupApprob() {
    
	let tpl = document.getElementById('tpl-form-step2').textContent;
	let tmp = document.createElement('div');

    // Decode html entities
    tmp.innerHTML = tpl; 
    tpl = tmp.textContent;

    activePopup({{ 'Approuver la demande' | trad | json }}, tpl, 'modal_small');
}
</script>
<script id="tpl-form-step2" type="template/html">
{% apply escape %}
    <form id="form-step2" method="POST" class="form-default">
        <div class="">
            {{ formInput(formStep2.password, subject, 'Votre mot de passe' | trad) }}
            {{ formInput(formStep2.login, subject, 'Login de l’utilisateur' | trad) }}
        </div>
        <div class="modal-footer">
            <button type="submit" class="button principal button_right">
                {{- 'Approuver' | trad -}}
            </button>
            <button type="button" class="button secondaire button_right" 
                onclick="desactivePopup();"
            >
                {{- 'Annuler' | trad -}}
            </button>
        </div>
    </form>
{% endapply %}
</script>
{#______________________________________________________________________________
Script Refus #}
<script>
function showPopupRefus() {
    
	let tpl = document.getElementById('tpl-form-refus').textContent;
	let tmp = document.createElement('div');

    // Decode html entities
    tmp.innerHTML = tpl; 
    tpl = tmp.textContent;

    activePopup({{ 'Refuser la demande' | trad | json }}, tpl);
}
</script>
<script id="tpl-form-refus" type="template/html">
{% apply escape %}
    <form id="form-refus" method="POST" class="form-default"
        action="{{ path(
            'Administration.GestionUtilisateur.refusDem',
            {demId: subject.CODE}
        ) | e('html_attr') }}"
        onsubmit="return ajaxSubmit(event)"
        data-onsuccess="arianePop"
    >
        <div class="">
            {{ formInput(formStep2.password, subject, 'Votre mot de passe' | trad) }}
            {{ inputAdvance('TEXTAREA',{
                titre: 'Commentaires associés au refus' | trad,
                name: 'comment',
                pathValidator: path('Ajax.validator.isStringAvecEspace'),
                paramsValidator: {sizeMin: 0, sizeMax: 3000},
            }) }}
        </div>
        <div class="modal-footer text-right">
            <button type="submit" class="button principal">
                {{- 'Refuser' | trad -}}
            </button>
            <button type="button" class="button secondaire m-0" 
                onclick="desactivePopup();"
            >
                {{- 'Annuler' | trad -}}
            </button>
        </div>
    </form>
{% endapply %}
</script>
{% endblock %}


