{% extends 'layout.twig' %}

{% block body %}
<div class="contenu clearfix"  id="demandes-de-service">
    <h1>
        {{ 'Demandes de service' | trad }}
        <a href="{{ path('Stock.DemandeService.nouvelle') }}" class="button principal ajout button_right mr-0">
            {{ 'Nouvelle' | trad }}
        </a>
        {{ Button(
            'Export' | trad,
            false,
            '',
            'ajaxPopup(\''~path('Stock.DemandeService.popupExport')~'\', \''~'Export demande de Service' | trad~'\', \'\', \'\', \'\', \'modal_export \')', ''
        ) }}
    </h1>

    {{ ariane() }}

    <div class="tab-content" id="tab-content">
        <p></p>
        {% set formId = "filtres-demandes-de-service" %}
        <form method="post" name="filtres_demandes_de_service" id="{{ formId }}">
            <div class="tab-content">
                <div id="filtre" class="tab-pane active in clearfix bg_form padding_form border_form filtre_facture">
                    <div class="filtre">
                        {{ inputAdvance('SELECT', {
                            'titre' : 'Etat' | trad,
                            'name' : 'filter_SE#point#SOLDE_DS',
                            'value' : post['filter_SE#point#SOLDE_DS'],
                            'listeValue' : listeFiltreEtat,
                            'inputAttr': {'data-multifilter': 'form_SOLDE_DS', 'data-multifilter-twin': 'table_SOLDE_DS'},
                            'bulle' :  'true'
                        }) }}
                    </div>
                    <div class="filtre">
                        {{ inputAdvance('SELECT', {
                            'titre' : 'Type' | trad,
                            'name' : 'filter_SE#point#TYPE_DEM',
                            'value' : post['filter_SE#point#TYPE_DEM'],
                            'listeValue' : listeFiltreType,
                            'bulle' :  'true',
                            'inputAttr': {'data-multifilter': 'form_TYPE_DEM', 'data-multifilter-twin': 'table_TYPE_DEM'}
                        }) }}
                    </div>
                    <div class="filtre">
                        {{ inputAdvance('ZOOMADVANCE', {
                            'titre': 'N° de DA' | trad,
                            'name' : 'filter_egal_SL#point#NO_DA',
                            'value' : post['filter_egal_SL#point#NO_DA'],
                            'pahtZoom' : path('Ajax.liste.demandeAchat'),
                            'entity' : 'DaEntet',
                            'codeBDD': 'NO_DA',
                            'libelleBDD': 'NO_DEM',
                            'widthPopup': 'modal_large',
                            'titleZoom' : 'Liste des Demandes d\'Achat/Demandeur' | trad,
                            'pathConvert' : path('Ajax.outil.convertLibelle'),
                            'convertLibelle' : 'convertLibelleGlobal',
                            'bulle' :  'true',
                            'faceAttr': {'data-zoomLink': 'input_filter_egal_SL#point#NO_DA'}
                        }) }}
                    </div>
                    <div class="filtre">
                        {{ inputAdvance('ZOOMADVANCE', {
                            'titre': 'Société' | trad,
                            'name' : 'filter_SE#point#CODE_STE',
                            'value' : post['filter_SE#point#CODE_STE'],
                            'pahtZoom' : path('Ajax.liste.societe'),
                            'entity' : 'Societe',
                            'titleZoom' : 'Liste des sociétés' | trad,
                            'pathConvert' : path('Ajax.outil.convertLibelle'),
                            'convertLibelle' : 'convertLibelleGlobal',
                            'bulle' :  'true',
                            'inputAttr': {
                                'data-multifilter': 'form_CODE_STE',
                                'data-multifilter-twin': 'table_CODE_STE'
                            },
                            'faceAttr': {
                                'data-zoomLink': 'input_filter_SE#point#CODE_STE'
                            },
                        }) }}
                    </div>
                    <div class="filtre">
                        {{ inputAdvance('ZOOMADVANCE', {
                            'titre': 'Site' | trad,
                            'name' : 'filter_egal_SE#point#SITE',
                            'value' : post['filter_egal_SE#point#SITE'],
                            'pahtZoom' : path('Ajax.liste.siteStockAccessible'),
                            'paramZoom' : "{'opened_only': true }",
                            'entity' : 'SSiteSto',
                            'titleZoom' : 'Liste des sites de stockage' | trad,
                            'pathConvert' : path('Ajax.outil.convertLibelle'),
                            'convertLibelle' : 'convertLibelleGlobal',
                            'bulle' :  'true',
                            'faceAttr': {
                                'data-zoomLink': 'input_filter_egal_SE#point#SITE'
                            },
                        }) }}
                    </div>
                    <div class="filtre">
                        {% set exclude_empty_code = is_blanc_interdit_tiers() ? 'true': 'false' %}
                        {{ inputAdvance('ZOOMADVANCE', {
                            'titre': 'Tiers / Site  destinataire' | trad,
                            'name' : 'filter_SL#point#DESTINAT',
                            'value' : post['filter_SL#point#DESTINAT'],
                            'pahtZoom' : path('Ajax.liste.tiersDestinataire'),
                            'paramZoom' : "{'SITE_ORIGINE': $('#'+CSS.escape('input_filter_SL#point#ORIGINE')).val(), 'TYPE_DEM': $('#'+CSS.escape('input_filter_SE#point#TYPE_DEM')).val(),'exclude_empty_code': " ~ exclude_empty_code ~ " }",
                            'entity' : 'Tiers',
                            'titleZoom' : 'Liste des destinataires' | trad,
                            'pathConvert' : path('Ajax.outil.convertLibelle'),
                            'convertLibelle' : 'convertLibelleGlobal',
                            'bulle' :  'true',
                            'faceAttr': {
                                'data-zoomLink': 'input_filter_SL#point#DESTINAT'
                            },
                        }) }}
                        {{ include('@Stock/Composant/ZoomAdvance/_empty_code_tiers.twig') }}
                    </div>
                    <div class="filtre">
                        {{ include('@Stock/Composant/ZoomAdvance/_section_imputation.twig', {
                            name: 'filter_SL#point#SECTIMP',
                            configAxeImp: configAxeImp,
                            siObligatoire: false,
                            codeSteInputId: 'input_filter_SE#point#CODE_STE',
                            value: post['filter_SL#point#SECTIMP'],
                            bulle: 'true',
                            faceAttr: { 'data-zoomLink': 'input_filter_SL#point#SECTIMP' },
                        }) }}
                    </div>
                    <div class="filtre">
                        {{ include('@Stock/Composant/ZoomAdvance/_code_complementaire.twig', {
                            name: 'filter_SL#point#CODE_COMP',
                            configAxeImp: configAxeImp,
                            cdcObligatoire: false,
                            codeSteInputId: 'input_filter_SE#point#CODE_STE',
                            value: post['filter_SL#point#CODE_COMP'],
                            bulle: 'true',
                            faceAttr: { 'data-zoomLink': 'input_filter_SL#point#CODE_COMP' },
                        }) }}
                    </div>
                    <div class="filtre">
                        {{ include('@Stock/Composant/ZoomAdvance/_sous_compte_analyse.twig', {
                            name: 'filter_SL#point#SSCPTEANA',
                            configAxeImp: configAxeImp,
                            scaObligatoire: false,
                            codeSteInputId: 'input_filter_SE#point#CODE_STE',
                            value: post['filter_SL#point#SSCPTEANA'],
                            bulle: 'true',
                            faceAttr: { 'data-zoomLink': 'input_filter_SL#point#SSCPTEANA' },
                        }) }}
                    </div>
                    <div class="filtre">
                        {{ inputAdvance('TEXT', {
                            'titre' : 'Mots Clés' | trad,
                            'name' : 'MOTCLE',
                            'value' : post.MOTCLE,
                            'bulle' :  'true'
                        }) }}
                    </div>
                    <div class="filtre">
                        {{ inputAdvance('SWITCH', {
                            'titre' : 'Tous les mots ?' | trad,
                            'name' : 'TOUS_LES_MOTS',
                            'value' : post.TOUS_LES_MOTS,
                        }) }}
                    </div>
                    <div class="filtreBtn">
                        <button type="submit" class="principal button btn_filtre button_right last">
                            {{ 'Filtrer' | trad }}
                        </button>
                        <button class="secondaire button button_right" id="buttonCollapseFA"  id="buttonCollapseFA"  data-toggle="collapse" href="#filtre_avances" >
                            {{  'Filtres avancés' | trad }}
                        </button>
                        <button type="button" class="secondaire button button_right" onClick="resetFormFilters('{{ formId }}');">
                            {{ 'Vider les champs' | trad }}
                        </button>
                        <button type="button" class="secondaire button button_right" onClick="resetFormFilters('{{ formId }}', 'filter_SE#point#CODE_STE');">
                            {{ 'Vider les champs (sauf société)' | trad }}
                        </button>
                        <button type="button" class="secondaire button button_right" onClick="resetWithSession('{{ formId }}', '{{ sessionFilters }}');">
                            {{ 'Réinitialiser avec la session' | trad }}
                        </button>
                    </div>
                </div>
            </div>

            <div id="filtre_avances" class="collapse clearfix">
                {{ filtreAvance(post, filtreAvance, initFiltreAvance) }}
            </div>
            <div class="resultat_filtre">
                {{ bullFiltre(post, formId) }}
            </div>

            <div id='refreshTable'>
                <table class="table table-hover">
                    <thead class="thead-dark" >
                    <tr>
                        <th scope="col" width="70px">
                            {{ triTable('NO_DS', post.tri_NO_DS, 'N° DS' | trad, '', '', formId) }}
                        </th>
                        <th scope="col" width="110px">
                            {{ triTable('CODE_STE', post.tri_CODE_STE, 'Code société' | trad, '', '', formId) }}
                        </th>
                        <th scope="col" width="100px">
                            {{ triTable('DTE_CREAT', post.tri_DTE_CREAT, 'Date création' | trad, '', '', formId) }}
                        </th>
                        <th scope="col" width="100px">
                            {{ triTable('TYPE_DEM', post.tri_TYPE_DEM, 'Type' | trad, '', '', formId) }}
                        </th>
                        <th scope="col" width="550px">
                            {{ triTable('COMMENTAIRE', post.tri_COMMENTAIRE, 'Commentaire' | trad, '', '', formId) }}
                        </th>
                        <th scope="col" width="100px">
                            {{ triTable('SOLDE_DS', post.tri_SOLDE_DS, 'Etat' | trad, '', '', formId) }}
                        </th>
                        <th scope="col" width="100px">
                            {{ triTable('DTE_VALID', post.tri_DTE_VALID, 'Date validation' | trad, '', '', formId) }}
                        </th>
                        <th scope="col" width="40px">{{ 'Actions' | trad }}</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr class="recherche">
                        <td>
                            {{ filterTable('egal_SE#point#NO_DS',post['filter_egal_SE#point#NO_DS'],'', '', '',formId, '','N° DS' | trad) }}
                        </td>
                        <td>
                            {{ filterTable(
                                'SE#point#CODE_STE',
                                post['filter_SE#point#CODE_STE'],
                                '', '', '',formId, '',
                                'Code société' | trad, false, false, 'readonly', null, null, null,
                                {
                                    'data-multifilter': 'table_CODE_STE',
                                    'data-multifilter-twin': 'form_CODE_STE'
                                }
                            ) }}
                        </td>
                        <td>
                            {{ filterTable('date_SE#point#DTE_CREAT',post['filter_date_SE#point#DTE_CREAT'],'', '', '', formId,'datepicker','Date création' | trad) }}
                        </td>
                        <td>
                            {{ filterSelect(
                                'SE#point#TYPE_DEM',
                                post['filter_SE#point#TYPE_DEM'],
                                listeFiltreType,
                                formId, '','','','',
                                'Type' | trad, true,
                                {
                                    'data-multifilter': 'table_TYPE_DEM',
                                    'data-multifilter-twin': 'form_TYPE_DEM'
                                },
                                false
                            ) }}
                        </td>
                        <td>
                            {{ filterTable('M#point#CONTMEMO',post['filter_M#point#CONTMEMO'],'', '', '', formId,'','Commentaire' | trad) }}
                        </td>
                        <td>
                            {{ filterSelect(
                                'SE#point#SOLDE_DS',
                                post['filter_SE#point#SOLDE_DS'],
                                listeFiltreEtat,
                                formId, '','', '', '',
                                'Etat' | trad, true,
                                {
                                    'data-multifilter': 'table_SOLDE_DS',
                                    'data-multifilter-twin': 'form_SOLDE_DS'
                                },
                                false
                            ) }}
                        </td>
                        <td>
                            {{ filterTable('date_SE#point#DTE_VALID',post['filter_date_SE#point#DTE_VALID'],'', '', '',formId,'datepicker','Date validation' | trad) }}
                        </td>
                        <td></td>
                    </tr>

                    {% for demandeService in demandesServices %}
                        <tr id="demandeService{{ demandeService.NO_DS }}" class="{% if loop.index is divisible by(2) %}even{% else %}odd{% endif %}">
                            <td
                                class="code montant toggleSlide"
                                onclick="toggleSlide('{{ path('Stock.DemandeService.ligne', { noDs: demandeService.NO_DS }) }}','demandeService{{ demandeService.NO_DS }}detail', {id : {{ demandeService.NO_DS }}});"
                            >
                                {{ demandeService.NO_DS }}
                            </td>
                            <td>{{ demandeService.CODE_STE }}</td>
                            <td>{{ demandeService.DTE_CREAT | dateTrad }}</td>
                            <td>{{ listeFiltreType[demandeService.TYPE_DEM] }}</td>
                            <td>{{ shortLibelle(demandeService.COMMENTAIRE, 130) }}</td>
                            <td>{{ listeFiltreEtat[demandeService.SOLDE_DS] }}</td>
                            <td>{{ demandeService.DTE_VALID | dateTrad | visualise }}</td>
                            <td class="parametrage" >
                                {{ menuContextuel(
                                    'demandeService'~demandeService.NO_DS,
                                    false,
                                    demandeService.menuContextuel,
                                    10
                                ) }}
                            </td>
                        </tr>
                        <tr class="detail_ligne hide_detail" id="demandeService{{ demandeService.NO_DS }}detail"></tr>
                    {% endfor %}
                    </tbody>
                </table>
                {{ paginations(
                    path('Stock.DemandeService.gestion'),
                    infoPagination.nombreDePage,
                    infoPagination.page,
                    infoPagination.post,
                    formId
                ) }}
            </div>
        </form>

        {% for demandeService in demandesServices %}
            <form class="d-none" id="nouveauBon{{ demandeService.NO_DS }}" method="POST">
                <input type="hidden" name="NO_DS" value="{{ demandeService.NO_DS }}">
            </form>
        {% endfor %}
    </div>
    <script type="application/javascript" src="{{ baseUrl('js/Modules/Stock/postAjaxForm.js') }}"></script>
    <script type="text/javascript" src="{{ baseUrl('js/Modules/Stock/multiForm.js') }}"></script>
    <script type="text/javascript" src="{{ baseUrl('js/Modules/Stock/resetFormFilters.js') }}"></script>
    <script type="application/javascript" src="{{ baseUrl('js/Modules/Stock/zoomAdvanceReinitialiser.js') }}"></script>
    <script type="application/javascript" src="{{ baseUrl('js/Modules/Stock/validerEntete.js') }}"></script>
    <script type="text/javascript" src="{{ baseUrl('js/Modules/Facture/Facture.js') }}"></script>
    <script type="application/javascript">
        function devaliderDemandeService (path) {
            const content = '{{ 'Souhaitez-vous vraiment dévalider cette demande de service ?' | trad }}'
            activePopup(
                '{{ 'Dévalider cette demande de service' | trad }}',
                `<div class="modal-body bs-row flex-y-center">
                    <div class="icone-modal interrogation"></div>
                    <div class="bs-col p-base">${content}</div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="button secondaire"
                        data-dismiss="modal"
                        onclick="desactivePopup();"
                    >${window.oxaproc.trad['Annuler']}</button>
                    <button type="button" class="button principal" data-popupconfirm-btnconfirm>
                        ${window.oxaproc.trad['Confirmer']}
                    </button>
                </div>`,
                'modal_small'
            );

            document.querySelector('[data-popupconfirm-btnconfirm]').addEventListener('click', () => {
                activeLoader();
                $.ajax({
                    type: 'POST',
                    url: path,
                    success: (data) => {
                        window.location = data.redirectRoute
                    },
                    error: (data) => {
                        const response = data.responseJSON;
                        activePopup(response.popupTitle, response.popupContent, 'modal_small');
                        desactiveLoader();
                    }
                });
            });
        }

        function nouveauBon (noDs, action) {
            const form = $('#nouveauBon' + noDs);

            form.prop('action', action);
            form.submit();
        }
    </script>
</div>
{% endblock %}