{% macro menu_item(item) %}
<a tabindex="-1" href="{{item.get_url()}}">
    {{_(item.label)}}</a>
{% endmacro %}

{% macro locale_menu(languages) %}
{% set locale = session['locale'] %}
{% if not locale %}
    {% set locale = 'en' %}
{% endif %}
{% if languages.keys()|length > 1 %}
<li class="dropdown">
    <a class="dropdown-toggle" data-toggle="dropdown" href="javascript:void(0)">
       <div class="f16"><i class="flag {{languages[locale].get('flag')}}"></i><b class="caret"></b>
       </div>
    </a>
    <ul class="dropdown-menu">
    <li class="dropdown">
        {% for lang in languages %}
            {% if lang != locale %}
                <a tabindex="-1" href="{{appbuilder.get_url_for_locale(lang)}}">
                  <div class="f16"><i class="flag {{languages[lang].get('flag')}}"></i> - {{languages[lang].get('name')}}
                </div></a>
            {% endif %}
        {% endfor %}
        </li>
     </ul>
</li>
{% endif %}
{% endmacro %}



{{ locale_menu(languages) }}
{% if not current_user.is_anonymous %}
    <li class="dropdown">
        <a class="dropdown-toggle" data-toggle="dropdown" href="#">
           <span class="fa fa-user"></span> {{g.user.get_full_name()}}<b class="caret"></b>
        </a>
        <ul class="dropdown-menu">
            {% set ns = namespace(firstEntry=true) %}
            {% for entryName in ['List Users', 'List Groups'] %}
                {% set entry = menu.find(entryName) %}
                {% if entry | is_menu_visible %}
                     {% if ns.firstEntry == true %}
                        {% set ns.firstEntry = false %}
                        <li class="title">Edit Users/Groups</li>
                     {% endif %}
                     <li class="entry">{{ menu_item(entry) }}</li>
                {% endif %}
            {% endfor %}
            {% set ns = namespace(firstEntry=true) %}
            {% for entryName in ['List S3Access', 'List S3Endpoints'] %}
                {% set entry = menu.find(entryName) %}
                {% if entry | is_menu_visible %}
                    {% if ns.firstEntry == true %}
                        {% set ns.firstEntry = false %}
                        <li class="title">Edit S3</li>
                    {% endif %}
                    <li class="entry">{{ menu_item(entry) }}</li>
                {% endif %}
            {% endfor %}
            <li class="title">User</li>
            <li class="entry"><a href="{{appbuilder.get_url_for_userinfo}}">{{_("Profile")}}</a></li>
            <li class="entry"><a href="{{appbuilder.get_url_for_logout}}">{{_("Logout")}}</a></li>
        </ul>
{% else %}
    <li><a href="{{appbuilder.get_url_for_login}}">
    <i class="fa fa-fw fa-sign-in"></i>{{_("Login")}}</a></li>
{% endif %}
