{% load timepiece_tags %}
{% load url from future %}

<div class="navbar navbar-fixed-top navbar-inverse">
    <div class="navbar-inner">

        <div id="full-nav" class="container-fluid">
            {% block navbar-left %}
                <a class="brand" href="/">Timepiece</a>
                <ul class="nav pull-left">
                    {% block quick-clock-in %}
                        {% if perms.entries.can_clock_in %}
                            <li class="dropdown">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="icon-time icon-white"> </i><b class="caret"></b> </a>
                                <ul class="dropdown-menu">
                                    <li><a href="{% url 'clock_in' %}">Clock in...</a></li>
                                    <li><a href="{% url 'create_entry' %}">Add Entry...</a></li>
                                    {% if work_projects %}<li class="divider"></li>{% endif %}
                                    {% for project in work_projects %}
                                        <li><a href="{% url 'clock_in' %}?project={{ project.id }}">{{ project }}</a></li>
                                    {% endfor %}
                                    {% if leave_projects %}<li class="divider"></li>{% endif %}
                                    {% for project in leave_projects %}
                                        <li><a href="{% url 'clock_in' %}?project={{ project.id }}">{{ project }}</a></li>
                                    {% endfor %}
                                </ul>
                            </li>
                        {% endif %}
                    {% endblock quick-clock-in %}

                    {% if perms.entries.add_entry %}
                        <li><a href="{% url 'view_user_timesheet' request.user.pk %}">Time Sheet</a></li>
                    {% endif %}

                    {# FIXME: Some permissions don't exist #}
                    {% if perms.crm.view_project or perms.auth.view_user or perms.crm.view_business or perms.contracts.add_project_contract %}
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">CRM <b class="caret"></b></a>
                            <ul class="dropdown-menu">
                                {% if perms.crm.view_project %}
                                    <li><a href="{% url 'list_projects' %}">Projects</a></li>
                                {% endif %}
                                {% if perms.auth.view_user %}  {# FIXME: Permission doesn't exist #}
                                    <li><a href="{% url 'list_users' %}">Users</a></li>
                                {% endif %}
                                {% if perms.crm.view_business %}  {# FIXME: Permission doesn't exist #}
                                    <li><a href="{% url 'list_businesses' %}">Businesses</a></li>
                                {% endif %}
                                {% if perms.contracts.add_project_contract %}
                                    <li><a href="{% url 'list_contracts' %}">Contracts</a></li>
                                {% endif %}
                            </ul>
                        </li>
                    {% endif %}

                    {# FIXME: Some of those permissions don't exist #}
                    {% if perms.entries.can_clock_in or perms.entries.view_entry_summary or perms.entries.view_payroll_summary or perms.contracts.change_entrygroup or user.is_staff %}
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">Management <b class="caret"></b></a>
                            <ul class="dropdown-menu">
                                {% if perms.entries.view_entry_summary %}  {# FIXME: permission doesn't exist #}
                                    <li><a href="{% url 'report_billable_hours' %}">Billable Hours</a></li>
                                    <li><a href="{% url 'report_hourly' %}">Hourly</a></li>
                                {% endif %}
                                {% if perms.entries.view_payroll_summary %}
                                    <li><a href="{% url 'report_payroll_summary' %}">Payroll Summary</a></li>
                                    <li><a href="{% url 'report_productivity' %}">Productivity</a></li>
                                    {% if user.is_staff or perms.contracts.change_entrygroup or perms.entries.can_clock_in %}
                                        <li class="divider"></li>
                                    {% endif %}
                                {% endif %}
                                {% if user.is_staff %}
                                    <li><a href="{% url 'admin:index' %}">Admin</a></li>
                                {% endif %}
                                {% if perms.contracts.change_entrygroup %}
                                    <li><a href="{% url 'list_outstanding_invoices' %}">Invoices</a></li>
                                {% endif %}
                                {% if perms.entries.can_clock_in %}
                                    <li><a href="{% url 'view_schedule' %}">Weekly Schedule</a></li>
                                {% endif %}
                            </ul>
                        </li>
                    {% endif %}

                    {% include "timepiece/extra_nav.html" %}
                </ul>
            {% endblock navbar-left %}

            {% block navbar-right %}
                {% if user.is_authenticated %}
                    <ul class="nav pull-right">
                        <li class="dropdown">
                            <a href="" class="dropdown-toggle" data-toggle="dropdown"><i class="icon-user icon-white"></i> {{ user.first_name }} <b class="caret"></b></a>
                            <ul class="dropdown-menu">
                                <li><a href="{% url 'edit_settings' %}">Settings</a></li>
                                <li><a href="{% url 'change_password' %}">Change Password</a></li>
                                <li class="divider"></li>
                                <li><a href="{% url 'auth_logout' %}">Logout</a></li>
                            </ul>
                        </li>
                    </ul>
                {% else %}
                    <a class="btn btn-primary pull-right" href="{% url 'auth_login' %}">Login</a>
                {% endif %}

                {% block quick-search %}
                    {% if perms.crm.view_business %}
                        <form id="quick_search" class="navbar-search pull-right" action="{% url 'quick_search' %}" method="post" accept-charset="utf-8">
                            {% csrf_token %}
                            {{ quick_search_form.quick_search }}
                        </form>
                        <script>
                            $(document).ready(function() {
                                var quick_search = $('#quick_search :input[name=quick_search_0]');
                                quick_search.bind('djselectableselect', function(event, ui) {
                                    $(this).parents('form').submit();
                                });
                            });
                        </script>
                    {% endif %}
                {% endblock quick-search %}
            {% endblock navbar-right %}
        </div>

        <div id="collapsed-nav" class="container-fluid">
            <a class="brand" href="/">Timepiece</a>

            {% if user.is_authenticated %}
                <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </a>
            {% endif %}

            <div class="nav-collapse">
                <ul class="nav">
                    {% if perms.entries.add_entry %}
                        <li><a href="{% url 'dashboard' %}">Dashboard</a></li>
                        <li><a href="{% url 'view_user_timesheet' request.user.pk %}">Time Sheet</a></li>
                        <li><a href="{% url 'view_schedule' %}">Schedule</a></li>
                        <li><a href="{% url 'create_entry' %}">Add Entry</a></li>
                        <li><a href="{% url 'auth_logout' %}">Logout</a></li>
                    {% endif %}
                </ul>
            </div>
        </div>
    </div>
</div>
