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

{% block extrajs %}
    <script src="{{ STATIC_URL }}bootstrap/js/bootstrap-tab.js" type="text/javascript" charset="utf-8"></script>
{% endblock %}

{% block crumbs %}
    {{ block.super }}
    <li><span class="divider">/</span> <a href="{% url 'view_person_time_sheet' user.pk %}">My Ledger</a></li>
{% endblock crumbs %}

{% block title %}
    {% if timesheet_user.get_full_name %}
        {{ timesheet_user.get_full_name }}'s
    {% else %}
        {{ timesheet_user.username }}'s
    {% endif %}
    Time Sheet
{% endblock %}

{% block content %}
    <div class="row">
        <div class="span12">
            <h2>
                {% if timesheet_user.get_full_name %}
                    {{ timesheet_user.get_full_name }}'s
                {% else %}
                    {{ timesheet_user.username }}'s
                {% endif %}
                TimeSheet
                ({{ from_date|date:"N j, Y" }} &mdash; {{ to_date|date:"N j, Y" }})
            </h2>
            <ul class="unstyled subnav">
                {% if perms.timepiece.view_user %}
                    <li><a href="{% url "view_person" timesheet_user.pk %}">User Information</a></li>
                {% endif %}
                {% if show_verify %}
                    <li><a href="{% url "change_person_time_sheet" "verify" timesheet_user.id from_date|date:"Y-m-d" %}">Verify Time Sheet</a></li>
                {% endif %}
                {% if show_approve %}
                    <li><a href="{% url "change_person_time_sheet" "approve" timesheet_user.id from_date|date:"Y-m-d" %}">Approve Time Sheet</a></li>
                {% endif %}
            </ul>
        </div>
    </div>

    <div class="row">
        <div class="span12">
            <form class="form-inline" method="get" action="" accept-charset="utf-8">
                {{ year_month_form|as_bootstrap:"inline" }}
                <input type="submit" name="yearmonth" class="btn" value="Update" />
            </form>
        </div class="span12">
    </div>

    <div class="row">
        <div class="span12">
            <ul class="nav nav-tabs">
                <li class="active"><a href="#overview" data-toggle="tab">Overview</a></li>
                <li><a href="#full-timesheet" data-toggle="tab">Full Timesheet</a></li>
                <li><a href="#hourly-summary" data-toggle="tab">Hourly Summary</a></li>
            </ul>
            <div class="tab-content">
                <div class="tab-pane active" id="overview">
                    <div class="row">
                        {% if project_entries %}
                            <div class="span6">
                                <h3>Project Summary</h3>
                                <table class="table table-bordered table-striped table-condensed">
                                    <thead>
                                        <tr>
                                            <th>Project</th>
                                            <th>Hours</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        {% for entry in project_entries %}
                                            <tr>
                                                <td>{{ entry.project__name}} </td>
                                                <td class="hours">{{ entry.sum|floatformat:2 }}</td>
                                            </tr>
                                        {% endfor %}
                                    </tbody>
                                </table>
                            </div>
                        {% endif %}
                        {% if entries %}
                            <div class="span6">
                                <h3>Billable Summary</h3>
                                <table class="table table-bordered table-striped table-condensed">
                                    <thead>
                                        <tr>
                                            <th>Category</th>
                                            <th>Hours</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr>
                                            <td>Billable</td><td class="hours">{{summary.billable|floatformat:2 }}</td>
                                        </tr>
                                        <tr>
                                           <td>Non-billable</td><td class="hours">{{summary.non_billable|floatformat:2 }}</td>
                                        </tr>
                                        <tr>
                                            <td>Invoiced</td><td class="hours">{{summary.invoiced|floatformat:2 }}</td>
                                        </tr>
                                        <tr>
                                            <td>Uninvoiced</td><td class="hours">{{summary.uninvoiced|floatformat:2 }}</td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        {% else %}
                            <div class="span12">
                                <p>No entries exist for this time sheet.</p>
                            </div>
                        {% endif %}
                    </div>
                </div>
                <div class="tab-pane" id="full-timesheet">
                    {% if entries %}
                        {% for entry in entries %}
                            {% ifchanged entry.start_time|date:"W" %}
                                <table class="table table-bordered table-condensed table-striped">
                                    <thead>
                                        <tr>
                                            <th>Date</th>
                                            <th>Project</th>
                                            <th>Activity</th>
                                            <th>Location</th>
                                            <th>Time In</th>
                                            <th>Time Out</th>
                                            <th>Breaks</th>
                                            <th>Hours</th>
                                            <th width="40%">Comments</th>
                                            <th></th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr class="ledger_header">
                                            <th colspan="10">Week of {% week_start entry.start_time %} </th>
                                        </tr>
                            {% endifchanged %}
                            <tr>
                                {% ifchanged entry.start_time|date %}
                                <td>{{ entry.start_time|date:"m/d/Y (D)" }}</td>
                                {% else %}
                                <td></td>
                                {% endifchanged %}
                                <td>{{ entry.project__name }}</td>
                                <td>{{ entry.activity__name }}</td>
                                <td>{{ entry.location__name }}</td>
                                <td>{{ entry.start_time|date:"P" }}</td>
                                <td>{{ entry.end_time|date:"P" }}</td>
                                <td class="hours">{{ entry.seconds_paused|seconds_to_hours|floatformat:2 }}</td>
                                <td class="hours">{{ entry.hours|floatformat:2 }}</td>
                                <td title="{{entry.comments}}">{{ entry.comments|truncatewords:12|urlizetrunc:25|safe }}</td>
                                <td>
                                    {% if entry.status == "unverified" %}
                                        <a href="{% url "timepiece-update" entry.id %}?next={{ request.get_full_path|urlencode }}#full-timesheet">Edit</a>
                                    {% else %}
                                        {% if perms.timepiece.change_user and entry.status != "invoiced" %}
                                            <a href="{% url "timepiece-reject-entry" entry.id %}?next={{ request.get_full_path|urlencode }}#full-timesheet">Reject</a>
                                        {% endif %}
                                    {% endif %}
                                </td>
                            </tr>
                            {% if entry.start_time.weekday == 6 %}
                                    </tbody>
                                </table>
                            {% endif %}
                        {% endfor %}
                        <table class="table table-bordered table-condensed">
                            <tr class="monthly_total">
                                <th colspan="4">Overall total for the period:</th>
                                <td class="hours">{{ summary.total|floatformat:2 }}</td>
                            </tr>
                        </table>
                    {% else %}
                        <p>No entries exist for this time sheet.</p>
                    {% endif %}
                </div>
                <div class="tab-pane" id="hourly-summary">
                    {% if grouped_totals %}
                        {% for week, week_totals, days in grouped_totals %}
                            <table class="table table-bordered table-condensed">
                                <tr class="ledger_header">
                                    <th colspan="2">Week of {{ week|date:"m/d/Y (D)" }} </th>
                                    <th>Billable</th>
                                    <th>Non Billable</th>
                                    <th>Total</th>
                                </tr>
                                {% for day, projects in days %}
                                    {% for project, totals in projects.1.iteritems %}
                                        <tr class="{% if day < from_date %}before_from_date{% endif %}">
                                            <td>
                                            {% if forloop.counter == 1 %}
                                                {{day|date:"m/d/Y (D)"}}
                                            {% endif %}
                                            </td>
                                            <td>
                                                {{project}}
                                            </td>
                                            <td class="hours">{{ totals.billable|floatformat:2 }}</td>
                                            <td class="hours">{{ totals.non_billable|floatformat:2 }}</td>
                                            <td class="hours">{{ totals.total|floatformat:2 }}</td>
                                        </tr>
                                    {% endfor %}
                                    <tr class="daily_total {% if day < from_date %}before_from_date{% endif %}">
                                        <td></td>
                                        <td class="hours"><strong>{{day|date:"D"}}:</strong></td>
                                        <td class="hours">{{ projects.0.billable|floatformat:2 }}</td>
                                        <td class="hours">{{ projects.0.non_billable|floatformat:2 }}</td>
                                        <td class="hours">{{ projects.0.total|floatformat:2 }}</td>
                                    <tr/>
                                {% endfor %}
                                <tr class="weekly_total">
                                    <th colspan="2">Totals for this week:</th>
                                    <td class="hours">{{ week_totals.billable|floatformat:2 }}</td>
                                    <td class="hours">{{ week_totals.non_billable|floatformat:2 }}</td>
                                    <td class="hours">{{ week_totals.total|floatformat:2 }}</td>
                                </tr>
                            </table>
                        {% endfor %}
                        <table class="table table-bordered table-condensed">
                            <tr class="monthly_total">
                                <th colspan="4">Overall total for the period:</th>
                                <td class="hours">{{ summary.total|floatformat:2 }}</td>
                            </tr>
                        </table>
                    {% else %}
                        <p>No entries exist for this time sheet.</p>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
{% endblock %}
