{% extends '@ajax-layout/layout.twig' %}

{% block body %}
    <div class="popupExport">
        <center>
            <div id="genetableau">
                {% set formName = 'ligne_cahier_inventaire_site_form' %}
                {% set path = path('Stock.LigneCahierInventaire.choixSitePopup', { noInv: noInv, action: action }) %}
                <form method="POST" id="{{ formName }}" name="{{ formName }}" action="{{ formAction }}">
                    <table class="table" id="tableau">
                        <thead class="thead-dark">
                            <tr>
                                <th>{{ 'Choix' | trad }}</th>
                                <th>{{ triTable('SITE',  infoPagination.filter.tri_SITE, 'Code' | trad, 'genetableau', path, formName) }}</th>
                                <th>{{ triTable('DESIGN',  infoPagination.filter.tri_DESIGN, 'Libellé' | trad, 'genetableau', path, formName) }}</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <th></th>
                                <th>{{ filterTable('SCI#point#SITE', infoPagination.filter['filter_SCI#point#SITE'], '', 'genetableau', path, formName) }}</th>
                                <th>{{ filterTable('STO#point#DESIGN', infoPagination.filter['filter_STO#point#DESIGN'], '', 'genetableau', path, formName) }}</th>
                            </tr>
                            {% for siteStock in sitesStock %}
                                <tr>
                                    <td>
                                        <label>
                                            <input type="radio" name="SITE" value="{{ siteStock.SITE }}">
                                        </label>
                                    </td>
                                    <td>{{ siteStock.SITE }}</td>
                                    <td>{{ siteStock.DESIGN }}</td>
                                </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </form>
                {{ paginations(
                    path,
                    infoPagination.nombreDePage,
                    infoPagination.page,
                    infoPagination.post,
                    'modal-body'
                ) }}
            </div>
            <p id="error_text" class="error text-center"></p>
        </center>
    </div>
    <div class="modal-footer">
        <button class="secondaire" onclick="desactivePopup();">{{'Annuler' | trad }}</button>
        <button class="principal button_right last mr-0" onclick="validateSite()">{{ 'Valider' | trad }}</button>
    </div>
    <script type="text/javascript" src="{{ baseUrl('js/Modules/Stock/uploadFile.js') }}"></script>
    <script type="text/javascript" src="{{ baseUrl('js/Modules/Stock/exportDoc.js') }}"></script>
    <script>
        function validateSite () {
            const siteChoice = $('#{{ formName }}').find('input[name="SITE"]:checked').val();

            if (siteChoice === undefined) {
                $('#error_text').text('{{ 'Vous devez sélectionner un site' }}');

                return;
            }

            activeLoader();

            const formId = '#{{ formName }}';
            if ('{{ action }}' === 'saisirResultats' || '{{ action }}' === 'voirResultats') {
                $(formId).submit();
            }
            if ('{{ action }}' === 'exporter') {
                exportStockDoc(
                    '{{ path('Stock.CahierInventaire.exporter', { noInv: noInv }) }}',
                    formId,
                    '{{ path('Ajax.Erreur.popupInformation') }}',
                    '{{ path('Ajax.Outil.appelAsync') }}',
                    '{{ 'Informations' | trad }}',
                    '{{ 'Votre document est en cours de téléchargement, vous allez recevoir un mail contenant votre document.' | trad }}'
                );
            }
        }
    </script>
{% endblock %}