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

{% block title %}
    {{ timesheet_user.get_name_or_username }}
    - Time Sheet for {{ from_date|date:'M Y' }}
{% endblock title %}

{% block bodyid %}user-timesheet{% endblock bodyid %}

{% block breadcrumb %}{% endblock breadcrumb %}

{% block extrajs %}
    <script>
        var basePath = "{% url 'view_user_timesheet' timesheet_user.pk %}",
            tabIds = ['overview', 'all-entries', 'daily-summary'],
            defaultTab = 'overview';
    </script>
    <script charset="utf-8" src="{{ STATIC_URL }}timepiece/js/permanent_tabs.js"></script>
{% endblock extrajs %}

{% block content %}
    <h2>{{ timesheet_user.get_name_or_username }} - Time Sheet for {{ from_date|date:'M Y' }}</h2>

    <div class="row-fluid">
        <div class="span12" id="header">
            <div id="options-buttons">
                {% if show_verify %}
                    {% url 'change_user_timesheet' timesheet_user.id 'verify' as base_url %}
                    {% create_dict from_date=from_date|date:'Y-m-d' as params %}
                    <a class="btn btn-primary" href="{{ base_url|add_parameters:params }}">Verify All Entries</a>
                {% endif %}

                {% if show_approve %}
                    {% url 'change_user_timesheet' timesheet_user.id 'approve' as base_url %}
                    {% create_dict from_date=from_date|date:'Y-m-d' as params %}
                    <a class="btn btn-primary" href="{{ base_url|add_parameters:params }}">Approve All Entries</a>
                {% endif %}

                {% if perms.entries.view_payroll_summary %}
                    {% url 'reject_user_timesheet' timesheet_user.pk as base_url %}
                    {% create_dict month=from_date|date:'n' year=from_date|date:'Y' as params %}
                    <a class="btn btn-primary" href="{{ base_url|add_parameters:params }}">Reject All Entries</a>
                {% endif %}

                {% if perms.auth.view_user %} {# FIXME: This permission doesn't exist #}
                    <a class="btn btn-primary" href="{% url 'view_user' timesheet_user.pk %}">View/Edit User</a>
                {% endif %}
            </div>

            {# We have to duplicate the form so the hidden fields are not in the same form #}
            <form class="form-inline visible-desktop" method="get" action="" accept-charset="utf-8">
                {{ year_month_form|as_bootstrap:"inline" }}
                <input type="submit" name="yearmonth" class="btn" value="Update" />
            </form>
            <form class="hidden-desktop" method="get" action="" accept-charset="utf-8">
                {{ year_month_form|as_bootstrap }}
                <input type="submit" name="yearmonth" class="btn" value="Update" />
            </form>
        </div>
    </div>

    <div class="row-fluid">
        <div class="span12">
            <ul class="nav nav-tabs">
                <li{% if active_tab == 'overview' %} class="active"{% endif %}>
                    {% url "view_user_timesheet" user.pk "overview" as link %}
                    <a class="tab-link" to="overview" href="{{ link|add_parameters:request.GET }}">Overview</a>
                </li>
                <li{% if active_tab == 'all-entries' %} class="active"{% endif %}>
                    {% url "view_user_timesheet" user.pk "all-entries" as link %}
                    <a class="tab-link" to="all-entries" href="{{ link|add_parameters:request.GET }}">All Entries</a>
                </li>
                <li{% if active_tab == 'daily-summary' %} class="active"{% endif %}>
                    {% url "view_user_timesheet" user.pk "daily-summary" as link %}
                    <a class="tab-link" to="daily-summary" href="{{ link|add_parameters:request.GET }}">Daily Summary</a>
                </li>
            </ul>
            <div class="tab-content">
                <div class="tab-pane{% if active_tab == 'overview' %} active{% endif %}" id="overview">
                    <div class="row-fluid">
                        {% 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|humanize_hours:"{hours:02d}:{minutes:02d}" }}</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|humanize_hours:"{hours:02d}:{minutes:02d}" }}</td>
                                        </tr>
                                        <tr>
                                           <td>Non-billable</td><td class="hours">{{summary.non_billable|humanize_hours:"{hours:02d}:{minutes:02d}" }}</td>
                                        </tr>
                                        <tr>
                                            <td>Invoiced</td><td class="hours">{{summary.invoiced|humanize_hours:"{hours:02d}:{minutes:02d}" }}</td>
                                        </tr>
                                        <tr>
                                            <td>Uninvoiced</td><td class="hours">{{summary.uninvoiced|humanize_hours:"{hours:02d}:{minutes:02d}" }}</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{% if active_tab == 'all-entries' %} active{% endif %}" id="all-entries">

                    {% if entries %}
                        {% url 'view_user_timesheet' timesheet_user.pk 'all-entries' as next_url %}
                        <table class="table table-bordered table-condensed table-hover">
                            <thead>
                                <tr>
                                    <th>Date</th>
                                    <th class="project">Project</th>
                                    <th class="activity">Activity</th>
                                    <th>Location</th>
                                    <th>Status</th>
                                    <th class="time">Time In</th>
                                    <th class="time">Time Out</th>
                                    <th class="time">Breaks</th>
                                    <th class="time">Hours</th>
                                    <th class="comments">Comments</th>
                                    <th class="aux"></th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for entry in entries %}
                                    {% ifchanged entry.start_time|date:"W" %}
                                        <tr class="emphasized-row">
                                            <th colspan="11">Week of {{ entry.start_time|week_start|date:'M j, Y' }} </th>
                                        </tr>
                                    {% endifchanged %}
                                    <tr>
                                        {% ifchanged entry.start_time|date %}
                                            <td>{{ entry.start_time|date:"m/d/Y (D)" }}</td>
                                        {% else %}
                                            <td></td>
                                        {% endifchanged %}
                                        <td class="project">{{ entry.project__name }}</td>
                                        <td class="activity">{{ entry.activity__name }}</td>
                                        <td>{{ entry.location__name }}</td>
                                        <td>{{ entry.status|title }}</td>
                                        <td class="time break">{{ entry.start_time|time }}</td>
                                        <td class="time break">{{ entry.end_time|time }}</td>
                                        <td class="hours time">{{ entry.seconds_paused|seconds_to_hours|humanize_hours:"{hours:02d}:{minutes:02d}" }}</td>
                                        <td class="hours time">{{ entry.hours|humanize_hours:"{hours:02d}:{minutes:02d}" }}</td>
                                        <td class="comments" title="{{entry.comments}}">{{ entry.comments|truncatewords:12|urlizetrunc:25|safe }}</td>
                                        <td class="aux">
                                            {% if entry.status == "unverified" or entry.status != 'invoiced' and perms.entries.view_payroll_summary %}
                                                <a href="{% url 'edit_entry' entry.id %}?next={{ next_url|add_parameters:request.GET|urlencode }}">Edit</a>
                                            {% endif %}
                                            {% if perms.entries.view_payroll_summary and entry.status != "unverified" and entry.status != "invoiced" %}
                                                | <a href="{% url 'reject_entry' entry.id %}?next={{ next_url|add_parameters:request.GET|urlencode }}">Reject</a>
                                            {% endif %}
                                        </td>
                                    </tr>
                                {% endfor %}
                                <tr class="emphasized-row">
                                    <th colspan=8 style="text-align: right;">Total over all entries:</th>
                                    <th class="hours">{{ summary.total|humanize_hours:"{hours:02d}:{minutes:02d}" }}</th>
                                    <th colspan=2></th>
                                </tr>
                            </tbody>
                        </table>
                    {% else %}
                        <p>No entries exist for this time sheet.</p>
                    {% endif %}
                </div>
                <div class="tab-pane{% if active_tab == 'daily-summary' %} active{% endif %}" id="daily-summary">
                    {% if grouped_totals %}
                        <table class="table table-bordered table-condensed">
                            {% for week, week_totals, days in grouped_totals %}
                                <tr class="ledger_header">
                                    <th colspan="2">Week of {{ week|date:'M j, Y'}} </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|add_timezone < 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|humanize_hours:"{hours:02d}:{minutes:02d}" }}</td>
                                            <td class="hours">{{ totals.non_billable|humanize_hours:"{hours:02d}:{minutes:02d}" }}</td>
                                            <td class="hours">{{ totals.total|humanize_hours:"{hours:02d}:{minutes:02d}" }}</td>
                                        </tr>
                                    {% endfor %}
                                    <tr class="daily_total {% if day|add_timezone < from_date %}before_from_date{% endif %}">
                                        <td></td>
                                        <td class="hours"><strong>{{day|date:"D"}}:</strong></td>
                                        <td class="hours">{{ projects.0.billable|humanize_hours:"{hours:02d}:{minutes:02d}" }}</td>
                                        <td class="hours">{{ projects.0.non_billable|humanize_hours:"{hours:02d}:{minutes:02d}" }}</td>
                                        <td class="hours">{{ projects.0.total|humanize_hours:"{hours:02d}:{minutes:02d}" }}</td>
                                    <tr/>
                                {% endfor %}
                                <tr class="weekly_total">
                                    <th colspan="2">Totals for this week:</th>
                                    <td class="hours">{{ week_totals.billable|humanize_hours:"{hours:02d}:{minutes:02d}" }}</td>
                                    <td class="hours">{{ week_totals.non_billable|humanize_hours:"{hours:02d}:{minutes:02d}" }}</td>
                                    <td class="hours">{{ week_totals.total|humanize_hours:"{hours:02d}:{minutes:02d}" }}</td>
                                </tr>
                                {% if not forloop.last %}
                                    <tr><td colspan="5">&nbsp;</td></tr>
                                {% endif %}
                            {% endfor %}
                            <thead>
                                <tr class="totals">
                                    <th></th>
                                    <th></th>
                                    <th class="billable"></th>
                                    <th class="non-billable"></th>
                                    <th class="total"></th>
                                </tr>
                            </thead>
                            <tr class="totals">
                                <td colspan="4"><strong>Overall total for the period:</strong></td>
                                <td class="hours">{{ summary.total|humanize_hours:"{hours:02d}:{minutes:02d}" }}</td>
                            </tr>
                        </table>
                    {% else %}
                        <p>No entries exist for this time sheet.</p>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
{% endblock content %}
