<form method="post" name="sds_lign" id="{{ formId }}" action="{{ formAction }}">
    <h3 class="mb-base">
        &nbsp&nbsp
        <button type="submit" class="button principal button_right last" id="validateLignForm">
            {{ 'Enregistrer' | trad }}
        </button>
        <button type="button" class="button secondaire listeLigns">
            {{ 'Annuler' | trad }}
        </button>
    </h3>
    <div>
        <div class="tab-pane active in clearfix padding_form bg_form border_form">
            <div class="colonne2">
                <div class="row mb-0">
                    {{ inputAdvance('ZOOMADVANCE',{
                        titre: 'Code article' | trad,
                        name: 'SDS_LIGN[ART]',
                        value: ligneDemandeService.ART ?? '',
                        pahtZoom: path('Ajax.liste.stockLocal'),
                        entity: 'Sarticle',
                        titleZoom : strBuild('@1@ : liste des articles' | trad, [siteStockOrigine.DESIGN ~ ' (' ~ siteStockOrigine.SITE ~ ')']),
                        paramZoom: "{'filter_egal_STO#point#SITE' : $('#input_SDS_ENTE_SITE').val(), 'divStkloSi': 'text_SDS_LIGN[SECTIMP]', 'divStkloCdc': 'text_SDS_LIGN[CODE_COMP]', 'divStkloSca': 'text_SDS_LIGN[SSCPTEANA]', 'divQte': 'input_SDS_LIGN[QTE_DEM]', 'qteLabelText': '" ~ 'Quantité' | trad ~ "' }",
                        pathConvert: path('Ajax.outil.convertLibelle'),
                        convertLibelle: 'convertLibelleGlobal',
                        libelleBDD: 'DESIGN',
                        obligatoire: true,
                        pathValidator : path('Ajax.validator.zoomStockLocal'),
                        paramsValidator : "{'null' : false, 'site' : $('#input_SDS_ENTE_SITE').val() }",
                    }) }}
                </div>
            </div>
            <div class="colonne2 last">
                <div class="row mb-0">
                    {{ inputAdvance('TEXT', {
                        titre: strBuild('Quantité @1@' | trad, [uniteStockee ? '(' ~ uniteStockee ~ ')': '']),
                        name: 'SDS_LIGN[QTE_DEM]',
                        obligatoire: true,
                        value: ligneDemandeService.QTE_DEM ? ligneDemandeService.QTE_DEM | qte: '',
                        pathValidator: path('Ajax.validator.float'),
                        paramsValidator: "{'min' : '0', 'obligatoire': true, 'format': " ~ format_qte() ~ "}",
                        addClass: 'text-right'
                    }) }}
                </div>
            </div>
            <div class="clearfix">
                <hr>
                <div class="colonne2">
                    <div class="row">
                        {{ include('@Stock/Composant/ZoomAdvance/_section_imputation.twig', {
                            name: 'SDS_LIGN[SECTIMP]',
                            configAxeImp: configAxeImp,
                            siObligatoire: is_si_obligatoire(typeEntete),
                            codeSteInputId: 'input_SDS_ENTE_CODE_STE',
                            value: ligneDemandeService ? ligneDemandeService.SECTIMP: post.SDS_LIGN.INIT.SECTIMP,
                        }) }}
                    </div>
                    <div class="row mb-0">
                        {{ include('@Stock/Composant/ZoomAdvance/_code_complementaire.twig', {
                            name: 'SDS_LIGN[CODE_COMP]',
                            configAxeImp: configAxeImp,
                            cdcObligatoire: is_cdc_obligatoire(typeEntete),
                            codeSteInputId: 'input_SDS_ENTE_CODE_STE',
                            value: ligneDemandeService ? ligneDemandeService.CODE_COMP: post.SDS_LIGN.INIT.CODE_COMP
                        }) }}
                    </div>
                </div>
                <div class="colonne2 last">
                    {% if is_cg_active() %}
                    <div class="row">
                        {{ inputAdvance('TREEVIEW', {
                            'titre' : 'Code gestion' | trad,
                            'name' : 'SDS_LIGN[CODGES]',
                            'pathTreeview' : path('Ajax.Treeview.codeGestion'),
                            'paramsTreeview' : "{'CODE_SI': $('#'+CSS.escape('input_SDS_LIGN[SECTIMP]')).val() !== '' ? $('#'+CSS.escape('input_SDS_LIGN[SECTIMP]')).val(): $('#'+CSS.escape('input_SDS_LIGN[INIT][SECTIMP]')).val(),  'cg_valide': 'on', 'inputCG' : 'SDS_LIGN[CODGES]', 'CODE_STE': $('#input_SDS_ENTE_CODE_STE').val(), 'affichageLibelle' : 1 }",
                            'titleTreeview' : 'Code gestion' | trad,
                            'value': ligneDemandeService ? ligneDemandeService.CODGES: post.SDS_LIGN.INIT.CODGES,
                            'convertLibelle' : 'cgTrad',
                            'pathValidator' : path('Ajax.validator.codeGestion'),
                            'paramsValidator' : "{'nomChamp' : '"~ ('Code gestion' | trad |e('js')|raw)~"', 'null' : '" ~ not isCodgestObligatoire ~ "', 'CODE_SI': $('#'+CSS.escape('input_SDS_LIGN[SECTIMP]')).val() !== '' ? $('#'+CSS.escape('input_SDS_LIGN[SECTIMP]')).val(): $('#'+CSS.escape('input_SDS_LIGN[INIT][SECTIMP]')).val(), 'CODE_STE': $('#input_SDS_ENTE_CODE_STE').val(), 'valide_obligatoire': '1', 'AUTHACHAT' : '1' }",
                            '_pathConvert' : path('Ajax.outil.convertLibelle'),
                            'entity' : 'Codgest',
                            'codeBDD' : 'CODE_GES',
                            'libelleBDD' : 'CODE_AFF',
                            'siLink': 'input_SDS_LIGN[SECTIMP]',
                            'sectimpu' : ligneDemandeService.SECTIMP,
                            'codeAffCG' : ligneDemandeService.LIBGES,
                            'obligatoire' : isCodgestObligatoire,
                            'codeSte' : "$('#CODE_STE').val()",
                            'codeSteCG' : demandeService.CODE_STE,
                            'myFunction' : 'convertLibelleCodeGestion',
                            'myParams' : '["'~path('Ajax.outil.convertLibelleCodeGestion')~'", this, "CODE_GES", "CODE_AFF", "input_SDS_LIGN[CODGES]", "text_SDS_LIGN[CODGES]", "'~ligneDemandeService.CODGES~'", "1", "", "input_SDS_LIGN[SECTIMP]", 1, ]',
                            'option': not has_codgest(demandeService.CODE_STE, ligneDemandeService.SECTIMP ?? post.SDS_LIGN.INIT.SECTIMP) ? 'readonly': ''
                        }) }}
                    </div>
                    {% endif %}
                    <div class="row mb-0">
                        {{ include('@Stock/Composant/ZoomAdvance/_sous_compte_analyse.twig', {
                            name: 'SDS_LIGN[SSCPTEANA]',
                            configAxeImp: configAxeImp,
                            scaObligatoire: is_sca_obligatoire(typeEntete),
                            codeSteInputId: 'input_SDS_ENTE_CODE_STE',
                            value: ligneDemandeService ? ligneDemandeService.SSCPTEANA: post.SDS_LIGN.INIT.SSCPTEANA
                        }) }}
                    </div>
                </div>
            </div>
            <div class="clearfix" id="cs">
                {% if CSExist('SDS_LIGN') %}
                    <hr>
                    {{ inputCSAdvance({
                        'post': post,
                        'codTab': 'SDS_LIGN',
                        'colonneActif': true,
                        'ModeModif': modeModif,
                        'codeEnr': autoCompletCodeEnr(noDs, noLigne),
                    }) }}
                {% endif %}
            </div>
            <fieldset class="scheduler-border mb-0">
                <legend class="scheduler-border">{{ 'Planning des quantités' | trad }}</legend>
                <div id="collection">
                    {% if ligneDemandeService is defined %}
                        {% for key, lignePlanning in ligneDemandeService.SDS_PLANS %}
                            <div class="clearfix" id="item{{ key }}">
                                <div class="colonne2">
                                    <div class="row">
                                        {{ inputAdvance('DATE', {
                                            titre: 'Délai' | trad,
                                            name: 'SDS_PLANS[' ~ key ~ '][DTE_SOUH]',
                                            obligatoire: true,
                                            value: lignePlanning.DTE_SOUH | dateTrad,
                                            pathValidator: path('Ajax.validator.date'),
                                            paramsValidator: "{'dateMin' : '"~ dateJour ~"', 'null': false}",
                                        }) }}
                                    </div>
                                </div>
                                <div class="colonne2 last">
                                    <div class="row bs-flex">
                                        <span style="width: 95% !important;">
                                            {{ inputAdvance('TEXT', {
                                                titre: 'Quantité' | trad,
                                                name: 'SDS_PLANS[' ~ key ~ '][QTE]',
                                                obligatoire: true,
                                                value: lignePlanning.QTE | qte,
                                                pathValidator: path('Ajax.validator.float'),
                                                paramsValidator: "{'min' : '0', 'obligatoire': true, 'format': " ~ format_qte() ~ "}",
                                                addClass: 'text-right'
                                            }) }}
                                        </span>
                                        {% if key > 0 %}
                                            <div style="width: 5% !important;" class="text-right">
                                                <span type="button" class="Supprimer_v2 removeItem" data-index="{{ key }}"></span>
                                            </div>
                                        {% endif %}
                                    </div>
                                    <input type="hidden" name="{{ 'SDS_PLANS[' ~ key ~ '][NO_DS]' }}" value="{{ lignePlanning.NO_DS }}">
                                    <input type="hidden" name="{{ 'SDS_PLANS[' ~ key ~ '][NO_LIG_DS]' }}" value="{{ lignePlanning.NO_LIG_DS }}">
                                </div>
                            </div>
                        {% endfor %}
                    {% else %}
                        <div class="clearfix" id="item0">
                            <div class="colonne2">
                                <div class="row">
                                    {{ inputAdvance('DATE', {
                                        titre: 'Délai' | trad,
                                        name: 'SDS_PLANS[0][DTE_SOUH]',
                                        obligatoire: true,
                                        pathValidator: path('Ajax.validator.date'),
                                        paramsValidator: "{'dateMin' : '" ~ dateJour ~ "', 'null': false}",
                                    }) }}
                                </div>
                            </div>
                            <div class="colonne2 last">
                                <div class="row bs-flex">
                                    <span style="width: 95% !important;">
                                        {{ inputAdvance('TEXT', {
                                            titre: 'Quantité' | trad,
                                            name: 'SDS_PLANS[0][QTE]',
                                            obligatoire: true,
                                            pathValidator: path('Ajax.validator.float'),
                                            paramsValidator: "{'min' : '0', 'obligatoire': true, 'format': " ~ format_qte() ~ "}",
                                            addClass: 'text-right'
                                        }) }}
                                    </span>
                                </div>
                            </div>
                        </div>
                    {% endif %}
                </div>
                <div class="bs-col-1">
                    <button type="button" class="principal addItem">
                        + Ajouter un délai
                    </button>
                </div>
            </fieldset>
        </div>
    </div>
