{% extends 'layout.twig' %}
{% block body %}

    <div class="contenu">

        <h1>{{ 'Gestion des droits' | trad }}</h1>

        {{ ariane() }}
    
        <form method="post" name="filter_admin" id="filter_admin">

        <div id="userSelected">
            {% for usr in listeUsrSelected %}
                <input name="UserSelected[]" type="hidden" id="selected_{{usr.COD_USR}}" value="{{usr.COD_USR |replace({'oxaPoint' : "."})}}">
            {% endfor %}
        </div>


        <h1>
            {{ 'Gestion des utilisateurs associés au groupe' | trad }} : 

            <button type="button" class="principal" onClick="ajaxPopupV2('{{ path('Administration.GestionDroitUtilisateur.updateUserInGrpDroit', {idGrp: idGrp}) }}', 'filter_admin');">{{ 'Ajouter' | trad }}</button>
            {{ arianeAnnuler() }}
        </h1>

            <table class="table table-hover" id="mainTableCde">
                <thead class="thead-dark">
                    <tr>
                        <th scope="col">{{ triTable('CODE',  post.tri_CODE, 'Code' | trad, '', '', 'filter_admin') }}</th>
                        <th scope="col">{{ triTable('NOM',  post.tri_NOM, 'Nom' | trad, '', '', 'filter_admin') }}</th>
                        <th scope="col">{{ triTable('CODE_STE',  post.tri_CODE_STE, 'Société' | trad, '', '', 'filter_admin') }}</th>
                        <th scope="col">{{ triTable('CTR_ADR',  post.tri_CTR_ADR, 'Centre' | trad, '', '', 'filter_admin') }}</th>
                        <th scope="col" width="90px">{{ 'Actions' | trad }}</th>
                    </tr>
                </thead>
                
                <tbody>
                    <tr class="recherche" >
                        <td>{{ filterTable('CODE', post.filter_CODE, '', 'CODE', '','filter_admin', '','Code' | trad,'',true) }}</td>
                        <td>{{ filterTable('NOM', post.filter_NOM, '', 'NOM', '','filter_admin', '','Nom' | trad,'',true) }}</td>
                        <td>{{ zoomFilter('egal_CODE_STE', post['filter_egal_CODE_STE'], '', 'filter_admin', path('Ajax.liste.societe'),'Liste des sociétés' | trad, '','','Société' | trad,'','','libelle_Societe', '', '') }}</td>
                        <td>{{ zoomFilter('egal_CTR_ADR', post['filter_egal_CTR_ADR'], '', 'filter_admin', path('Ajax.liste.centre'),'Liste des centres' | trad, '','','Centre' | trad,'','','libelle_Societe', '', '') }}</td>
                        <td>
                            <input type="checkbox" class="form-control checkbox" data-checkall-input="clcheckbox" />
                        </td>
                    </tr>

                    {% for user in listeUser %}
                        <tr id="idUser{{ loop.index }}">
                            <td>{{ user.CODE |replace({'oxaPoint' : "."}) }}</td>
                            <td>{{ user.NOM }}</td>
                            <td>{{ user.LIB_SOC }} ({{ user.CODE_STE }})</td>
                            <td>{{ user.LIB_CTR }} ({{ user.CTR_ADR }})</td>
                            <td>
                                <input type="checkbox" 
                                    class="form-control checkbox chy" 
                                    id="user_{{user.CODE}}" 
                                    value="{{ user.CODE }}" 
                                    name="clcheckbox[]" 
                                    data-checkall="clcheckbox"
                                />
                            </td>
                        </tr>
                    {% endfor %}
                </tbody>
            </table>
            
            {{ paginations('',infoPagination.nombreDePage, infoPagination.page, infoPagination.post, '') }}
        </form>
    
    </div>

    <script>
        $( document ).ready(function() {
            $('#userSelected input').each(function( index ) {
                console.log('#input_'+$(this).attr('id').substring(9));
                $('#user_'+$(this).attr('id').substring(9)).prop( "checked", true );
            });
        });

        $('.chy').change(function() {
            if ($(this).is(':checked')) {
                $('#userSelected').append("<input name='UserSelected[]' type='hidden' id='selected_"+$(this).val()+"' value='"+$(this).val()+"'>");
            } else {
                $("#selected_"+$(this).val()).remove();
            }
        });


    </script>


{% endblock %}