{% extends "timepiece/time-sheet/base.html" %}
{% load timepiece_tags math_tags %}

{% block title %}Timepiece Entries{% endblock title %}

{% block bodyid %}dashboard{% endblock bodyid %}

{% block content %}
    {% if view_entries %}
        <div class="row-fluid">
            <div class="span12">
                <h2>My Time Sheet</h2>
                <ul class="unstyled subnav">
                    <li><a href="{% url timepiece-clock-in %}">Clock In</a></li>
                    <li><a href="{% url timepiece-add %}">Add Entry</a></li>
                </ul>
            </div>
        </div>
    {% endif %}

    {% if view_entries %}
        <div class="row-fluid hidden-desktop">
            <div class="span12">
                <h3>
                    {% with entries.count as num_entries %}
                        My Active Entries
                    {% endwith %}
                </h3>

                {% with my_active_entries as entries %}
                    <div class="scroll">
                        {% include "timepiece/time-sheet/_entry_list.html" with active="True" %}
                    </div>
                {% endwith %}
            </div>
        </div>
    {% endif %}

    {% if assignments %}
        <div class="row-fluid">
            <div class="span12">
                <h3>My Projects</h3>
                <div class="scroll">
                    <table class="table table-striped table-bordered table-condensed">
                        <thead>
                            <tr>
                                <th>Project</th>
                                <th>My Hours</th>
                                <th>Hours Worked</th>
                                <th>Hours Remaining</th>
                                <th>Due Date</th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody>
                            {% regroup assignments by contract.project.type as type_list %}
                            {% for type in type_list %}
                                <tr class="ledger_header">
                                    <th colspan="6">{{ type.grouper }}s</th>
                                </tr>
                                {% for assignment in type.list %}
                                    <tr>
                                        <td><a href="{% url timepiece-clock-in %}?project={{ assignment.contract.project.pk }}">{{ assignment.contract.project }}</a></td>
                                        <td class="hours">{{ assignment.num_hours }}</td>
                                        <td class="hours">{{ assignment.hours_worked|floatformat:2 }} ({% widthratio assignment.hours_worked assignment.num_hours 100 %}%)</td>
                                        <td class="hours">{{ assignment.hours_remaining|floatformat:2 }}</td>
                                        <td>{{ assignment.end_date }}</td>
                                        <td>
                                            <ul class="actions">
                                                <li><a href="{% url timepiece-add %}?project={{ assignment.contract.project.pk }}">Add Entry</a></li>
                                                {% if perms.timepiece.view_entry_summary %}
                                                    <li><a href="{% url view_contract assignment.contract.pk %}">Report</a></li>
                                                {% endif %}
                                            </ul>
                                        </td>
                                    </tr>
                                {% endfor %}
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    {% endif %}

    {% if view_entries %}
        <div class="row-fluid visible-desktop">
            <div class="span12">
                <h3>
                    {% with entries.count as num_entries %}
                        My Active Entries
                    {% endwith %}
                </h3>

                {% with my_active_entries as entries %}
                    <div class="scroll">
                        {% include "timepiece/time-sheet/_entry_list.html" with active="True" %}
                    </div>
                {% endwith %}
            </div>
        </div>
    {% endif %}

    <div class="row-fluid">
        {% if allocations or project_entries or my_active_entries %}
            <div class="span12">
                <h3>My Work This Week</h3>
                <table class="table table-bordered table-condensed">
                    <thead>
                        <tr>
                            <th>Project</th>
                            <th>Hours Worked</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% if my_active_entries %}
                            <tr class="ledger_header">
                                <th colspan="2">Current Entry</th>
                            </tr>

                            {% for entry in my_active_entries %}
                                <tr>
                                    <td><a href="{% url timepiece-clock-in %}?project={{ entry.project_id }}">{{ entry.project }}</a></td>
                                    <td class="hours">{% get_active_hours entry %}</td>
                                </tr>
                            {% endfor %}
                        {% endif %}

                        {% for assignment in allocations %}
                            <tr>
                                <td><a href="{% url timepiece-clock-in %}?project={{ assignment.assignment.contract.project.pk }}">{{ assignment.assignment.contract.project }}</a></td>
                                <td class="hours">
                                    {% bar_graph assignment.assignment.contract.project assignment.hours_worked assignment.hours %}
                                </td>
                            </tr>
                        {% endfor %}

                        {% if project_entries %}
                            <tr class="ledger_header">
                                <th colspan="2">Past Entries</th>
                            </tr>

                            {% for entry in project_entries %}
                                <tr>
                                    <td><a href="{% url timepiece-clock-in %}?project={{ entry.project__pk }}">{{ entry.project__name }}</a></td>
                                    <td class="hours">{{ entry.sum }}</td>
                                </tr>
                            {% endfor %}
                        {% endif %}

                        <tr class="ledger_header">
                            <th colspan="2">Time Totals</th>
                        </tr>

                        {% for row in activity_entries %}
                            <tr>
                                <th>Total {% if row.billable %}billable{% else %}non-billable{% endif %} time:</th>
                                <td class="hours">
                                    {{ row.sum }}
                                </td>
                            </tr>
                        {% endfor %}

                        <tr>
                            <th> Total time this week: </th>
                            <td class="hours">
                                {% ifnotequal schedule.count 0 %}
                                    {% bar_graph "" current_total schedule.0.hours_per_week %}
                                {% else %}
                                    {{ current_total }}
                                {% endifnotequal %}
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        {% endif %}
    </div>


    {% if allocations %}
        <div class="row-fluid">
            <div style="height: 320px; width: 320px;" id="my-weekly-hours-chart"></div>
        </div>
    {% endif %}

    {% if view_entries %}
        <div class="row-fluid">
            <div class="span12">
                <h3>
                    {% with this_weeks_entries.count as num_entries %}
                        Time Detail This Week ({{ num_entries }} entr{{ num_entries|pluralize:"y,ies" }})
                    {% endwith %}
                </h3>

                {% with this_weeks_entries as entries %}
                    <div class="scroll">
                        {% include "timepiece/time-sheet/_entry_list.html" %}
                    </div>
                {% endwith %}
            </div>
        </div>
    {% endif %}
{% endblock %}
