{% extends 'timepiece/invoice/base.html' %}
{% load timepiece_tags %}
{% load bootstrap_toolkit %}
{% load url from future %}

{% block title %}Outstanding Hours{% endblock title %}

{% block crumbs %}
    {{ block.super }}
    <li><span class="divider">/</span> <a href="">Outstanding Hours</a></li>
{% endblock crumbs %}

{% block content %}
    <div class="row-fluid">
        <div class="span12">
            <h2>Outstanding Hours</h2>
            <ul class="nav nav-pills">
                <li>
                    <span>Outstanding Hours</span>
                </li>
                {% if perms.contracts.view_entry_group %}  {# FIXME: Permission doesn't exist #}
                    <li>
                        <a href="{% url 'list_invoices' %}">Previous Invoices</a>
                    </li>
                {% endif %}
            </ul>
        </div>
    </div>

    <div class="row-fluid">
        <div class="span12">
            {% date_filters "date-filter" "months" 0 %}
        </div>
    </div>

    <div class="row-fluid">
        <div class="span4">
            <form class="form-horizontal" method="get" action="" id="date-filter" accept-charset="utf-8">
                {{ date_form|as_bootstrap:"horizontal" }}
                <div class="control-group">
                    <div class="controls">
                        <input class="btn btn-primary" type='submit' name='yearmonth' value="Update" id='yearmonth'/>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div class="row-fluid">
        <div class="span6">
            {% if unverified %}
                <i class="icon-warning-sign"></i>
                <p><strong>Warning:</strong> The following users have <strong>unverified</strong> time during this period:</p>
                <ul>
                    {% for pk, first, last in unverified %}
                        <li>
                            <a href="{% user_timesheet_url pk from_date %}">{{ first }} {{ last }}</a>
                        </li>
                    {% endfor %}
                </ul>
            {% endif %}
            {% if unapproved %}
                <i class="icon-warning-sign"></i>
                <p><strong>Warning:</strong> The following users have <strong>unapproved</strong> time during this period:</p>
                <ul>
                    {% for pk, first, last in unapproved %}
                        <li>
                            <a href="{% user_timesheet_url pk from_date %}">{{ first }} {{ last }}</a>
                        </li>
                    {% endfor %}
                </ul>
            {% endif %}
            {% if not unverified and not unapproved %}
                <p>All time verified and approved</p>
            {% endif %}
        </div>
    </div>

    <div class="row-fluid">
        <div class="span12">
            {# Display each project type as a separate table. #}
            {# For each table, order by project status, then business display name, then project name. #}
            {% regroup project_totals by project.type.label as type_list %}
            {% for type in type_list %}
                <h3>Summary of {{ type.grouper }} Entries</h3>

                <table class="table table-bordered table-condensed table-striped">
                    <thead>
                        <tr>
                            <th rowspan="2">Project</th>
                            <th rowspan="2">Active Contracts</th>
                            <th rowspan="2">Business</th>
                            <th rowspan="2">Status</th>
                            <th colspan="2">Uninvoiced Hours</th>
                            <th rowspan="2"></th>
                        </tr>
                        <tr>
                            <th>Billable</th>
                            <th>Non-billable</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% regroup type.list by project.name as project_list %}
                        {% for project in project_list %}
                            <tr>
                                <td><a href="{% project_timesheet_url project.list.0.project.pk to_date %}">{{ project.grouper }}</a></td>
                                <td>
                                    {# FIXME: This generates a db query for each project; can we make it faster? #}
                                    {% for contract in project.list.0.project.get_active_contracts %}
                                        <a href="{{ contract.get_absolute_url }}">{{ contract.name }}</a>
                                        {% if not forloop.last %}<br />{% endif %}
                                    {% endfor %}
                                </td>
                                <td>
                                {{ project.list.0.project.business.get_display_name }}</td>
                                <td>{{ project.list.0.project.status.label|title }}</td>
                                <td class="hours">{% get_uninvoiced_hours project.list 'billable' %}</td>
                                <td class="hours">{% get_uninvoiced_hours project.list 'nonbillable' %}</td>
                                <td>
                                    {% if from_date %}
                                        <a href="{% url 'create_invoice' %}?project= {{ project.list.0.project.pk }}&to_date={{ to_date|date:'Y-m-d' }}&from_date={{ from_date|date:'Y-m-d' }}">Make Invoice</a>
                                    {% else %}
                                        <a href="{% url 'create_invoice' %}?project={{ project.list.0.project.pk }}&to_date={{ to_date|date:'Y-m-d' }}">Make Invoice</a>
                                    {% endif %}
                                </td>
                            </tr>
                        {% endfor %}
                    </tbody>
                </table>
            {% empty %}
                <p>There are no outstanding hours which match your filter criteria.</p>
            {% endfor %}
        </div>
    </div>
{% endblock content %}
