{% extends 'timepiece/contract/base.html' %}
{% load timepiece_tags %}

{% block title %}{{ contract }}{% endblock title %}
{% block bodyid %}contracts{% endblock bodyid %}

{% block crumbs %}
    {{ block.super }}
    <li><span class="divider">/</span> <a href="{% url 'view_contract' contract.pk %}">{{ contract }}</a></li>
{% endblock crumbs %}

{% block extrajs %}
    <script>
    $(document).ready(function(){
        $('[data-toggle="popover"]').popover();
    });
    </script>
{% endblock extrajs %}

{% block content %}
    <div class="row-fluid">
        <div class="span12">
            <h2>{{ contract }}</h2>
            <ul class="nav nav-pills">
                {% if perms.contracts.change_contract %}  {# FIXME: permission doesn't exist #}
                    <li><a href="{{ contract.get_admin_url }}">Edit</a></li>
                {% endif %}
            </ul>
        </div>
    </div>

    <div class="row-fluid">
        <div class="span4">
            <h3>Contract Info</h3>
            <table class="table table-bordered">
                <tr><th>Status</th><td>{{ contract.get_status_display }}</td></tr>
                <tr><th>Start Date</th><td>{{ contract.start_date|date:'M j, Y' }}</td></tr>
                {% if contract.end_date < today %}
                    <tr class="expired" title="This contract has expired.">
                {% elif contract.end_date < warning_date %}
                    <tr class="expiring-soon" title="This contract will expire in less than two weeks.">
                {% else %}
                    <tr>
                {% endif %}
                    <th>End Date</th>
                    <td{% if contract.end_date < today %} class="past"{% endif %}>
                        {{ contract.end_date|date:'M j, Y' }}
                        {% if contract.end_date >= today and contract.end_date < warning_date %}
                            <i class="icon-warning-sign"></i>
                        {% endif %}
                    </td>
                </tr>
                <tr><th>Contract Hours</th>
                    <td>
                        {{ contract.contracted_hours|floatformat:2 }}
                        {% if contract.pending_hours %}
                            <span class="pending_hours">(+{{ contract.pending_hours }})</span>
                        {%  endif %}</td>
                    </td>
                </tr>
                <tr><th>Hours Worked (billable)</th>
                    <td>
                        {{ contract.hours_worked|floatformat:2 }}
                        ({% widthratio contract.hours_worked contract.contracted_hours 100 %}%)
                    </td>
                </tr>
                {% if contract.pre_launch_hours_worked %}
                    <tr class="error"><th>Pre-Contract Hours Worked (Billable)</th>
                        <td>
                            {{ contract.pre_launch_hours_worked|floatformat:2 }}
                            ({% widthratio contract.pre_launch_hours_worked contract.contracted_hours 100 %}%)
                            <a class="btn btn-danger btn-mini" data-toggle="popover"
                               data-html="true"
                               title="Pre-contract Entries"
                               data-content="
                                   <table class='table table-bordered'>
                                      <tr>
                                        <td class='table span2'>Project</td>
                                        <td class='th-continued'>User</td>
                                        <td class='th-continued'>Start</td>
                                        <td class='th-continued'>End</td>
                                        <td class='th-continued'>Activity</td>
                                        <td class='th-continued'>Hours</td>
                                      </tr>
                                      {% for e in contract.pre_launch_entries %}
                                         <tr>
                                           <td>{{e.project}}</td>
                                           <td>{{e.user}}</td>
                                           <td>{{e.start_time}}</td>
                                           <td>{{e.end_time}}</td>
                                           <td>{{e.activity}}</td>
                                           <td>{{e.hours}}</td>
                                         </tr>
                                      {% endfor %}
                                   </table>
                               "
                               data-placement="bottom"
                               data-container="body">
                                   <i class="icon-white icon-comment"></i>
                           </a>
                        </td>
                    </tr>
                {% endif %}

                {% if contract.post_launch_hours_worked %}
                    <tr class="error"><th>Post-Contract Hours Worked</th>
                        <td>
                            {{ contract.post_launch_hours_worked|floatformat:2 }}
                            ({% widthratio contract.post_launch_hours_worked contract.contracted_hours 100 %}%)

                             <a class="btn btn-danger btn-mini" data-toggle="popover"
                               data-html="true"
                               title="Post-contract Entries"
                               data-content="
                                   <table class='table table-bordered'>
                                      <tr>
                                        <td class='table span2'>Project</td>
                                        <td class='th-continued'>User</td>
                                        <td class='th-continued'>Start</td>
                                        <td class='th-continued'>End</td>
                                        <td class='th-continued'>Activity</td>
                                        <td class='th-continued'>Hours</td>
                                      </tr>
                                      {% for e in contract.post_launch_entries %}
                                         <tr>
                                           <td>{{e.project}}</td>
                                           <td>{{e.user}}</td>
                                           <td>{{e.start_time}}</td>
                                           <td>{{e.end_time}}</td>
                                           <td>{{e.activity}}</td>
                                           <td>{{e.hours}}</td>
                                         </tr>
                                      {% endfor %}
                                   </table>
                               "
                               data-placement="bottom"
                               data-container="body">
                                   <i class="icon-white icon-comment"></i>
                           </a>
                        </td>
                    </tr>
                {% endif %}

                <tr><th>Hours Remaining</th><td>{{ contract.hours_remaining|floatformat:2 }}</td></tr>
                <tr><th>Type</th><td>{{  contract.get_type_display }}</td></tr>
            </table>
        </div>
        <div class="span7 offset1">
            <h3>Projects</h3>
            {% if contract.projects.exists %}
                <table class="table table-bordered table-condensed">
                    <thead>
                    <tr>
                        <th rowspan="2">Name</th>
                        <th colspan="2">Hours Worked</th>
                        <th rowspan="2">Time Sheet</th>
                        <th rowspan="2">Hours Report</th>
                    </tr>
                    <tr>
                        <th>Billable</th>
                        <th>Non-billable</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for project in contract.projects.all %}
                        {% project_hours_for_contract contract project 'billable' as project_hours %}
                        {% project_hours_for_contract contract project 'nonbillable' as nonbillable_hours %}
                        <tr>
                            <td><a href="{% url 'view_project' project.pk %}">{{ project.name }}</a></td>
                            <td class="hours">{{ project_hours|floatformat:'2' }}</td>
                            <td class="hours">{{ nonbillable_hours|floatformat:'2' }}</td>
                            <td><a href="{% url 'view_project_timesheet' project.id %}">Time Sheet</a></td>
                            <td><a href="{% project_report_url_for_contract contract project %}">Hours Report</a></td>
                        </tr>
                    {% endfor %}
                    </tbody>
                </table>
            {% else %}
                <p>This contract is not associated with any projects.</p>
            {% endif %}
        </div>
    </div>

    <div class="row-fluid">
        <div class="span12">
            <h3>Contract Hours</h3>
            <table class='table table-bordered'>
                <tr>
                    <th>Date Requested</th>
                    <th>Hours</th>
                    <th>Status</th>
                    <th>Date Approved</th>
                    <th>Notes</th>
                </tr>
                {% for hour in contract.contract_hours.all %}
                    <tr>
                        <td>{{ hour.date_requested }}</td>
                        <td class="hours">{{ hour.hours }}</td>
                        <td>{{ hour.get_status_display }}</td>
                        <td>{% if hour.date_approved %}{{ hour.date_approved }}{% endif %}</td>
                        <td>{{ hour.notes }}</td>
                    </tr>
                {% endfor %}
            </table>
        </div>
    </div>
{% endblock content %}