</form>
<div class="clearfix d-none" id="itemModel">
    <div class="colonne2">
        <div class="row">
            {{ inputAdvance('TEXT', {
                titre: 'Délai' | trad,
                name: 'SDS_PLANS[%i%][DTE_SOUH]',
                obligatoire: true,
                pathValidator: path('Ajax.validator.date'),
                paramsValidator: "{'dateMin' : '"~ dateJour ~"', 'null': false}",
                inputAttr: { 'data-date': true },
            }) }}
        </div>
    </div>
    <div class="colonne2 last">
        <div class="row bs-flex">
            <span style="width: 95% !important;">
                {{ inputAdvance('TEXT', {
                    titre: 'Quantité' | trad,
                    name: 'SDS_PLANS[%i%][QTE]',
                    obligatoire: true,
                    pathValidator: path('Ajax.validator.float'),
                    paramsValidator: "{'min' : '0', 'obligatoire': true, 'format': " ~ format_qte() ~ "}",
                    addClass: 'text-right'
                }) }}
            </span>
            <div style="width: 5% !important;" class="text-right">
                <span type="button" class="Supprimer_v2 removeItem" data-index="%i%"></span>
            </div>
        </div>
    </div>
</div>
<script type="application/javascript" src="{{ baseUrl('js/Modules/Stock/csInitializer.js') }}"></script>
<script type="application/javascript">
    if (!'{{ modeModif }}') {
        $('#' + CSS.escape('input_SDS_LIGN[ART]')).on('change', (event) => {
            const codeArt = $(event.target).val();
            const site =  $('#input_SDS_ENTE_SITE').val();

            initLigneDemandeServiceCs([['SARTICLE', codeArt], ['SSTOCKLO', site + codeArt]])
        });

        $('#' + CSS.escape('input_SDS_LIGN[SECTIMP]')).on('change', (event) => {
            initLigneDemandeServiceCs([['SECTIMPU', $(event.target).val()]])
        });

        $('#' + CSS.escape('input_SDS_LIGN[CODE_COMP]')).on('change', (event) => {
            initLigneDemandeServiceCs([['CODECOMP', $(event.target).val()]])
        });

        $('#' + CSS.escape('input_SDS_LIGN[SSCPTEANA]')).on('change', (event) => {
            initLigneDemandeServiceCs([['SSCPTANA', $(event.target).val()]])
        });

        function initLigneDemandeServiceCs (tablesSources) {
            initializeCs(
                '{{ path('Ajax.InitChampController.initializeCs') }}',
                '{{ formId }}',
                $('#'+CSS.escape('input_SDS_ENTE_CODE_STE')).val(),
                '{{ autoCompletCodeEnr(noDs, noLigne) }}',
                'SDS_LIGN',
                tablesSources
            );
        }
    }
</script>