{#

This file is part of the Sonata package.

(c) Thomas Rabaix <thomas.rabaix@sonata-project.org>

For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.

#}

{% extends '@SonataAdmin/CRUD/base_list.html.twig' %}

{% import _self as tree %}

{% macro navigate_child(collection, admin, root, current_category_id, depth) %}
    {% import _self as tree %}

    {% if root and collection|length == 0 %}
        <div>
            <p class="bg-warning">{{ 'warning_no_category'|trans({}, admin.translationdomain) }}</p>
        </div>
    {% endif %}
    <ul{% if root %} class="sonata-tree sonata-tree--small js-treeview sonata-tree--toggleable"{% endif %}>
        {% for element in collection|filter(element => element is not null) %}
            <li>
                <div class="sonata-tree__item{% if element.id == current_category_id %} is-active{% endif %}"{% if depth < 2 %} data-treeview-toggled{% endif %}>
                    {% if element.parent or root %}<i class="fa fa-caret-right" data-treeview-toggler></i>{% endif %}
                    <a class="sonata-tree__item__edit" href="{{ url(app.request.attributes.get('_route'), app.request.query.all|merge({category: element.id})) }}">{{ element.name }}</a>
                </div>

                {% if element.children|length %}
                    {{ tree.navigate_child(element.children, admin, false, current_category_id, depth + 1) }}
                {% endif %}
            </li>
        {% endfor %}
    </ul>
{% endmacro %}

{% block list_table %}
    {% if datagrid.values['category']['value'] is defined %}
        <div class="col-xs-6 col-md-3">
            {{ tree.navigate_child([root_category], admin, true, datagrid.values['category']['value'], 1) }}
        </div>
        <div class="col-xs-12 col-md-9 no-padding">
            {{ parent() }}
        </div>
    {% else %}
        {{ parent() }}
    {% endif %}
{% endblock list_table %}
