{% extends 'layout.twig' %}

{% block body %}
    <section class="contenu">
        <h1>{{ 'Demandes d’achats' | trad }}</h1>

        {{ ariane() }}

        <div class="content">

        <form 
            method="POST" class="form-default  my-base"
            action="{{ path('DemandeAchat.DuplicationDemandeAchat.duplicate', {idDA: da.NO_DA}) }}" 
            onsubmit="return ajaxSubmit(event);"
            data-onsuccess="duplicateSuccessForm"
        >
            <header class="bs-row flex-y-center">
                <h2>{{ strBuild(
                    'Duplication de la demande n°@1@' | trad,
                    [da.NO_DA]
                ) }}</h2>
                <div class="bs-col text-right">
                    {{ arianeAnnuler() }}
                    <button type="submit" class="button principal">
                        {{- 'Dupliquer' | trad -}}
                    </button>
                </div>
            </header>

            <article class="bg_form padding_form border_form my-base">
                <h3>{{ 'Demande originale' | trad}}</h3>
                <div class="bs-row gutter-x-base">
                    <dl class="dl-info bs-col-half">
                        <dt>{{ 'N° de demande' | trad}}</dt>
                        <dd>{{ da.NO_DA }}</dd>
                        <dt>{{ 'État de la demande' | trad}}</dt>
                        <dd>{{ getDemAchatEtatLabel(da) }}</dd>
                        <dt>{{ 'Date de création' | trad}}</dt>
                        <dd>{{ da.DTE_EMISS | dateTrad }}</dd>
                        <dt>{{ 'Type de document' | trad}}</dt>
                        <dd>{{ da.TypeDoc.LIBELLE }} ({{ da.TypeDoc.TYPE }})</dd>
                    </dl>
                    <dl class="dl-info bs-col-half">
                        <dt>{{ 'Société' | trad}}</dt>
                        <dd>{{ da.Societe.RAIS_SOC }} ({{ da.Societe.CODE_STE }})</dd>
                        <dt>{{ 'Fournisseur' | trad}}</dt>
                        <dd>
                            {%- if da.Fournisseur.CODE -%}
                                {{ da.Fournisseur.RS }} ({{ da.Fournisseur.CODE }})
                            {%- endif -%}
                        </dd>
                        <dt>{{ 'Centre Emetteur' | trad}}</dt>
                        <dd>
                            {%- if da.CentreEmet is not empty -%}
                                {{ da.CentreEmet.RAIS_SOC }} ({{ da.CentreEmet.CODE_CTR }})
                            {%- endif -%}
                        </dd>
                        <dt>{{ 'Centre Livraison' | trad}}</dt>
                        <dd>
                            {%- if da.CentreLivr is not empty -%}
                                {{ da.CentreLivr.RAIS_SOC }} ({{ da.CentreLivr.CODE_CTR }})
                            {%- endif -%}
                        </dd>
                    </dl>
                </div>
            </article>
            
            <article class="bg_form padding_form border_form my-base">
                <h3>{{ 'Nouvelle demande' | trad}}</h3>
                <div class="bs-row gutter-x-base">
                    <div class="bs-col-half">
                        {{ inputAdvance('ZOOMADVANCE', {
                            titre: 'Société' | trad,
                            name: 'CODE_STE',
                            pahtZoom: path('Ajax.liste.societe'),
                            paramZoom: {with_DA_MODENUM: true} | json,
                            entity: 'Societe',
                            titleZoom: 'Liste des sociétés' | trad,

                            pathValidator: path('Ajax.validator.zoom'),
                            paramsValidator: {
                                entity: 'Societe',
                                nomChamp: 'Société' | trad,
                                null: false
                            } | json,
                            obligatoire: true,

                            valueTexteZoom: da.Societe.RAIS_SOC ~ ' ('~ da.Societe.CODE_STE ~')',
                            value: da.Societe.CODE_STE,

                            pathConvert: path('Ajax.outil.convertLibelle'),
                        }) }}


                        {{ inputAdvance('TEXTE', {
                            titre: 'Numéro' | trad,
                            name: 'NO_DA',
                            pathValidator: path('Ajax.validator.integer'),
                            paramsValidator: {sizeMin: 0, sizeMax: 9} | json,
                            value: newDa.NO_DA,
                            option: config.MODE_NUM == 'A' ? 'disabled' : '',
                            obligatoire: (config.MODE_NUM == 'M'),
                        }) }}

                        {{ inputAdvance('SELECT', {
                            titre : 'Chiffrage/Exécution' | trad,
                            name : 'CHIF_EXEC',
                            listeValue : {
                                'E': 'Exécution' | trad,
                                'C': 'Chiffrage' | trad,
                            },
                            value : da.CHIF_EXEC,
                        }) }}

                        {{ inputAdvance('ZOOMADVANCE', {
                            titre: 'Type de document' | trad,
                            name: 'TYP_DOC',
                            pahtZoom: path('Ajax.liste.typeDoc'),
                            paramZoom: {
                                valide: 1,
                            } | json,
                            entity: 'TypeDoc',
                            titleZoom: 'Liste des types de documents'  | trad,
                            pathValidator: path('Ajax.validator.zoom'),
                            paramsValidator: {
                                entity: 'TypeDoc',
                                nomChamp: 'Type de document' | trad,
                                null: true,
                            } | json,
                            valueTexteZoom: da.TypeDoc.LIBELLE ~ ' (' ~ da.TypeDoc.TYPE ~')',
                            value: da.TypeDoc.TYPE,
                            pathConvert: path('Ajax.outil.convertLibelle'),
                        }) }}
                    </div>
                    <div class="bs-col-half">
                        {{ inputAdvance('ZOOMADVANCE', {
                            titre: 'Demandeur' | trad,
                            name: 'NO_DEM',
                            obligatoire: true,
                            pahtZoom: path('Ajax.liste.demandeur'),
                            paramZoom: 'getDemandeurParamZoom()',
                            entity: 'HASTUTIL',
                            titleZoom: 'Liste des demandeurs' | trad,

                            pathValidator: path('Ajax.validator.demandeur'),
                            paramsValidator: 'getDemandeurValidatorParam()',

                            valueTexteZoom: da.Demandeur.NOM ~ ' (' ~ da.Demandeur.CODE ~')',
                            value: da.Demandeur.CODE,
                            pathConvert: path('Ajax.outil.convertLibelle'),
                        }) }}

                        {{ inputAdvance('ZOOMADVANCE', {
                            titre: 'Centre émetteur' | trad,
                            name: 'CTR_EMETT',
                            pahtZoom: path('Ajax.liste.centreEmetteur'),
                            paramZoom: 'getCentreParamZoom()',
                            entity: 'Centre',
                            titleZoom: 'Liste des centres' | trad,
                            pathValidator: path('Ajax.validator.zoom'),
                            paramsValidator: 'getCtrEmetValidatorParam()',
                            valueTexteZoom: da.CentreEmet.RAIS_SOC ~ ' (' ~ da.CentreEmet.CODE_CTR ~ ')',
                            value: da.CentreEmet.CODE_CTR,
                            obligatoire: true,
                            pathConvert: path('Ajax.outil.convertLibelle'),
                        }) }}

                        {{ inputAdvance('ZOOMADVANCE', {
                            titre: 'Centre de livraison' | trad,
                            name: 'CTR_LIV',
                            pahtZoom: path('Ajax.liste.centreLivraison'),
                            paramZoom: 'getCentreParamZoom()',
                            entity: 'Centre',
                            titleZoom: 'Liste des centres' | trad,
                            pathValidator: path('Ajax.validator.zoom'),
                            paramsValidator: 'getCtrLivValidatorParam()',
                            valueTexteZoom: da.CentreLivr.RAIS_SOC ~ ' (' ~ da.CentreLivr.CODE_CTR ~ ')',
                            value: da.CentreLivr.CODE_CTR,
                            obligatoire: true,
                            pathConvert: path('Ajax.outil.convertLibelle'),
                        }) }}

                        {{ specifique('HtmlExtension', 'inputDemandeAchatEntete') }}

                    </div>
                </div>
            </article>
        </form>
    </section>
    <script>

    window.action.duplicateSuccessForm = function(responseData, status, xhr) {
        window.location.href = responseData.linkDetail;
    };

    function getCurrentSocValue() {
        return document.querySelector('[name="CODE_STE"]').value;
    }

    function getCentreParamZoom() {
        return {code_ste: getCurrentSocValue()};
    }
    function getDemandeurParamZoom() {
        return {filter_egal_CODE_STE: getCurrentSocValue()};
    }
    function getDemandeurValidatorParam() {
        return {
            nomChamp: {{ 'Demandeur' | trad | json }}, 
            null: false,
            post: {
                CODE_STE: getCurrentSocValue(),
                VALIDE: 1
            },
        };
    }
    function getCtrEmetValidatorParam() {
        return {
            entity: 'Centre',
            nomChamp: {{ 'Centre émetteur' | trad | json }}, 
            null: false,
            post: {
                filter_EMISSION: 'T',
                filter_egal_CODE_STE: getCurrentSocValue(),
            }
        };
    }
    function getCtrLivValidatorParam() {
        return {
            entity: 'Centre',
            nomChamp: {{ 'Centre de livraison' | trad | json }}, 
            null: false,
            post: {
                filter_LIVRAISON: 'T',
                filter_egal_CODE_STE: getCurrentSocValue(),
            }
        };
    }

    // On Societe change -> revalidate input with a dependency on Societe
    document
        .querySelector('[name="CODE_STE"]')
        .addEventListener('change', function(event) {
            let inputSoc = event.target;

            //___________________
            // Update CODE_CDE using da_modenum
            let inputDaId = document.querySelector('[name="NO_DA"]');
            let inputDaIdRequired = 
                inputDaId
                    .closest('.input-wrapper')
                    .querySelector('[data-labelsuffix]');
            switch (inputSoc.getAttribute('data-da_modenum')) {
                case 'A':
                    inputDaId.disabled = true;
                    inputDaId.value = '';
                    inputDaIdRequired.classList.remove('obligatoire');
                    inputDaIdRequired.innerHTML = '';
                    break;
                case 'I':
                    inputDaId.disabled = false;
                    inputDaIdRequired.classList.remove('obligatoire');
                    inputDaIdRequired.innerHTML = '';
                    break;
                case 'M':
                default:
                    inputDaId.disabled = false;
                    inputDaIdRequired.classList.add('obligatoire');
                    inputDaIdRequired.innerHTML = '*';
            }

            //___________________
            // Re-validate
            for( selector of [
                '[name="CTR_EMETT"]',
                '[name="CTR_LIV"]',
                '[name="NO_DEM"]',
            ]) {
                document
                    .querySelector(selector)
                    .dispatchEvent(new Event('change'));
            }
        });
    </script>
</div>
{% endblock %}