{% load i18n %}
{% load rules %}

    <nav class="navbar navbar-default navbar-fixed-top" role="navigation">
        <div class="container">

            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbar-collapse">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="{% url 'home' %}">{{ request.site.name }}</a>
            </div>

            <div class="collapse navbar-collapse" id="navbar-collapse">
                <ul class="nav navbar-nav">

                    <li>
                        <a href="{% url 'query:query' %}">{% trans 'Query' %}</a>
                    </li>
                    <li>
                        <a href="{% url 'contact:contact' %}">{% trans 'Contact' %}</a>
                    </li>

                </ul>

                <ul class="nav navbar-nav navbar-right">

                    {% if user.is_authenticated %}

                    {% has_perm 'daiquiri_auth.view_profile' request.user as can_view_view_profile %}
                    {% has_perm 'daiquiri_contact.view_contactmessages' request.user as can_view_contactmessages %}
                    {% has_perm 'daiquiri_metadata.view_databases' request.user as can_view_databases %}
                    {% has_perm 'daiquiri_query.view_examples' request.user as can_view_examples %}

                    {% if can_view_view_profile or can_view_databases or can_view_databases or can_view_examples %}

                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
                            {% trans 'Management' %}<span class="caret"></span>
                        </a>
                        <ul class="dropdown-menu">
                            {% if can_view_view_profile %}
                            <li>
                                <a href="{% url 'auth:users' %}">{% trans 'User management' %}</a>
                            </li>
                            {% endif %}

                            {% if can_view_contactmessages %}
                            <li>
                                <a href="{% url 'contact:messages' %}">{% trans 'Contact messages' %}</a>
                            </li>
                            {% endif %}

                            {% if can_view_databases %}
                            <li>
                                <a href="{% url 'metadata:management' %}">{% trans 'Metadata management' %}</a>
                            </li>
                            {% endif %}

                            {% if can_view_examples %}
                            <li>
                                <a href="{% url 'query:examples' %}">{% trans 'Query examples' %}</a>
                            </li>
                            {% endif %}
                        </ul>
                    </li>

                    {% endif %}

                    {% if request.user.is_staff %}
                    <li>
                        <a href="{% url 'admin:index' %}">{% trans 'Admin' %}</a>
                    </li>
                    {% endif %}

                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
                            {{ user.profile.full_name }}<span class="caret"></span>
                        </a>
                        <ul class="dropdown-menu">
                            <li>
                                <a href="{% url 'account_profile' %}">{% trans 'Update profile' %}</a>
                            </li>
                            <li>
                                <a href="{% url 'account_email' %}">{% trans 'Update email' %}</a>
                            </li>
                            <li>
                                <a href="{% url 'account_change_password' %}">{% trans 'Change password' %}</a>
                            </li>
                            <li>
                                <a href="{% url 'socialaccount_connections' %}">{% trans 'Account connections' %}</a>
                            </li>
                            <li>
                                <a href="{% url 'account_token' %}">{% trans 'API token' %}</a>
                            </li>
                            <li role="separator" class="divider"></li>
                            <li>
                                {% include 'account/logout_form.html' %}
                            </li>
                        </ul>
                    </li>

                    {% else %}

                    <li>
                        <a href="{% url 'account_login' %}">{% trans 'Login' %}</a>
                    </li>

                    {% endif %}

                </ul>
            </div>
        </div>
    </nav>
