{% extends "dashboard_app/_content_with_sidebar.html" %}
{% load i18n %}
{% load humanize %}

{% block extrahead %}
{{ block.super }}
<style type="text/css">
dt { font-weight: bold }
dd pre { margin: 0;}
</style>
{% endblock %}

{% block content %}
<h2>Result: {{ test_result.test_case|default:"<i>unknown test case</i>" }}</h2>
<dl>
  <dt>{% trans "Outcome" %}</dt>
  <dd>
    <img src="{{ STATIC_URL }}dashboard_app/images/icon-{{ test_result.result_code }}.png"
         alt="{{ test_result.result_code }}" width="16" height="16" border="0"/>
    {{ test_result.result_code }}
  </dd>
  <dt>{% trans "Measurement" %}</dt>
  <dd>
  {% if test_result.measurement != None %}
    {{ test_result.measurement }} {{ test_result.test_case.units }}
  {% else %}
    <i>{% trans "no measurement taken" %}</i>
  {% endif %}
  </dd>
  <dt>{% trans "Log file location" %}</dt>
  <dd>
    {% if test_result.filename %}
        {% if test_result.related_attachment_available and test_result.related_attachment.is_viewable %}
        {% with test_result.related_attachment as attachment %}
        <a href="{{ attachment.get_view_url }}">{{ test_result.filename }}</a> line <a href="{{ attachment.get_view_url }}#L{{test_result.lineno}}">{{ test_result.lineno }}</a>
        {% endwith %}
        {% else %}
            {{ test_result.filename }} line {{ test_result.lineno }}
        {% endif %}
    {% else %}
        <i>{% trans "information not provided" %}</i>
    {% endif %}
  </dd>
  <dt>{% trans "Message from the log file" %}</dt>
  <dd>
  {% if test_result.message %}
    <pre>{{ test_result.message }}</pre>
  {% else %}
    <i>{% trans "information not provided" %}</i>
  {% endif %}
  </dd>
  <dt>{% trans "Test started on" %}</dt>
  <dd>
  {% if test_result.timestamp %}
    {{ test_result.timestamp|naturalday }}
    {{ test_result.timestamp|time }}
  {% else %}
    <i>{% trans "information not provided" %}</i>
  {% endif %}
  </dd>
  <dt>{% trans "Test duration" %}</dt>
  <dd>
  {% if test_result.duration %}
    {# TODO need a filter for displaying this sensibly. Currently there are some rounding errors #}
    {{ test_result.duration }}
  {% else %}
    <i>{% trans "information not provided" %}</i>
  {% endif %}
  </dd>
</dl>

<h3 id="attachments">Attachments</h3>

{% include "dashboard_app/_attachments.html" with attachments=test_result.attachments %}

{% if test_result.test_run.get_results.count > 1 %}
<h3>Other results from the same test run</h3>
<select id="other_results">
  {% regroup test_result.test_run.get_results by test_case as test_result_group_list %}
  {% for test_result_group in test_result_group_list %}
  {% if test_result_group.list|length > 1 %}<optgroup label="Results for test case {{ test_result_group.grouper }}">{% endif %}
    {% for other_test_result in test_result_group.list %}
    <option value="{{ other_test_result.get_absolute_url }}"
    {% if other_test_result.pk == test_result.pk %}disabled selected{% endif %}
    >
      Result #{{ other_test_result.relative_index }}: {{ other_test_result.get_result_display }}
      {% if test_result_group.list|length == 1 %} from test case {{ test_result_group.grouper }}{% endif %}
      {% if other_test_result.measurement != None %} ({{ other_test_result.measurement }}){% endif %}
    </option>
    {% endfor %}
  {% if test_result_group.list|length > 1 %}</optgroup>{% endif %}
  {% endfor %}
</select>
<script type="text/javascript">
  $("#other_results").change(function (event) {
    location.href=$(this).val();
  });
</script>
{% endif %}

{% endblock %}

{% block sidebar %}
<h3>Metadata</h3>
<dl>
  <dt>ID</dt>
  <dd>
    <small><span style="white-space:nowrap">{{ test_result.test_run.analyzer_assigned_uuid }}/{{ test_result.relative_index }}</span> (<a href="{{ test_result.get_permalink }}">{% trans "permalink" %}</a>)</small>
  </dd>
  <dt>Test Case</dt>
  <dd>
    {% if test_result.test_case %}
    <b>{{ test_result.test_case }}</b>
    {% else %}
    <i>{% trans "unknown test case" %}</i>
    {% endif %}
    {% trans "from test" %} <b><a href="{{ test_result.test_run.test.get_absolute_url }}">{{ test_result.test_run.test }}</a></b>
  </dd>
</dl>
<h3>Result Attributes</h3>
{% with test_result.attributes.values as attrs %}
<ul class="attributes">
  {% for item in attrs|dictsort:"name" %}
  <li>{{ item.name }}&nbsp;=&nbsp;{{ item.value }}</li>
  {% empty %}
  <i>none</i>
  {% endfor %}
</ul>
{% endwith %}
<h3>Run Attributes</h3>
{% with test_result.test_run.attributes.values as attrs %}
<ul class="attributes">
  {% for item in attrs|dictsort:"name" %}
  <li>{{ item.name }}&nbsp;=&nbsp;{{ item.value }}</li>
  {% empty %}
  <i>none</i>
  {% endfor %}
</ul>
{% endwith %}
{% endblock %}
