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

{% block body %}
    <div class="popupExport">
        <center>
            <div id="genetableau">
                {% set formName = 'choix_site_form' %}
                {% set path = path('Stock.StockArticle.popupAjoutStockLocal', { codeArt: codeArt }) %}
                <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('egal_SITE', infoPagination.filter['filter_egal_SITE'], '', 'genetableau', path, formName) }}</th>
                            <th>{{ filterTable('DESIGN', infoPagination.filter['filter_DESIGN'], '', 'genetableau', path, formName) }}</th>
                        </tr>
                        {% for siteStock in sitesStock %}
                            <tr>
                                <td>
                                    <label>
                                        <input type="radio" name="SSTOCKLO[SITE]" value="{{ siteStock.SITE }}">
                                    </label>
                                </td>
                                <td>{{ siteStock.SITE }}</td>
                                <td>{{ siteStock.DESIGN }}</td>
                            </tr>
                        {% endfor %}
                        <input type="hidden" name="SSTOCKLO[CODE_ART]" value="{{ codeArt }}">
                        <input type="hidden" name="niveau" value="2">
                        </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="ajoutStockLocal()">{{ 'Valider' | trad }}</button>
    </div>
    <script>
        function ajoutStockLocal () {
            const form = $('#{{ formName }}');
            const siteChoice = $('#{{ formName }}').find('input[name="SSTOCKLO[SITE]"]:checked').val();

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

                return;
            }

            form.attr('action', '{{ path('Stock.StockLocal.nouveauForm') }}');
            form.submit();
        }
    </script>
{% endblock %}