{% extends 'print_base.html' %}
{% load wnsq_tags %}
{% load i18n %}
{% block header %}
    <title>{% trans 'measurement report'|capfirst %} {{ object.sample }}</title>
{% endblock header %}


{% block content %}
    <body class="metro">
    <button class="noprint" onclick="window.print();">{% trans 'print'|capfirst %}</button>

    <h4>{% trans 'measurement report'|capfirst %}</h4>
    <hr/>


    <!-- generic sample information -->
    <table class="sample_report_table">
        <thead>
        <tr>
            <th colspan="2">{% trans 'sample'|capfirst %}</th>
        </tr>
        </thead>

        <tbody>

        <!-- sample name or id -->
        <tr>
            <td>{% trans 'name'|capfirst %}</td>
            <td>{{ object.sample }}</td>
        </tr>

        <!-- bag id -->
        <tr>
            <td>{% trans 'bag'|capfirst %}</td>
            <td>{{ object.sample.contained_in }}</td>
        </tr>

        <!-- optional project -->
        <tr>
            <td>{% trans 'project'|capfirst %}</td>
            <td>{{ object.sample.project|value_or_nothing }}</td>
        </tr>

        </tbody>
    </table>

    <!-- optional part for reference samples -->
    {% if is_reference %}
        <table class="sample_report_table">
            <thead>
            <tr>
                <th colspan="2">{% trans 'reference gas'|capfirst %}</th>
            </tr>
            </thead>
            <tbody>
            <tr>
                <td>{% trans 'substance'|capfirst %} ({% trans 'concentration'|capfirst %})</td>
                <td>
                    {{ object.sample.cylinder.get_substance_display }}&NonBreakingSpace;({{ object.sample.cylinder.concentration|floatformat:2 }}&NonBreakingSpace;ppm)
                </td>
            </tr>
            <tr>
                <td>{% trans 'cylinder'|capfirst %} ({% trans 'number'|capfirst %})</td>
                <td>{{ object.sample.cylinder.name }} ({{ object.sample.cylinder.number }})</td>
            </tr>
            <tr>
                <td>{% trans 'expires' %}</td>
                <td>{{ object.sample.cylinder.expires_at|date:"SHORT_DATE_FORMAT" }}</td>
            </tr>
            </tbody>
        </table>
    {% endif %}

    <!-- sampling information -->
    <table class="sample_report_table">

        <thead>
        <tr>
            <th colspan="2">{% trans 'Sampling' %}</th>
        </tr>
        </thead>

        <tbody>
        <tr>
            <td>{% trans 'date'|capfirst %}</td>
            <td>
                {{ object.sample.sampled_at|date:"SHORT_DATE_FORMAT" }}
                -
                {{ object.sample.sampled_at|time }}
            </td>
        </tr>
        <tr>
            <td>{% trans 'dilution'|capfirst %}</td>
            <td>
                {% if object.sample.total_dilution == 1 %}
                    {% nothing %}
                {% else %}
                    {{ object.sample.total_dilution|floatformat:2 }}
                {% endif %}

            </td>
        </tr>
        </tbody>

    </table>

    <!-- measurement base information -->
    <table class="sample_report_table">

        <thead>
        <tr>
            <th colspan="2">{% trans 'measurement'|capfirst %}</th>
        </tr>
        </thead>

        <tbody>
        <tr>

            <td>{% trans 'location'|capfirst %}</td>
            <td>{{ object.location }} ({{ object.temperature|floatformat:1 }} C&deg;)</td>
        </tr>
        <tr>
            <td>{% trans 'date'|capfirst %} / {% trans 'Duration' %}</td>
            <td>
                {{ object.started_at|date:"SHORT_DATE_FORMAT" }}
                <br/>
                {{ object.started_at|time }} {% nothing %} {{ object.finished_at|time }}
            </td>
        </tr>

        <tr>
            <td>{% trans 'olfactometer'|capfirst %}</td>
            <td>{{ object.olfactometer }}</td>
        </tr>

        <tr>
            <td>{% trans 'calibration'|capfirst %}/{% trans 'expires' %}</td>
            <td>{{ object.olfactometer.calibration.valid_to|date:"SHORT_DATE_FORMAT" }}</td>
        </tr>

        <tr>
            <td>{% trans 'method'|capfirst %}</td>
            <td>{{ object.get_used_method_display }}</td>
        </tr>

        <tr>
            <td>{% trans 'presentation time'|capfirst %}</td>
            <td>{{ object.presentation_time }}</td>
        </tr>

        </tbody>

    </table>

    {% if show_overview_table %}
        {% comment %}
        not show_overview_table is basically also saying that this is a quality measurement, which
        means the result table is pointless (since always empty)
        {% endcomment %}
        <table class="sample_report_table">
            <thead>
            <tr>
                <th colspan="2">{% trans 'result'|capfirst %}</th>
            </tr>
            </thead>
            <tbody>

            <!-- the actual result including any predilution, in parens if not compliant -->
            <tr>
                <td>{% trans 'measurement result'|capfirst %}</td>
                <td>
                    {% if object.analysis_result %}
                        {% if not object.compliant_result %}({% endif %}
                        <strong>{{ result_with_predil|floatformat:2 }}</strong>
                        {% if not object.compliant_result %}){% endif %}&NonBreakingSpace;
                        OU<sub>E</sub> &frasl; m<sup>3</sup><sup>*</sup>
                    {% else %}
                        {% nothing %}
                    {% endif %}

                </td>
            </tr>

            <!-- ite_pan (just the geometric mean without predilution taken into account -->
            <tr>
                <td><strong>Z<sub>ite,pan</sub></strong></td>
                <td>
                    {% if object.analysis_result %}
                        <strong>{{ object.analysis_result|floatformat:2 }}</strong>
                    {% else %}
                        {% nothing %}
                    {% endif %}
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <small>
                        <sup>*</sup>{% trans 'indication only applicable for measurements in accordance with the traceability requirements of the EN13725.' %}
                    </small>
                </td>
                {% if manually_excluded %}
                    <tr>
                        <td>
                            <small>{% trans 'Manually excluded panel member' %}</small>
                        </td>
                        <td>
                            <small>
                                {% for panel_member in manually_excluded %}
                                    {{ panel_member }}
                                    {% if not forloop.last %}
                                        ,
                                    {% endif %}
                                {% endfor %}
                            </small>
                        </td>
                    </tr>
                {% endif %}

            </tr>
            </tbody>

        </table>
    {% endif %}

    {% if object.comments %}
        <table class="sample_report_table">
            <thead>
            <tr>
                <th>{% trans 'comments'|capfirst %}</th>
            </tr>
            </thead>
        </table>
        <tbody>
        <tr>
            <td>
                {{ object.comments }}
            </td>
        </tr>
        </tbody>
    {% endif %}

    <hr/>
    {% if show_overview_table %}
        <div>
            {% for r in round_info %}

                <table class="round_table">
                    <thead>
                    <tr>
                        <th colspan="{% if has_blank_column %}4{% else %}3{% endif %}">
                            {% trans 'round'|capfirst %} {{ r.number }}
                            {% if not r.is_included %}
                                ({% trans 'discarded' %})
                            {% endif %}
                        </th>
                    </tr>
                    <tr>
                        <th>{% trans 'panelist'|capfirst %}</th>
                        <th>ITE</th>
                        <th>&Delta;Z</th>
                        {% if has_blank_column %}
                            <th>{% trans '0-Errors' %}</th>
                        {% endif %}
                    </tr>
                    </thead>

                    <tbody>
                    {% for p in r.results %}
                        <tr>
                            <td style="width: 25pt;">{{ p.panelist.shorthand }}</td>
                            <td style="width: 80pt; text-align: right;">
                                {% if p.ite and p.valid %}
                                    {{ p.ite|floatformat:2 }}
                                {% elif p.ite and not p.valid %}
                                    ({{ p.ite|floatformat:2 }})
                                {% else %}
                                    {% nothing %}
                                {% endif %}
                            </td>
                            <td style="width: 25pt; text-align: right;">
                                {% if p.dz %}
                                    {{ p.dz|floatformat:2 }}
                                {% else %}
                                    {% nothing %}
                                {% endif %}
                            </td>
                            {% if has_blank_column %}
                                <td style="width: 25pt; text-align: right;">{{ p.blank|value_or_nothing }}</td>
                            {% endif %}
                        </tr>
                    {% endfor %}
                    </tbody>

                </table>
            {% endfor %}
        </div>
    {% endif %}
    {% if include_matrices %}
        <div style="page-break-before: always;"></div>
        {% for r in matrices %}
            <!-- avoid page breaks inside a single matrix -->
            <div style="page-break-inside: avoid; margin-top: 20pt;">
                <table class="sample_report_table">
                    <thead>
                    <tr>
                        <th colspan="2">
                            {% trans 'round'|capfirst %} {{ forloop.counter }}
                        <span style="font-weight: normal;">
                            ({{ object.sample }} {{ object.sample.contained_in }}
                            {% if object.sample.project %}
                                , {% trans 'project'|capfirst %} {{ object.sample.project }})
                            {% else %}
                                )
                            {% endif %}
                        </span>
                        </th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr>
                        <td>{% trans 'Operator' %}</td>
                        <td>{{ r.round.operator }}</td>
                    </tr>
                    <tr>
                        <td>{% trans 'started' %} / {% trans 'finished' %}</td>
                        <td>{{ r.round.started_at }} / {{ r.round.finished_at }}</td>
                    </tr>
                    </tbody>
                </table>

                <table class="round_table">
                    <thead>
                    <tr>
                        <th>{% trans 'Step' %}</th>
                        {% for p in r.panel %}
                            <th>{{ p }}</th>
                        {% endfor %}
                    </tr>
                    </thead>

                    <tbody>

                    {% for step in r.reply_data %}
                        <tr>
                            {% for rd in step %}

                                {% if not forloop.first %}
                                    <td style="text-align: center;">
                                        {{ rd.0|value_or_nothing }}
                                        {% comment %}
                                    explicitly test for None to catch actual zeroes e.g. in int/hed
                                    {% endcomment %}
                                        {% if rd.1 != None %}
                                            {{ rd.1 }}
                                        {% endif %}
                                    </td>
                                {% else %}

                                    <td style="text-align: center;">
                                        <!-- dilution value -->
                                        {{ rd }}
                                    </td>

                                {% endif %}
                            {% endfor %}
                        </tr>
                    {% endfor %}

                    </tbody>

                </table>
            </div>
        {% endfor %}
    {% endif %}
    </body>
{% endblock content %}
