{% extends 'layout.twig' %}

{% block body %}
    <div class="contenu clearfix">
        <h1>{{ 'Bons d\'Entrée' | trad }}</h1>

        {{ ariane() }}
        <div class="tab-content" id="tab-content">
            <form method="post" name="sbe_ente" id="bonEntreFormModifier" action="{{ path('Stock.BonEntree.modifier', { noBe: bonEntree.NO_BE }) }}">
                <h3 class="mb-base">
                    {{ 'Modification du bon d\'entrée N° ' ~ bonEntree.NO_BE  }}
                    <button id="enregistrerValider" type="button" class="button principal button_right last">{{ 'Enregistrer et valider' | trad }}</button>
                    <button id="enregistrer" type="button" class="button secondaire">{{ 'Enregistrer' | trad }}</button>
                </h3>
                <details open class="block_edition clearfix">
                    <summary class="titre_block">{{ strBuild('Entête du bon d\'entrée N°@1@' | trad, [bonEntree.NO_BE]) }}</summary>
                    <div class="clearfix">
                        <div class="colonne3">
                            <div class="informations">
                                <p>
                                    <span class="champs_label">{{ 'N° de bon d\'entrée' | trad }}</span>
                                    <span class="resultat">{{ bonEntree.NO_BE }}</span>
                                </p>
                                <p>
                                    <span class="champs_label">{{ 'Société' | trad }}</span>
                                    <span class="resultat">{{ societe.RAIS_SOC ~ ' (' ~ bonEntree.CODE_STE ~ ')' }}</span>
                                </p>
                                <p>
                                    <span class="champs_label">{{ 'Type' | trad }}</span>
                                    <span class="resultat">{{ listeFiltreType[bonEntree.TYPE_BE] }}</span>
                                </p>
                            </div>
                        </div>
                        <div class="colonne3">
                            <div class="informations">
                                <p>
                                    <span class="champs_label">{{ 'Créé le' | trad }}</span>
                                    <span class="resultat">{{ bonEntree.DTE_CREAT | dateTrad }}</span>
                                </p>
                                <p>
                                    <span class="champs_label">{{ 'Validé le' | trad }}</span>
                                    <span class="resultat">{{ bonEntree.DTE_VALID | dateTrad | visualise }}</span>
                                </p>
                                <p>
                                    <span class="champs_label">{{ 'Proposition de réappro N°' | trad }}</span>
                                    <span class="resultat">{{ bonEntree.BE_NOPR | visualise }}</span>
                                </p>
                            </div>
                        </div>
                        <div class="colonne3 last">
                            <div class="informations">
                                <p>
                                    <span class="champs_label">{{ 'Magasinier' | trad }}</span>
                                    <span class="resultat">{{ utilisateur.NOM ~ ' (' ~ bonEntree.MAG ~ ')' }}</span>
                                </p>
                                <p>
                                    <span class="champs_label">{{ 'Site' | trad }}</span>
                                    <span class="resultat" data-site="{{ bonEntree.SITE }}" id="resultatSite">
                                        {{ siteStock.DESIGN ~ ' (' ~ bonEntree.SITE ~ ')' }}
                                    </span>
                                    <input type="hidden" id="input_SBE_ENTE_SITE" value="{{ bonEntree.SITE }}">
                                </p>
                                {% if is_prix_visible() %}
                                    <p>
                                        <span class="champs_label">{{ 'Total BE' | trad }}</span>
                                        <span class="resultat" id="tot_be" data-devise="{{ bonEntree.DEVISE }}">
                                            {{ bonEntree.TOT_BE | prix ~ ' ' ~ bonEntree.DEVISE }}
                                        </span>
                                    </p>
                                {% endif %}
                            </div>
                        </div>
                    </div>
                    <hr>
                    <div class="clearfix">
                        <div class="colonne2">
                            <div class="row mb-0">
                                {{ inputAdvance('TEXT', {
                                    'titre' : 'Origine' | trad,
                                    'name' : 'SBE_ENTE[ORIGINE]',
                                    'value' : bonEntree.ORIGINE,
                                    'pathValidator': path('Ajax.validator.isStringWithContent'),
                                    'paramsValidator': "{'sizeMax' : 100, 'sizeMin': 0}",
                                }) }}
                            </div>
                        </div>
                        <div class="colonne2 last">
                            <div class="row mb-0">
                                {{ inputAdvance('ZOOMADVANCE', {
                                    'titre': 'Provenance' | trad,
                                    'name' : 'SBE_ENTE[PROV]',
                                    'value' : bonEntree.PROV,
                                    'pahtZoom' : path('Ajax.liste.tiers'),
                                    'entity' : 'Tiers',
                                    'titleZoom' : 'Liste des tiers' | trad,
                                    'pathConvert' : path('Ajax.outil.convertLibelle'),
                                    'convertLibelle' : 'convertLibelleGlobal',
                                    'pathValidator' : path('Ajax.validator.zoom'),
                                    'paramsValidator' : "{'entity' : 'Tiers','nomChamp' : 'Provenance', 'null' : true}",
                                }) }}
                            </div>
                        </div>
                    </div>
                    {% if CSExist('SBE_ENTE') %}
                        <hr>
                        {{ inputCSAdvance({
                            'codTab' : 'SBE_ENTE',
                            'colonneActif' : true,
                            'codeEnr' : autoCompletCodeEnr(bonEntree.NO_BE),
                        }) }}
                    {% endif %}
                </details>
            </form>
            <form method="post" name="ligneInitialisation" id="ligneInitialisation">
                <details open class="block_edition">
                    <summary class="titre_block">{{ 'Pour initialisation des lignes' | trad }}</summary>
                    <div class="clearfix">
                        <div class="colonne2">
                            <div class="row">
                                <input id="input_SBE_ENTE_CODE_STE" type="hidden" name="SBE_ENTE[CODE_STE]" value="{{ bonEntree.CODE_STE }}">
                                {{ include('@Stock/Composant/ZoomAdvance/_section_imputation.twig', {
                                    name: 'SBE_LIGN[INIT][SECTIMP]',
                                    configAxeImp: configAxeImp,
                                    siObligatoire: false,
                                    codeSteInputId: 'input_SBE_ENTE_CODE_STE',
                                    value: '',
                                }) }}
                            </div>
                            <div class="row">
                                {{ include('@Stock/Composant/ZoomAdvance/_code_complementaire.twig', {
                                    value: '',
                                    configAxeImp: configAxeImp,
                                    name: 'SBE_LIGN[INIT][CODE_COMP]',
                                    codeSteInputId: 'input_SBE_ENTE_CODE_STE',
                                    cdcObligatoire: false
                                }) }}
                            </div>
                        </div>
                        <div class="colonne2 last">
                            {% if is_cg_active() %}
                            <div class="row">
                                {{ inputAdvance('TREEVIEW', {
                                    'titre' : 'Code gestion' | trad,
                                    'name' : 'SBE_LIGN[INIT][CODGES]',
                                    'pathTreeview' : path('Ajax.Treeview.codeGestion'),
                                    'paramsTreeview' : "{'CODE_SI': $('#'+CSS.escape('input_SBE_LIGN[INIT][SECTIMP]')).val(),  'cg_valide': 'on', 'CODE_STE': $('#'+CSS.escape('input_SBE_ENTE_CODE_STE')).val(), 'inputCG' : 'SBE_LIGN[INIT][CODGES]', 'affichageLibelle' : 1 }",
                                    'titleTreeview' : 'Code gestion' | trad,
                                    'value': post.SBE_ENTE.INIT.CODGES,
                                    'convertLibelle' : 'cgTrad',
                                    'pathValidator' : path('Ajax.validator.codeGestion'),
                                    'paramsValidator' : "{'nomChamp' : '"~ ('Code gestion' | trad |e('js')|raw)~"', 'null' : true, 'CODE_SI': $('#'+CSS.escape('input_SBE_LIGN[INIT][SECTIMP]')).val() }",
                                    '_pathConvert' : path('Ajax.outil.convertLibelle'),
                                    'entity' : 'Codgest',
                                    'codeBDD' : 'CODE_GES',
                                    'libelleBDD' : 'CODE_AFF',
                                    'codeSte' : "$('#input_SBE_ENTE_CODE_STE').val()",
                                    'siLink': 'input_SBE_LIGN[INIT][SECTIMP]',
                                    'codeAffCG' : post['text_SBE_LIGN[INIT][CODGES]'],
                                    'codeSteCG': post['input_SBE_LIGN_CODE_STE'],
                                    'sectimpu': post['input_SBE_LIGN[INIT][SECTIMP]'],
                                    'myFunction' : 'convertLibelleCodeGestion',
                                    'bulle' :  'true',
                                    'myParams' : '["'~path('Ajax.outil.convertLibelleCodeGestion')~'", this, "CODE_GES", "CODE_AFF", "input_SBE_LIGN[INIT][CODGES]", "text_SBE_LIGN[INIT][CODGES]", "' ~ post['SBE_LIGN[INIT][CODGES]'] ~ '", "1", "", "input_SBE_LIGN[INIT][SECTIMP]", 1, ]',
                                }) }}
                            </div>
                            {% endif %}
                            <div class="row">
                                {{ include('@Stock/Composant/ZoomAdvance/_sous_compte_analyse.twig', {
                                    value: '',
                                    configAxeImp: configAxeImp,
                                    name: 'SBE_LIGN[INIT][SSCPTEANA]',
                                    codeSteInputId: 'input_SBE_ENTE_CODE_STE',
                                    scaObligatoire: false
                                }) }}
                            </div>
                        </div>
                    </div>
                </details>
            </form>
            {{ include('@Stock/Composant/_crud_lign.twig', {
                ajouterFormPath: path('Stock.LigneBonEntree.ajouterForm', { noBe: bonEntree.NO_BE }),
                tableComposant: '@Stock/LigneBonEntree/Composant/_table.twig',
                tableComposantParameters: {
                    lignesBonEntree: lignesBonEntree,
                    bonEntree: bonEntree,
                    actions: true
                }
            }) }}
        </div>
    </div>
    <script type="application/javascript" src="{{ baseUrl('js/Modules/Stock/collectionForm.js') }}"></script>
    <script type="application/javascript" src="{{ baseUrl('js/Modules/Stock/lotsForm.js') }}"></script>
    <script type="application/javascript" src="{{ baseUrl('js/Modules/Stock/crudLign.js') }}"></script>
    <script type="application/javascript" src="{{ baseUrl('js/Modules/Stock/postAjaxForm.js') }}"></script>
    <script type="application/javascript" src="{{ baseUrl('js/Modules/Stock/validerEntete.js') }}"></script>
    <script type="application/javascript">
        addZoomChangeListenerOnDomChange();

        $('#enregistrerValider').on('click', () => {
            activeLoader();

            const form = $('#bonEntreFormModifier');
            submitForm(form, form.serializeArray(), onSubmitFormSuccessWithValidation);
        });

        $('#enregistrer').on('click', () => {
            activeLoader();

            const form = $('#bonEntreFormModifier');
            submitForm(form, form.serializeArray());
        });

        function onLignFormSuccessSpecific (data) {
            const totBe = $('#tot_be');

            totBe.text(data.TOT_BE + ' ' + totBe.attr('data-devise'));
        }

        function isDatePeremModifiable (dtePeremInputId, typeBe, path, notInputId) {
            if (typeBe !== 'R') {
                return;
            }

            $.ajax({
                type: 'POST',
                url: path,
                data: {
                    noLot: $('#' + CSS.escape(notInputId)).val(),
                    codeArt: $('#' + CSS.escape('input_SBE_LIGN[CODE_ART]')).val()
                },
                success: (data) => {
                    const dtePeremInput = $('#' + CSS.escape(dtePeremInputId));
                    if (data.lotGlobal) {
                        dtePeremInput.prop('readonly', true);
                        dtePeremInput.val(data.lotGlobal.DTE_PEREM);
                    } else {
                        dtePeremInput.prop('readonly', false);
                    }
                }
            });
        }
    </script>
{% endblock %}