{% macro renderPager(pager, nodeId, search, orderedBy, order, max) %}
    {{
        pagerfanta(
            pager,
            'twitter_bootstrap_translated',
            {
                'proximity' : 1,
                'routeName': 'claro_resources_rights_users_with_rights_form',
                'routeParams': {
                    'node': nodeId,
                    'search': search,
                    'orderedBy': orderedBy,
                    'order': order,
                    'max': max
                }
            }
        )
    }}
{% endmacro %}

{% macro sortUrl(nodeId, search, orderedBy, order, max) %}
    {% if order == 'ASC' %}
        {% set newOrder = 'DESC' %}
    {% else %}
        {% set newOrder = 'ASC' %}
    {% endif %}
    {{ 
        path(
            'claro_resources_rights_users_with_rights_form',
            {
                'node': nodeId, 
                'search': search,
                'orderedBy': orderedBy,
                'order': newOrder,
                'max': max
            }
        )
    }}
{% endmacro %}

{% from _self import renderPager %}
{% from _self import sortUrl %}

{% if search != '' %}
    <div class="alert alert-info">
        {{ 'results_for_search_on'|trans({}, 'platform') }} :
        "{{ search }}"
    </div>
{% endif %}

<div class="row">
    <div class="input-group col-md-4 pull-left">
        <a href="#users-without-rights-list"
           class="btn btn-default"
           id="add-new-user-rights-btn"
           data-toggle="tab"
        >
            <i class="fa fa-plus-circle"></i>
            {{ 'add_user'|trans({}, 'platform') }}
        </a>
    </div>
    <div class="input-group col-md-4 pull-right">
        <input class="form-control search-user-with-rights-input"
               type="text"
               id="search-user-with-rights-input"
               placeholder="{{ 'search'|trans({}, 'platform') }}"
               value={{search}}
        >
        <span class="input-group-btn">
            <button class="filter btn btn-default search-user-btn" type="button" id="search-user-with-rights-btn">
                <i class="fa fa-search"></i>
            </button>
        </span>
    </div>
</div>
<br>

{% if users|length == 0 %}
    <div class="alert alert-warning">
        {{ 'no_user'|trans({}, 'platform') }}
    </div>
{% else %}
    <div class="table-responsive">
        <table class="table table-striped table-bordered table-condensed">
            <thead>
                <tr>
                    <th>
                        <a href="{{ sortUrl(resource.getId(), search, 'firstName', order, max) }}"
                           data-ordered-by="firstName"
                        >
                            {{ 'name'|trans({}, 'platform') }}
                        </a>
                    </th>
                    <th>
                        <a href="{{ sortUrl(resource.getId(), search, 'username', order, max) }}"
                           data-ordered-by="username"
                        >
                            {{ 'username'|trans({}, 'platform') }}
                        </a>
                    </th>
                    {% for decoder in resource.getResourceType().getMaskDecoders() %}
                        <th> {{ decoder.getName()|trans({}, 'resource') }} </th>
                    {% endfor %}
                    {% if isDir %}
                        <th> {{ 'creation'|trans({}, 'platform') }} </th>
                    {% endif %}
                </tr>
            </thead>
            <tbody>
                {% for user in users %}
                    {% if usersRoles[user.getUsername()] is defined %}
                        {% set roleId = usersRoles[user.getUsername()].getId() %}
                        <tr>
                            <td>
                                {{ user.getFirstName() }}
                                {{ user.getLastName() }}
                                <input type="hidden" name="role_row[{{ roleId }}]" value="{{ roleId }}"/>
                            </td>
                            <td>
                                {{ user.getUsername() }}
                            </td>

                            {% for decoder in resource.getResourceType().getMaskDecoders() %}
                                <td>
                                    <input name="roles[{{ roleId }}][{{ decoder.getName() }}]"
                                           type="checkbox"
                                           {% if usersRights[user.getUsername()] is defined and
                                                (usersRights[user.getUsername()].getMask() b-and decoder.getValue())
                                           %}
                                               checked
                                           {% endif %}
                                    >
                                </td>
                            {% endfor %}

                            {% if isDir %}
                                <td>
                                    <a data-resource-id="{{ resource.getId() }}"
                                       class="res-creation-options"
                                       href="{{ path('claro_resource_right_creation_form', {'node': resource.getId(), 'role': roleId }) }}"
                                    >
                                       <i class="fa fa-cog"></i> {{ 'options'|trans({}, 'platform') }}
                                    </a>
                                </td>
                            {% endif %}
                        </tr>
                    {% endif %}
                {% endfor %}
            </tbody>
        </table>
    </div>
        
    {% if users.getNbPages() > 1 %}
        {{ renderPager(users, resource.getId(), search, orderedBy, order, max) }}
    {% endif %}

    {% if isDir %}
        <br>
        <input id="advanced-recursive-option" name="isRecursive" type="checkbox"/>
        {{ 'recursive_dir_action'|trans({}, 'platform')}}
    {% endif %}
{% endif %}
        
<div id="users-with-rights-datas"
     data-node-id="{{ resource.getId() }}"
     data-search="{{ search }}"
     data-ordered-by="{{ orderedBy }}"
     data-order="{{ order }}"
     data-max="{{ max }}"
>
</div>