<form id="form-edit-user" method="POST" class="form-default tab-content {{ form_attr.class ? ''}}"
    {{ attr(
        {onsubmit: 'return ajaxSubmit(event)'}
        | merge(form_attr)
    ) }}
>
    <header class="bs-row">
    <ul class="bs-col-auto align-self-end nav nav-tabs sous_onglet" role="tablist">
        <li class="active" role="presentation">
            <a data-toggle="tab" href="#tab-detail">
                {{- 'Coordonnées' | trad -}}
            </a>
        <li role="presentation">
            <a data-toggle="tab" href="#tab-abonn">
                {{- 'Abonnements' | trad -}}
            </a>
    </ul>
    <div class="bs-col text-right">
        {% block action %}
        <button type="submit" class="button principal">
            {{- 'Enregistrer' | trad -}}
        </button>
        {% endblock %}
    </div>
    </header>
{#_____________________________________________________________________________
Tab Detail #}

    {% macro approbRequired() %}
        <div class="text-muted text-right white-tooltip align-self-center">
            <i class="icosq-approb mx-base"></i>
            <span class="infos_tooltip top-left">
                {{ 'Modification soumise à approbation' | trad }}
            </span>
        </div>
    {% endmacro %}

<script>
$(function() {
    let listFormApprobReq = {{
        form 
        | filter(input => input.approbRequired) 
        | map(input => input.name)
        | json }};
    for (name in listFormApprobReq) {
        document
            .querySelector('[name="'+name+'"]')
            .closest('.input-wrapper')
            .append(parseHtml({{ _self.approbRequired() | json }}))
    }
})
</script>
    <article id="tab-detail" class="bs-row gutter-x-base tab-pane fade in active">
    <div class="bs-col-full">
        <fieldset class="scheduler-border">
            <legend class="scheduler-border">{{ 'Coordonnées' | trad}}</legend>

            <div class="bs-row gutter-x-base">
            <div class="bs-col-half">
                {{ formInput(form.CODE_STE, subject, 'Société' | trad) }}
                {{ formInput(form.CODE_UO, subject, 'Unité Organisationnelle' | trad) }}
            
                {{ formInput(form.SI, subject, 'Section d’imputation' | trad) }}
                {{ formInput(form.CDC, subject, 'Code complémentaire' | trad) }}
                {{ formInput(form.SCA, subject, 'Sous-compte d’analyse' | trad) }}
                
                {{ formInput(form.CTR_ADR, subject, 'Centre émetteur' | trad) }}
                {{ formInput(form.CTRLIV, subject, 'Centre de livraison' | trad) }}
            </div>
            <div class="bs-col-half">
                {{ formInput(form.NOM, subject, 'Nom et prénom' | trad) }}
                {{ formInput(form.FONCTION, subject, 'Fonction' | trad) }}
                {{ formInput(form.SUPHIER, subject, 'Supérieur Hiérarchique' | trad) }}
                
                {{ formInput(form.TEL, subject, 'Téléphone fixe' | trad) }}
                {{ formInput(form.GSM, subject, 'Téléphone mobile' | trad) }}
                {{ formInput(form.FAX, subject, 'Fax' | trad) }}
                {{ formInput(form.EMAIL, subject, 'E-mail' | trad) }}
            </div>
            </div>
        </fieldset>
    </div>
    {# Actual condition : $paravrac->hasSmtpChoice() #}
    {% if form.USRMSG is not empty %}
    <div class="bs-col-half">
        <fieldset class="scheduler-border">
            <legend class="scheduler-border">{{ 'Messagerie' | trad}}</legend>

            {{ formInput(form.USRMSG, subject, 'Code utilisateur' | trad) }}
            {{ formInput(form.MDPMSG, subject, 'Mot de passe' | trad) }}
            <div class="text-right">
                <button type="button" class="button secondaire" onclick="testSmtp()">
                    {{- 'Test' | trad -}}
                </button>
            </div>
        </fieldset>
    </div>
    {% endif %}
    <div class="bs-col-half">
        <fieldset class="scheduler-border">
            <legend class="scheduler-border">{{ 'Domaines fonctionnels' | trad}}</legend>
            {{ formInput(form.list_domfonc, subject, 'Domaines fonctionnels de l’utilisateur' | trad) }}
        </fieldset>
    </div>
    {% if form.isSingleSignOn is not empty or form.NOMDOM is not empty %}
    <div class="bs-col-half" {% if modeModif %}style="display:none;"{% endif %}>
        <fieldset class="scheduler-border">
            <legend class="scheduler-border">{{ 'Mode de connexion' | trad}}</legend>
            {{ formInput(
                form.isSingleSignOn,
                subject,
                'Single Sign-On' | trad,
                {inputAttr: {onchange: "updateModeConnexion()"}}
            ) }}
            {{ formInput(
                form.NOMDOM,
                subject,
                'Domaines Active Directory' | trad,
                {inputAttr: {onchange: "updateModeConnexion()"}}
            ) }}
        </fieldset>
    </div>
    {% endif %}
    <div class="bs-col-half">
        <fieldset class="scheduler-border">
            <legend class="scheduler-border">
                {{- 'Copie cachée lors de l’envoi des emails suivants' | trad -}}
            </legend>
        
            {{ formInput(form.BCC_MBDC, subject, 'Bons de commande envoyés aux fournisseurs' | trad) }}
        </fieldset>
    </div>
    
    </article>
{#_____________________________________________________________________________
Tab Abonnement #}
    <article id="tab-abonn" class="tab-pane fade  bs-row gutter-x-base">
    <div class="bs-col-half">
        {# Actual condition : Roles::isAdmin() || $paravrac->hasEprocDemande() #}
        {% if form.SeuilCde is not empty %}
        <fieldset class="scheduler-border">
            <legend class="scheduler-border">
                {{- 'Commande eproc' | trad -}}
            </legend>
            {{ formInput(
                form.AUTOCC,
                subject,
                'Actif' | trad,
                {inputAttr: {onchange: "eprocUpdate()"}}
            ) }}
            {{ formInput(form.SeuilCde, subject, 'Seuil commande Eproc' | trad) }}
            <fieldset class="scheduler-border">
                <legend class="scheduler-border">
                    {{- 'Autorisations' | trad -}}
                </legend>
                {{ formInput(form.AuthLigneManuelle, subject, 'Ligne manuelle panier' | trad) }}
                {{ formInput(form.AuthLigneGenerique, subject, 'Ligne manuelle générique' | trad) }}
                {{ formInput(form.AuthLigneSFamille, subject, 'Ligne sous-famille' | trad) }}
                {{ formInput(form.AuthLigneLibre, subject, 'Ligne libre' | trad) }}
            </fieldset>
        </fieldset>
        {% endif %}

        <fieldset class="scheduler-border">
            <legend class="scheduler-border">
                {{- 'Module de Signatures' | trad -}}
            </legend>
            
            {{ formInput(form.fichier_signature, subject, 'Fichier image de votre signature' | trad) }}
            {# Case : dem edit OR user edit (all scenario except creation) #}
            
            <div class="mt-base">
                {% if subject.CREATMOD != 'C' %}
                <a href="{{ path(
                    'Administration.GestionUtilisateur.userImgSignat',
                    {userId: subject.CREATMOD == 'M' ? subject.USR_MOD : subject.CODE}
                ) }}" target="_blank">
                    {{- 'Aperçu de votre image de signature actuelle' | trad -}}
                </a>
                {% endif %}
            </div>
            
            {{ formInput(form.DEL_PREF, subject, 'Délégataire préféré' | trad) }}
        
            <fieldset class="scheduler-border">
                <legend class="scheduler-border">
                    {{- 'Est signataire de' | trad -}}
                </legend>
                {{ formInput(form.SIG_DA, subject, 'Demande d’achat' | trad) }}
                {{ formInput(form.MAILSIDA, subject, 'Recevoir un e-mail pour DA à signer' | trad) }}
                {{ formInput(form.SIG_CDE, subject, 'Commande' | trad) }}
                {{ formInput(form.MAILSICA, subject, 'Recevoir un e-mail pour CDE à signer' | trad) }}
                {% block Specif_ItemSignat %}{% endblock %}
                {{ formInput(form.SIG_BAP, subject, 'Bon à payer' | trad) }}
                {{ formInput(form.MAILSIBA, subject, 'Recevoir un e-mail pour BAP à signer' | trad) }}
            </fieldset>
        </fieldset>
    </div>
    <div class="bs-col-half">
        <fieldset class="scheduler-border">
            <legend class="scheduler-border">
                {{- 'Module d’approvisionnement' | trad -}}
            </legend>
            {{ formInput(
                form.DEMAND,
                subject,
                'Actif' | trad,
                {inputAttr: {onchange: "approvUpdate()"}}
            ) }}
            <fieldset class="scheduler-border">
                <legend class="scheduler-border">
                    {{ 'Réception d’un e-mail pour' | trad}}
                </legend>
                {{ formInput(form.DEMVALBR, subject, 'Saisie/validation BR sur vos DA' | trad) }}
                {{ formInput(form.DEMREJDA, subject, 'Signature de vos DA' | trad) }}
                {{ formInput(form.EMADATRAI, subject, 'DA traitée' | trad) }}
                {{ formInput(form.EMACOMPAO, subject, 'Comparatif AO' | trad) }}
                {{ formInput(form.DEMREJCA, subject, 'Signature de vos CDE' | trad) }}
                {{ formInput(form.EMA_DEM_MODIF_PL_CDE, subject, 'Modif. du planning de ses commandes' | trad) }}
                {% block Specif_ItemEmail %}{% endblock %}
            </fieldset>
        </fieldset>
        <fieldset class="scheduler-border">
            <legend class="scheduler-border">{{ 'Autre' | trad }}</legend>
            {{ formInput(form.OXTRBRDAU, subject, 'Module de saisie de réception' | trad) }}
            {{ formInput(
                form.MAGASIN,
                subject,
                'Module de suivi des bons de sortie' | trad,
                { inputAttr: {onchange: "suiviUpdate()"} }
            ) }}
            {{ formInput(form.site_stk, subject, 'Site de stock' | trad) }}
        </fieldset>
    </div>
    </article>
    


</form>
<script>
function suiviUpdate() {
    chainDisable(document.querySelector('[name="MAGASIN"]'),['site_stk'])
}
function approvUpdate() {
    chainDisable(document.querySelector('[name="DEMAND"]'), [
        'DEMVALBR', 'DEMREJDA', 'EMADATRAI', 'EMACOMPAO',
        'DEMREJCA', 'EMA_DEM_MODIF_PL_CDE'
    ]);
}
function eprocUpdate() {
    chainDisable(document.querySelector('[name="AUTOCC"]'), [
        'SeuilCde', 'AuthLigneManuelle', 'AuthLigneGenerique',
        'AuthLigneSFamille', 'AuthLigneLibre'
    ]);
}

function chainDisable(input, listInput) {
    if (!input || input.disabled) return; // Case : all eproc is disabled for profile edit
    for (name of listInput) {
        let child = document.querySelector('[name="'+name+'"]');
        if (child != null) child.disabled = ! input.checked;

        let zoomInput = document.querySelector('[name="text_'+name+'"]');
        if (zoomInput) zoomInput.disabled = ! input.checked;
    }
}

function updateModeConnexion() {
    // isSingleSignOn and NOMDOM cannot be both selected

    var sso = document.querySelector('[name="isSingleSignOn"]');
    var ldap = document.querySelector('[name="NOMDOM"]');

    if (sso != null) {
        sso.disabled = (ldap != null && ldap.value != '');
    }
    if (ldap != null) {
        ldap.disabled = (sso != null && sso.checked);
    }
}

//___________________________

updateModeConnexion();
approvUpdate();
eprocUpdate();
suiviUpdate();

//___________________________

// TODO : disable button if input not filled
function testSmtp() {
    let form = document.getElementById('form-edit-user');

    $.ajax({
        url: {{ path('Ajax.TestService.testSMTP') | json }},
        type: 'POST',
        data: {
            login: form.USRMSG.value, 
            password: form.MDPMSG.value, 
            email: form.EMAIL.value,
        },
        success: function (data) {
            desactiveLoader();
            activePopupInfo(
                {{ 'Informations' | trad | json }},
                data,
                'modal_small'
            );
        },
        error: function(xhr) {
            desactiveLoader();
            activePopupError(
                {{ 'Erreur validation' | trad | json }},
                xhr.responseText,
                'modal_small'
            );
        }
    });
}
</script>

