{% extends 'knp_menu.html.twig' %}

{% block label %}
    {#% if item.labelAttribute('icon') %}<i class="{{ item.labelAttribute('icon') }}"></i>{% endif %#}
    {% if not item.labelAttribute('iconOnly') %}
        {% if options.allow_safe_labels and item.getExtra('safe_label', false) %}{{ item.label|trans|raw }}{% else %}{{ item.label|trans }}{% endif %}
    {% endif %}
    {#% if item.labelAttribute('data-image') %}<img src="{{ item.labelAttribute('data-image') }}" alt="{{ item.name }}" class="menu-thumbnail"/>{% endif %#}
    {#% if item.hasChildren and options.depth is not same as(0) and item.displayChildren %}
        <span class="pull-right-container">
            <i class="fas fa-angle-left pull-right"></i>
        </span>
    {% endif %#}
{% endblock %}

{% block list %}
    {% if item.hasChildren and options.depth is not same as(0) and item.displayChildren %}
        {% import "knp_menu.html.twig" as macros %}

        {% if matcher.isAncestor(item) %}
            {%- set listAttributes = listAttributes|merge({class: (listAttributes.class|default('') ~ ' in')|trim}) -%}
        {% endif %}
        {% if not item.isRoot %}
            {%- set listAttributes = listAttributes|merge({class: (listAttributes.class|default('') ~ ' treeview-menu')|trim}) -%}
        {% endif %} 
        <ul{{ macros.attributes(listAttributes) }}>
            {{ block('children') }}
        </ul>
    {% endif %}
{% endblock %}

{% macro attributes(attributes) %}
    {% for name, value in attributes %}
        {%- if value is not none and value is not same as(false) -%}
            {{- ' %s="%s"'|format(name, value is same as(true) ? name|e : value|e)|raw -}}
        {%- endif -%}
    {%- endfor -%}
{% endmacro %}

{% block spanElement %}
    {% import "knp_menu.html.twig" as macros %}
    {% if item.attribute('class') matches '/(^|\s+)header(\s+|$)/' %}
        {{ block('label') }}
    {% else %}
        <a{{ macros.attributes(item.labelAttributes) }}>
            {{ block('label') }}
        </a>
    {% endif %}
{% endblock %}
{% block linkElement %}
    {% import _self as knp_menu %}
    {% if (item.name != 'Activités par arrondissement') or  (is_granted('ROLE_MANAGER') and item.name == 'Activités par arrondissement') %}
    <a href="{{ item.uri }}" class="nav-link{% if matcher.isCurrent(item) != matcher.isAncestor(item,1) %} active {% endif %}">
        {% if item.attribute('icon') is not empty %}
            <i class="nav-icon fas {{ item.attribute('icon') }}"></i>
        {% else %}
            <i class="nav-icon far fa-circle"></i>
        {% endif %}
        <p>
            {% if item.name == 'Sensibilité par ministère' %}
                {% if is_granted('ROLE_MANAGER') %}
                    Sensibilité par entité
                {% elseif app.user.direction %}
                    Sensibilité du ministère
                {% elseif app.user.commune  %}
                    Sensibilité de la commune
                {% elseif is_granted('ROLE_DDD') %}
                    Sensibilité par commune
                {% endif %}
            {% elseif item.name == 'Ministère / ODD' %}
                {% if is_granted('ROLE_MANAGER') %}
                    Structure / ODD
                {% elseif app.user.direction %}
                    Ministère / ODD
                {% elseif app.user.commune  %}
                    Commune / ODD
                {% elseif is_granted('ROLE_DDD') %}
                    Commune / ODD
                {% endif %}
            {% elseif item.name == 'Ministère / Cible' %}
                {% if is_granted('ROLE_MANAGER') %}
                    Structure / Cible
                {% elseif app.user.direction %}
                    Ministère / Cible
                {% elseif app.user.commune  %}
                    Commune / Cible
                {% elseif is_granted('ROLE_DDD') %}
                    Commune / Cible
                {% endif %}
            {% elseif item.name == 'ODD / Ministère' %}
                {% if is_granted('ROLE_MANAGER') %}
                    ODD / Structure
                {% elseif app.user.direction %}
                    ODD / Ministère
                {% elseif app.user.commune  %}
                    ODD / Commune
                {% elseif is_granted('ROLE_DDD') %}
                    ODD / Commune
                {% endif %}
            {% elseif item.name == 'Activité / Ministère' %}
                {% if is_granted('ROLE_MANAGER') %}
                    Activités / Structure
                {% elseif app.user.direction %}
                    Activités / Ministère
                {% elseif app.user.commune  %}
                    Activités / Commune
                {% elseif is_granted('ROLE_DDD') %}
                    Activités / Commune
                {% endif %}
            {% else %}
                {{ block('label') }} 
            {% endif %}

            {% if item.hasChildren %}
                <i class="right fas fa-angle-left"></i>
            {% endif %}
        </p>
    </a>
    {% endif %}
{% endblock %}