{% macro menu_item(item) %}
	<a tabindex="-1" href="{{item.get_url()}}">
   	{% if item.icon %}
        <i class="fa {{item.icon}}"></i>&nbsp;
    {% endif %}
	{{_(item.label)}}</a>
{% endmacro %}

{% macro menu_debug(menu) %}
    {% for item1 in menu.get_list() %}
        {{ item1.label }} {{ item1.href }}
            {% for item2 in item1.childs %}
		        {{ item2.label }} {{ item2.href }}
        	{% endfor %}
	{% endfor %}
{% endmacro %}


{% macro menu_block(menu) %}
	{% for item1 in menu.get_list() %}
		{% if item1 | is_menu_visible %}
			{% if item1.childs %}
				<li class="dropdown">
				<a class="dropdown-toggle" data-toggle="dropdown" href="javascript:void(0)">
                {% if item1.icon %}
                    <i class="fa {{item1.icon}}"></i>&nbsp;
                {% endif %}
				{{_(item1.label)}}<b class="caret"></b></a>
		        <ul class="dropdown-menu">
				{% set divider = False %}
                {% for item2 in item1.childs %}
                    {% if item2.name == '-' %}
                        {% set divider = True %}
                    {% else %}
                        {% if item2 | is_menu_visible %}
                            {% if divider %}
                                <li class="divider"></li>
                                {% set divider = False %}
                            {% endif %}
					        <li>{{ menu_item(item2) }}</li>
                        {% endif %}
					{% endif %}
				{% endfor %}
				</ul></li>
			{% else %}
				<li>
                    {{ menu_item(item1) }}
				</li>
        	{% endif %}
        {% endif %}
    {% endfor %}

{% endmacro %}

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

{% macro navbar_block(menu, languages) %}

<div class="navbar {{menu.extra_classes}}" role="navigation">
   <div class="container">
        <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                {% if appbuilder.app_icon %}
                	<a class="navbar-brand" href="{{appbuilder.get_url_for_index}}">
                	<img src="{{appbuilder.app_icon}}" >
                	</a>
                {% else %}
                	<span class="navbar-brand">
                	<a href="{{appbuilder.get_url_for_index}}">
                	{{ appbuilder.app_name }}
                	</a>
                	</span>
                {% endif %}
        </div>
        <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                        {{ menu_block(menu)}}
                </ul>
                <ul class="nav navbar-nav navbar-right">
                {{ locale_menu(languages) }}
                {% if not current_user.is_anonymous() %}
				    <li><a href="{{appbuilder.get_url_for_logout}}">
                    <i class="fa fa-sign-out"></i>{{_("Logout")}}</a></li>
                    <li><a class="pull-right" href="{{appbuilder.get_url_for_userinfo}}">
                    <i class="fa fa-user"></i> {{g.user.get_full_name()}}</a></li>
                {% else %}
                    <li><a href="{{appbuilder.get_url_for_login}}">
                    <i class="fa fa-sign-in"></i>{{_("Login")}}</a></li>
                {% endif %}
                </ul>
        </div>
   </div>
</div>
{% endmacro %}
