{% load i18n %}
{% load static %}

{% load unicms_contexts %}
{% load unicms_menus %}
{% load unicms_template_italia %}

{% for item in items %}

{% call obj=item method='link' request=request as link %}

{% comment %}
{% is_mega_menu item as is_mega_menu %}
{% endcomment %}

<li class="nav-item
           {% if item.has_childs %}dropdown{% endif %}
           megamenu">
    <a class="nav-link
              {% if item.has_childs %}dropdown_toggle{% endif %}
              {% if not forloop.counter0 %}pl-lg-2{% endif %}
              pr-lg-1"
       href={% if item.link %}"{{ link }}"{% else %}"#"{% endif %}>
            <span>{{ item.name }}</span>
    </a>
    {% if item.has_childs %}
    <span data-toggle="dropdown" aria-expanded="false"
          class="megamenu-dropdown-arrow m-lg-auto pb-lg-1">
        <svg class="icon icon-xs float-xs-right">
            <use xlink:href="{% static 'svg/sprite.svg' %}#it-expand"></use>
        </svg>
    </span>
    {% endif %}
    {% if item.get_childs %}

    {% load_item_childs item as childs %}

    <div class="dropdown-menu">
        <div class="row p-md-3 p-0">
            {% for child in childs %}

                {% call obj=child method='link' request=request as child_link %}

                <!-- If previous item hasn't got childs, close col div -->
                {% ifchanged child.has_childs %}
                    {% if child.has_childs and not forloop.first %}
                            </ul>
                        </div>
                    </div>
                    {% elif not child.has_childs %}
                    <div class="col-lg-4">
                        <div class="link-list-wrapper">
                            <ul class="link-list">
                    {% endif %}
                {% endifchanged %}

                {% if child.has_childs %}
                    {% load_item_childs child as subchilds %}

                    <!-- Render a full col div -->
                    <div class="col-lg-4">
                        <div class="link-list-wrapper">
                            <ul class="link-list">
                                <li>
                                    <b>
                                        <a class="list-item" href="{{ child_link }}">
                                            <span class="text-dark">{{ child.name }}</span>
                                        </a>
                                    </b>
                                </li>
                                {% for subchild in subchilds %}
                                {% call obj=subchild method='link' request=request as subchild_link %}
                                <li>
                                    <a class="list-item py-0 sub" href="{{ subchild_link}}">
                                        <span class="text-dark">{{ subchild.name }}</span>
                                    </a>
                                </li>
                                {% endfor %}
                            </ul>
                        </div>
                    </div>
                    <!-- If current item hasn't got childs, render a stack-->
                {% else %}
                    <!-- If previous item has child, render new col, else add items in stack -->
                        <li>
                            <b>
                                <a class="list-item" href="{{ child_link}}">
                                    <span class="text-dark">{{ child.name }}</span>
                                </a>
                            </b>
                        </li>

                        <!-- If last, close col -->

                        {% if forloop.last  %}
                                </ul>
                            </div>
                        </div>
                        {% endif %}
                {% endif %}

            {% endfor %}
            </div>
        </div>
    {% endif %}
</li>
{% endfor %}
