{% extends "layout.html" %}
{% block body %}

<div class="row">
    <p><a class="btn btn-primary" href="javascript:history.back()" role="button">&laquo; Back to home page</a></p>
</div>

<div class="row">
    <h1>CalSpeed Super Series {{ selected_season }}: Team Championship</h1>
</div>

{% if not season_data.is_approved() %}
    <div class="alert alert-warning" role="alert"><b>The results are not official yet!</b> The last round is pending approval. This message will disappear once the results get approved by the race director.</div>
{% endif %}

<table class="table">
    <thead>
    <tr>
        <th>Pos</th>
        <th>+/-</th>
        <th class="col-md-2">Team name</th>
        <th class="col-md-2">Driver name</th>
        {% for round in range(season_data.get_total_rounds()) %}
        <th>{{ round+1 }}</th>
        {% endfor %}
        <th>Drops (Dr)</th>
        <th>Total (Dr)</th>
        <th>Drops (Team)</th>
        <th>Total (Team)</th>
        <th>Diff (Team)</th>
    </tr>
    </thead>

    <tbody>
    {% for team_item in table.get_table() %}

        {# This is a workaround for Jinja bug. If this is not present, then points diff will not work #}
        {% if first_place_points %}{% endif %}

        {% set rowloop = loop %}
        {% for driver_row in team_item.driver_rows %}
            <tr {% if rowloop.index0 % 2 == 0 %}class="row-striped-odd"{% endif %}>
            {% if loop.index0 == 0 %}
                <td rowspan="{{ team_item.driver_rows|length }}">{{ team_item.position }}</td>
                <td rowspan="{{ team_item.driver_rows|length }}">{% if team_item.position_change > 0 %}{{ team_item.position_change }} <span class="glyphicon glyphicon-chevron-down"></span>{% elif team_item.position_change < 0 %}{{ team_item.position_change|abs }} <span class="glyphicon glyphicon-chevron-up"></span>{% else %}{{ team_item.position_change }}{% endif %}</td>
                <td rowspan="{{ team_item.driver_rows|length }}">{{ team_item.team }}</td>
            {% endif %}

            <td>{{ driver_row.driver }}</td>
            {% for item in driver_row.rounds %}
            {% if item.dropped %}
            <td class="dropped_points">{{ item.points }}</td>
            {% elif item.non_droppable %}
            <td class="non_droppable">{{ item.points }}</td>
            {% elif item.exists %}
            <td>{{ item.points }}</td>
            {% else %}
            <td>-</td>
            {% endif %}
            {% endfor %}
            <td>{{ driver_row.dropped_points }}</td>
            <td>{{ driver_row.total_points }}</td>

            {% if loop.index0 == 0 %}
            <td rowspan="{{ team_item.driver_rows|length }}">{{ team_item.dropped_points }}</td>
            <td rowspan="{{ team_item.driver_rows|length }}">{{ team_item.total_points }}</td>
            <td rowspan="{{ team_item.driver_rows|length }}">{% if first_place_points %}{{ team_item.total_points - first_place_points }}{% endif %}</td>
            {% endif %}
        </tr>
        {% endfor %}

        {% if not first_place_points %}{% set first_place_points = team_item.total_points %}{% endif %}
    {% endfor %}
    </tbody>

</table>

{% endblock %}

