<!DOCTYPE html>
<html lang="en">
<head lang="en">
    <meta charset="UTF-8">
    <style>
        .tg {
            border-collapse: collapse;
            border-spacing: 0;
        }

        .tg td {
            border-color: black;
            border-style: solid;
            border-width: 1px;
            font-family: Arial, sans-serif;
            font-size: 14px;
            overflow: hidden;
            padding: 10px 5px;
            word-break: normal;
        }

        .tg th {
            border-color: black;
            border-style: solid;
            border-width: 1px;
            font-family: Arial, sans-serif;
            font-size: 14px;
            font-weight: normal;
            overflow: hidden;
            padding: 10px 5px;
            word-break: normal;
        }

        .tg .tg-c3ow {
            border-color: inherit;
            text-align: center;
            vertical-align: top
        }

        .tg .tg-0pky {
            border-color: inherit;
            text-align: left;
            vertical-align: top
        }

        .tg .tg-0lax {
            text-align: left;
            vertical-align: top
        }

        .tg .tg-color {
            border-color: inherit;
            text-align: left;
            vertical-align: top;
            background: lightcoral
        }

        #with_margin {
            margin-bottom: 20px;
        }

    </style>
    <title> {{ title }} </title>
</head>
<body>

<h2>Summary of non-compliance:</h2>
<table class="tg">
    <thead>
    <tr>
        <th class="tg-0pky" colspan="2">Modality</th>
        <th class="tg-0pky" colspan="2"># non-compliant (%)</th>
        <th class="tg-0pky" colspan="2">Non-compliant subjects</th>
        <th class="tg-0pky" colspan="2">Reasons</th>
        <th class="tg-0pky" colspan="2"># compliant (%)</th>
        <th class="tg-0pky" colspan="2"># subjects</th>
        <th class="tg-0pky" colspan="2"># Echo-Times</th>


        {#        <th class="tg-0pky" colspan="2">errors</th>#}
    </tr>
    </thead>
    <tbody>
    {% for modality in dataset.modalities %}
        <tr>
            <td class="tg-0pky" colspan="2">{{ modality.name }}</td>
            {% set percent_non_compliant = 100 * modality.non_compliant_subject_names|length|float / modality.subjects|length|float %}
            {% set percent_compliant = 100 * modality.compliant_subject_names|length|float / modality.subjects|length|float %}

            <td class="tg-0pky" colspan="2">
                {{ modality.non_compliant_subject_names|length }}
                ({{ percent_non_compliant|round(2, 'floor') }} %)
            </td>

            <td class="tg-0pky" colspan="2">
                {% for name in modality.non_compliant_subject_names %}
                    {{ name }},
                {% endfor %}
            </td>
            <td class="tg-0pky" colspan="2">
                {% for parameter in modality.reasons_non_compliance() %}
                    {{ parameter }},
                {% endfor %}
            </td>
            <td class="tg-0pky" colspan="2">
                {{ modality.compliant_subject_names|length }}
                ( {{ percent_compliant|round(2, 'floor') }} %)
            </td>
            <td class="tg-0pky" colspan="2">
                {{ modality.subjects|length }}
            </td>
            <td class="tg-0pky" colspan="2">
                {{ modality.get_echo_times() | length }}
            </td>
            {#            <td class="tg-0pky" colspan="2">#}
            {#                {{ mode.error_children|length }}#}
            {#            </td>#}
        </tr>
    {% endfor %}
    </tbody>
</table>


{% for modality in dataset.modalities %}

    {% if not modality.compliant %}
        <h2>Modality : {{ modality.name }}</h2>
        {% for echo_time in modality.get_echo_times() %}
            {% set te = echo_time %}
            {% set reference = modality.get_reference(echo_time) %}
            {% if modality.is_multi_echo() %}
                <h4>Multi-Echo Modality : Reference {{ loop.index }}</h4>
            {% else %}
                <h4>Reference</h4>
            {% endif %}

            <table class="tg" id="with_margin">
                <thead>
                <tr>
                    {% for key in reference.keys() %}
                        <th class="tg-0pky" colspan="2">{{ key }}</th>
                    {% endfor %}
                </tr>

                </thead>
                <tbody>
                <tr>
                    {% for key in reference.keys() %}
                        <td class="tg-0pky"
                            colspan="2">{{ reference[key] }}</td>
                    {% endfor %}
                </tr>
                </tbody>
            </table>
            {% if modality.reasons_non_compliance(te).any() %}
                <table class="tg" id="with_margin">
                <thead>
                <tr>
                    <th class="tg-c3ow"
                        colspan="4">Parameter
                    </th>
                    <th class="tg-c3ow"
                        colspan="4">Ref. Value
                    </th>
                    <th class="tg-c3ow"
                        colspan="4">Found
                    </th>
                    <th class="tg-c3ow"
                        colspan="4">Subject_Session
                    </th>
                </tr>
                </thead>
                <tbody>
            {% endif %}
        {% for parameter in modality.reasons_non_compliance(te) %}
            {# {% set reasons = modality.query_reason(parameter, te) %}#}
            <tr>
                <td class="tg-0pky" colspan="4">{{ parameter }}</td>
                <td class="tg-0pky" colspan="4">
                    {% for value in modality.query_reason(parameter, te, 'ref_value') %}
                        {{ value }},
                    {% endfor %}
                </td>
                <td class="tg-0pky" colspan="4">
                    {% for value in modality.query_reason(parameter, te, 'new_value') %}
                        {{ value }},
                    {% endfor %}
                </td>
                <td class="tg-0pky" colspan="2">
                    {% for subject in modality.query_reason
                            (parameter, te, 'subjects') %}
                        {{ subject }},
                    {% endfor %}
                </td>
            </tr>
        {% endfor %}
        </tbody>
        </table>

            {##}
            {#            <table class="tg" id="with_margin">#}
            {#                {% for subject in modality.subjects %}#}
            {#                    {% for session in subject.sessions %}#}
            {#                        {% for run in session.runs %}#}
            {#                            {% if run.delta and (run.echo_time == echo_time) %}#}
            {#                                <thead>#}
            {#                                <tr>#}
            {#                                    <th class="tg-c3ow" colspan="4">Subject</th>#}
            {#                                    <th class="tg-c3ow"#}
            {#                                        colspan="4">Session#}
            {#                                    </th>#}
            {#                                    {% for item in run.delta %}#}
            {#                                        <th class="" tg=-0pky"#}
            {#                                            colspan="2">{{ "Ref. : "+item[1] }}</th>#}
            {#                                        <th class="" tg=-0pky"#}
            {#                                            colspan="2">{{ "Found: "+item[1] }}</th>#}
            {#                                    {% endfor %}#}
            {#                                </tr>#}
            {#                                </thead>#}
            {#                                <tbody>#}
            {#                                <tr>#}
            {#                                    <td class="tg-0pky"#}
            {#                                        colspan="4">{{ subject.name }}</td>#}
            {#                                    <td class="tg-0pky"#}
            {#                                        colspan="4">{{ session.name }}</td>#}
            {#                                    {% for item in run.delta %}#}
            {#                                        <td class="tg-0pky"#}
            {#                                            colspan="2">{{ item[2][1] }}</td>#}
            {#                                        <td class="tg-0pky"#}
            {#                                            colspan="2">{{ item[2][0] }}</td>#}
            {#                                    {% endfor %}#}
            {#                                </tr>#}
            {#                                </tbody>#}
            {#                            {% endif %}#}
            {#                        {% endfor %}#}
            {##}
            {#                    {% endfor %}#}
            {##}
            {#                {% endfor %}#}
            {#            </table>#}
        {% endfor %}
    {% endif %}
{% endfor %}


{% for modality in dataset.modalities %}

    {% if modality.compliant %}
        <h2>Modality : {{ modality.name }}</h2>
        {% for echo_time in modality.get_echo_times() %}
            {% set reference = modality.get_reference(echo_time) %}
            {% if modality.is_multi_echo() %}
                <h4>Mult-Echo Modality : Reference {{ loop.index }}</h4>
            {% else %}
                <h4>Reference</h4>
            {% endif %}
            <table class="tg" id="with_margin">
                <thead>
                <tr>
                    {% for key in reference.keys() %}
                        <th class="tg-0pky"
                            colspan="2">{{ key }}</th>
                    {% endfor %}
                </tr>

                </thead>
                <tbody>
                <tr>
                    {% for key in reference.keys() %}
                        <td class="tg-0pky"
                            colspan="2">{{ reference[key] }}</td>
                    {% endfor %}
                </tr>
                </tbody>
            </table>
        {% endfor %}
    {% endif %}
{% endfor %}


</body>
</html>
