{% extends "base.html" %}

{% load django_tables2 i18n %}

{% block content %}
{% trans "any" as any %}
    <div class="card bg-body shadow">
        <div class="card-header text-center">
            <h4>
                {{ passage }}
                {% if user.registration.is_admin or user.registration in passage.pool.tournament.organizers.all %}
                    <button class="btn btn-sm btn-secondary"
                            data-bs-toggle="modal" data-bs-target="#updatePassageModal">
                        <i class="fas fa-edit"></i>
                        {% trans "Update" %}
                    </button>
                {% endif %}
            </h4>
        </div>
        <div class="card-body">
            <dl class="row">
                <dt class="col-sm-3">{% trans "Pool:" %}</dt>
                <dd class="col-sm-9"><a href="{{ passage.pool.get_absolute_url }}">{{ passage.pool }}</a></dd>

                <dt class="col-sm-3">{% trans "Position:" %}</dt>
                <dd class="col-sm-9">{{ passage.position }}</dd>

                <dt class="col-sm-3">{% trans "Reporter:" %}</dt>
                <dd class="col-sm-9"><a href="{{ passage.reporter.get_absolute_url }}">{{ passage.reporter.team }}</a></dd>

                <dt class="col-sm-3">{% trans "Opponent:" %}</dt>
                <dd class="col-sm-9"><a href="{{ passage.opponent.get_absolute_url }}">{{ passage.opponent.team }}</a></dd>

                <dt class="col-sm-3">{% trans "Reviewer:" %}</dt>
                <dd class="col-sm-9"><a href="{{ passage.reviewer.get_absolute_url }}">{{ passage.reviewer.team }}</a></dd>

                {% if passage.observer %}
                    <dt class="col-sm-3">{% trans "Observer:" %}</dt>
                    <dd class="col-sm-9"><a href="{{ passage.observer.get_absolute_url }}">{{ passage.observer.team }}</a></dd>
                {% endif %}

                <dt class="col-sm-3">{% trans "Reported solution:" %}</dt>
                <dd class="col-sm-9"><a href="{{ passage.reported_solution.file.url }}">{{ passage.reported_solution }}</a></dd>

                <dt class="col-sm-3">{% trans "Reporter penalties count:" %}</dt>
                <dd class="col-sm-9">{{ passage.reporter_penalties }}</dd>

                <dt class="col-sm-3">{% trans "Syntheses:" %}</dt>
                <dd class="col-sm-9">
                    {% for review in passage.written_reviews.all %}
                        <a href="{{ review.file.url }}">{{ review }}{% if not forloop.last %}, {% endif %}</a>
                    {% empty %}
                        {% trans "No review was uploaded yet." %}
                    {% endfor %}
                </dd>
            </dl>
        </div>
        {% if notes is not None %}
            <div class="card-footer text-center">
                {% if my_note is not None %}
                    <button class="btn btn-info" data-bs-toggle="modal" data-bs-target="#{{ my_note.modal_name }}Modal">{% trans "Update notes" %}</button>
                {% endif %}
            </div>
        {% elif user.registration.participates %}
            <div class="card-footer text-center">
                <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#uploadWrittenReviewModal">{% trans "Upload review" %}</button>
            </div>
        {% endif %}
    </div>

    {% if notes %}
        <hr>

        <h2>{% trans "Notes detail" %}</h2>

        {% render_table notes %}

        <div class="card bg-body shadow">
            <div class="card-body">
                <dl class="row">
                    <dt class="col-sm-8">
                        {% trans "Average points for the reporter writing" %}
                        ({{ passage.reporter.team.trigram }}) :
                    </dt>
                    <dd class="col-sm-4">
                        {{ passage.average_reporter_writing|floatformat }}/{% if TFJM_APP == "TFJM" %}20{% else %}10{% endif %}
                    </dd>

                    <dt class="col-sm-8">
                        {% trans "Average points for the reporter oral" %}
                        ({{ passage.reporter.team.trigram }}) :
                    </dt>
                    <dd class="col-sm-4">
                        {{ passage.average_reporter_oral|floatformat }}/{% if TFJM_APP == "TFJM" %}20{% else %}10{% endif %}
                    </dd>

                    <dt class="col-sm-8">
                        {% trans "Average points for the opponent writing" %}
                        ({{ passage.opponent.team.trigram }}) :
                    </dt>
                    <dd class="col-sm-4">{{ passage.average_opponent_writing|floatformat }}/10</dd>

                    <dt class="col-sm-8">
                        {% trans "Average points for the opponent oral" %}
                        ({{ passage.opponent.team.trigram }}) :
                    </dt>
                    <dd class="col-sm-4">{{ passage.average_opponent_oral|floatformat }}/10</dd>

                    <dt class="col-sm-8">
                        {% trans "Average points for the reviewer writing" %}
                        ({{ passage.reviewer.team.trigram }}) :
                    </dt>
                    <dd class="col-sm-4">{{ passage.average_reviewer_writing|floatformat }}/10</dd>

                    <dt class="col-sm-8">
                        {% trans "Average points for the reviewer oral" %}
                        ({{ passage.reviewer.team.trigram }}) :
                    </dt>
                    <dd class="col-sm-4">{{ passage.average_reviewer_oral|floatformat }}/10</dd>

                    {% if passage.observer %}
                        <dt class="col-sm-8">
                            {% trans "Average points for the observer writing" %}
                            ({{ passage.observer.team.trigram }}) :
                        </dt>
                        <dd class="col-sm-4">{{ passage.average_observer_writing|floatformat }}/10</dd>

                        <dt class="col-sm-8">
                            {% trans "Average points for the observer oral" %}
                            ({{ passage.observer.team.trigram }}) :
                        </dt>
                        <dd class="col-sm-4">{{ passage.average_observer_oral|floatformat }}/10</dd>
                    {% endif %}
                </dl>

                <hr>

                <dl class="row">
                    <dt class="col-sm-8">
                        {% trans "Reporter points" %}
                        ({{ passage.reporter.team.trigram }}) :
                    </dt>
                    <dd class="col-sm-4">
                        {{ passage.average_reporter|floatformat }}/{% if TFJM_APP == "TFJM" %}52{% else %}50{% endif %}
                    </dd>

                    <dt class="col-sm-8">
                        {% trans "Opponent points" %}
                        ({{ passage.opponent.team.trigram }}) :
                    </dt>
                    <dd class="col-sm-4">
                        {{ passage.average_opponent|floatformat }}/{% if TFJM_APP == "TFJM" %}29{% else %}{% if passage.observer %}26{% else %}29{% endif %}{% endif %}
                    </dd>

                    <dt class="col-sm-8">
                        {% trans "reviewer points" %}
                        ({{ passage.reviewer.team.trigram }}) :
                    </dt>
                    <dd class="col-sm-4">{{ passage.average_reviewer|floatformat }}/{% if TFJM_APP == "TFJM" %}19{% else %}{% if passage.observer %}18{% else %}21{% endif %}{% endif %}</dd>

                    {% if passage.observer %}
                        <dt class="col-sm-8">
                            {% trans "observer points" %}
                            ({{ passage.observer.team.trigram }}) :
                        </dt>

                        <dd class="col-sm-4">{{ passage.average_observer|floatformat }}/6</dd>
                    {% endif %}
                </dl>
            </div>
        </div>
    {% endif %}

    {% if notes is not None %}
        {% trans "Update passage" as modal_title %}
        {% trans "Update" as modal_button %}
        {% url "participation:passage_update" pk=passage.pk as modal_action %}
        {% include "base_modal.html" with modal_id="updatePassage" %}

        {% for note in notes.data %}
            {% trans "Update notes" as modal_title %}
            {% trans "Update" as modal_button %}
            {% url "participation:update_notes" pk=note.pk as modal_action %}
            {% include "base_modal.html" with modal_id=note.modal_name %}
        {% endfor %}
    {% elif user.registration.participates %}
        {% trans "Upload review" as modal_title %}
        {% trans "Upload" as modal_button %}
        {% url "participation:upload_written_review" pk=passage.pk as modal_action %}
        {% include "base_modal.html" with modal_id="uploadWrittenReview" modal_enctype="multipart/form-data" %}
    {% endif %}
{% endblock %}

{% block extrajavascript %}
    <script>
        document.addEventListener('DOMContentLoaded', () => {
            {% if notes is not None %}
                initModal("updatePassage", "{% url "participation:passage_update" pk=passage.pk %}")

              {% for note in notes.data %}
                initModal("{{ note.modal_name }}", "{% url "participation:update_notes" pk=note.pk %}")
            {% endfor %}
            {% elif user.registration.participates %}
                initModal("uploadWrittenReview", "{% url "participation:upload_written_review" pk=passage.pk %}")
            {% endif %}
        })
    </script>
{% endblock %}
