{% extends '@LcShop/backend/default/list.html.twig' %}

{% block content_title %}
    Édition des positions
    {% if entity is defined and entity is not null %}
        <strong>: {{ entity.title }}</strong>
    {% endif %}
{% endblock %}
{% block batch_actions %}{% endblock %}
{% block global_actions %}{% endblock %}
{% block main %}
    {% set _fields_visible_by_user = fields|filter((metadata, field) => easyadmin_is_granted(metadata.permission)) %}
    {% set _number_of_hidden_results = 0 %}
    {% set _list_item_actions = easyadmin_get_actions_for_list_item(_entity_config.name) %}

    {{ form_start(postion_form) }}
    <div class="row">
        <div class="col-12">
            <div class="card card-outline card-primary">

                <div class="card-body p-0">

                    <table class="table lc-sortable"
                           data-parent-position="{{ entity is not null ? entity.position : '' }}">
                        <thead>
                        {% block table_head %}
                            <tr>
                                <th></th>
                                {% for field, metadata in _fields_visible_by_user %}
                                    {% set isSortingField = (metadata.property == app.request.get('sortField')) or ('association' == metadata.type and app.request.get('sortField') starts with metadata.property ~ '.') %}
                                    {% set nextSortDirection = isSortingField ? (app.request.get('sortDirection') == 'DESC' ? 'ASC' : 'DESC') : 'DESC' %}
                                    {% set _column_label = metadata.label|trans(_trans_parameters) %}
                                    {% set _column_icon = isSortingField ? (nextSortDirection == 'DESC' ? 'fa-arrow-up' : 'fa-arrow-down') : 'fa-sort' %}

                                    <th class="{{ isSortingField ? 'sorted' }} {{ metadata.virtual ? 'virtual' }} {{ metadata.dataType|lower }} {{ metadata.css_class }}" {{ easyadmin_config('design.rtl') ? 'dir="rtl"' }}>
                                        <span>{{ _column_label|raw }}</span>
                                    </th>
                                {% endfor %}

                            </tr>
                        {% endblock table_head %}
                        </thead>

                        <tbody>
                        {% block table_body %}
                            {% for item in paginator.currentPageResults %}
                                {% if not easyadmin_is_granted(_entity_config.list.item_permission, item) %}
                                    {% set _number_of_hidden_results = _number_of_hidden_results + 1 %}
                                {% else %}
                                    {# the empty string concatenation is needed when the primary key is an object (e.g. an Uuid object) #}
                                    {% set _item_id = '' ~ attribute(item, _entity_config.primary_key_field_name) %}
                                    <tr class="lc-draggable" data-id="{{ _item_id }}">
                                        <td>
                                            <i class="fa fa-fw fa-sort"></i>
                                        </td>

                                        {% for field, metadata in _fields_visible_by_user %}
                                            {% set isSortingField = metadata.property == app.request.get('sortField') %}
                                            {% set _column_label =  (metadata.label ?: field|humanize)|trans(_trans_parameters) %}

                                            <td class="{{ isSortingField ? 'sorted' }} {{ metadata.dataType|lower }} {{ metadata.css_class }}" {{ easyadmin_config('design.rtl') ? 'dir="rtl"' }}>
                                                {{ easyadmin_render_field_for_list_view(_entity_config.name, item, metadata) }}
                                            </td>
                                        {% endfor %}

                                    </tr>
                                {% endif %}
                            {% else %}
                                <tr>
                                    <td class="no-results" colspan="{{ _fields_visible_by_user|length + 1 }}">
                                        {{ 'search.no_results'|trans(_trans_parameters, 'EasyAdminBundle') }}
                                    </td>
                                </tr>
                            {% endfor %}

                           {# {% if _number_of_hidden_results > 0 %}
                                <tr class="datagrid-row-empty">
                                    <td class="text-center" colspan="{{ _fields_visible_by_user|length + 1 }}">
                            <span class="datagrid-row-empty-message"><i
                                        class="fa fa-lock mr-1"></i> {{ 'security.list.hidden_results'|trans({}, 'EasyAdminBundle') }}</span>
                                    </td>
                                </tr>
                            {% endif %}#}
                        {% endblock table_body %}
                        </tbody>
                    </table>
                    <div style="display: none;">
                        {{ form_row(postion_form.entities) }}

                        {{ form_rest(postion_form) }}
                    </div>

                </div>
            </div>
        </div>
    </div>

    <div class="card">
        <div class="card-footer">
            <div class="form-actions">
                {% set _action = easyadmin_get_action_for_edit_view('list', _entity_config.name) %}

                <div class="button-action">
                    <a class=" {{ _action.css_class|default('') }}" href="{{ path('easyadmin', _request_parameters|merge({ action: _action.name })) }}" target="{{ _action.target }}">
                        {{ _action.label is defined and not _action.label is empty ? _action.label|trans(_trans_parameters) }}
                    </a>

                    <button type="submit" class="btn btn-primary float-right"> Sauvegarder</button>
                </div>

            </div>
        </div>
    </div>
    {{ form_end(postion_form) }}

{% endblock main %}

{% block head_stylesheets %}
    {{ parent() }}

{% endblock %}
{% block plugin_javascript %}
    {{ parent() }}

    <script src="{{ asset('bundles/lcshop/js/backend/plugin/jquery-ui/jquery-ui.min.js') }}"></script>
{% endblock %}

{% block script_javascript %}
    {{ parent() }}
    <script src="{{ asset('bundles/lcshop/js/backend/script/default/init-sort.js') }}"></script>
{% endblock %}
