{% extends "base.html" %}

{% load i18n %}
{% load crispy_forms_filters %}

{% block content %}
    <div class="card bg-body shadow">
        <div class="card-header text-center">
            <h4>{{ team.name }}</h4>
        </div>
    <div class="card-body">
        <dl class="row">
            <dt class="col-sm-6 text-sm-end">{% trans "Name:" %}</dt>
            <dd class="col-sm-6">{{ team.name }}</dd>

            <dt class="col-sm-6 text-sm-end">{% trans "Trigram:" %}</dt>
            <dd class="col-sm-6">{{ team.trigram }}</dd>

            <dt class="col-sm-6 text-sm-end">{% trans "Email:" %}</dt>
            <dd class="col-sm-6"><a href="mailto:{{ team.email }}">{{ team.email }}</a></dd>

            <dt class="col-sm-6 text-sm-end">{% trans "Access code:" %}</dt>
            <dd class="col-sm-6">{{ team.access_code }}</dd>

            <dt class="col-sm-6 text-sm-end">{% trans "Coaches:" %}</dt>
            <dd class="col-sm-6">
                {% for coach in team.coaches.all %}
                    <a href="{% url "registration:user_detail" pk=coach.user.pk %}">{{ coach }}</a>{% if not forloop.last %},{% endif %}
                {% empty %}
                    {% trans "any" %}
                {% endfor %}
            </dd>

            <dt class="col-sm-6 text-sm-end">{% trans "Participants:" %}</dt>
            <dd class="col-sm-6">
                {% for student in team.students.all %}
                    <a href="{% url "registration:user_detail" pk=student.user.pk %}">{{ student }}</a>{% if not forloop.last %},{% endif %}
                {% empty %}
                    {% trans "any" %}
                {% endfor %}
            </dd>

            <dt class="col-sm-6 text-sm-end">{% trans "Tournament:" %}</dt>
            <dd class="col-sm-6">
                {% if team.participation.tournament %}
                    <a href="{% url "participation:tournament_detail" pk=team.participation.tournament.pk %}">{{ team.participation.tournament }}</a>
                {% else %}
                    {% trans "any" %}
                {% endif %}
            </dd>

            <dt class="col-sm-6 text-sm-end">{% trans "Photo authorizations:" %}</dt>
            <dd class="col-sm-6">
                {% for participant in team.participants.all %}
                    {% if participant.photo_authorization %}
                        <a href="{{ participant.photo_authorization.url }}">{{ participant }}</a>{% if not forloop.last %},{% endif %}
                    {% else %}
                        {{ participant }} ({% trans "Not uploaded yet" %}){% if not forloop.last %},{% endif %}
                    {% endif %}
                {% endfor %}
            </dd>

            {% if team.participation.final %}
                <dt class="col-sm-6 text-sm-end">{% trans "Photo authorizations (final):" %}</dt>
                <dd class="col-sm-6">
                    {% for participant in team.participants.all %}
                        {% if participant.photo_authorization_final %}
                            <a href="{{ participant.photo_authorization_final.url }}">{{ participant }}</a>{% if not forloop.last %},{% endif %}
                        {% else %}
                            {{ participant }} ({% trans "Not uploaded yet" %}){% if not forloop.last %},{% endif %}
                        {% endif %}
                    {% endfor %}
                </dd>
            {% endif %}

            {% if not team.participation.tournament.remote  %}
                <dt class="col-sm-6 text-sm-end">{% trans "Health sheets:" %}</dt>
                <dd class="col-sm-6">
                    {% for student in team.students.all %}
                        {% if student.under_18 %}
                            {% if student.health_sheet %}
                                <a href="{{ student.health_sheet.url }}">{{ student }}</a>{% if not forloop.last %},{% endif %}
                            {% else %}
                                {{ student }} ({% trans "Not uploaded yet" %}){% if not forloop.last %},{% endif %}
                            {% endif %}
                        {% endif %}
                    {% endfor %}
                </dd>

                <dt class="col-sm-6 text-sm-end">{% trans "Vaccine sheets:" %}</dt>
                <dd class="col-sm-6">
                    {% for student in team.students.all %}
                        {% if student.under_18 %}
                            {% if student.vaccine_sheet %}
                                <a href="{{ student.vaccine_sheet.url }}">{{ student }}</a>{% if not forloop.last %},{% endif %}
                            {% else %}
                                {{ student }} ({% trans "Not uploaded yet" %}){% if not forloop.last %},{% endif %}
                            {% endif %}
                        {% endif %}
                    {% endfor %}
                </dd>

                <dt class="col-sm-6 text-sm-end">{% trans "Parental authorizations:" %}</dt>
                <dd class="col-sm-6">
                    {% for student in team.students.all %}
                        {% if student.under_18 %}
                            {% if student.parental_authorization %}
                                <a href="{{ student.parental_authorization.url }}">{{ student }}</a>{% if not forloop.last %},{% endif %}
                            {% else %}
                                {{ student }} ({% trans "Not uploaded yet" %}){% if not forloop.last %},{% endif %}
                            {% endif %}
                        {% endif %}
                    {% endfor %}
                </dd>

                {% if team.participation.final %}
                    <dt class="col-sm-6 text-sm-end">{% trans "Parental authorizations (final):" %}</dt>
                    <dd class="col-sm-6">
                        {% for student in team.students.all %}
                            {% if student.under_18_final %}
                                {% if student.parental_authorization_final %}
                                    <a href="{{ student.parental_authorization_final.url }}">{{ student }}</a>{% if not forloop.last %},{% endif %}
                                {% else %}
                                    {{ student }} ({% trans "Not uploaded yet" %}){% if not forloop.last %},{% endif %}
                                {% endif %}
                            {% endif %}
                        {% endfor %}
                    </dd>
                {% endif %}
            {% endif %}

            <dt class="col-sm-6 text-sm-end">{% trans "Motivation letter:" %}</dt>
            <dd class="col-sm-6">
                {% if team.motivation_letter %}
                    <a href="{{ team.motivation_letter.url }}">{% trans "Download" %}</a>
                {% else %}
                    <em>{% trans "Not uploaded yet" %}</em>
                {% endif %}
                {% if user.registration.team == team and not user.registration.team.participation.valid or user.registration.is_admin %}
                    <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#uploadMotivationLetterModal">{% trans "Replace" %}</button>
                {% endif %}
            </dd>

        {% if user.registration.is_volunteer %}
            {% if user.registration in self.team.participation.tournament.organizers or user.registration.is_admin %}
                <div class="text-center">
                    <a class="btn btn-info" href="{% url "participation:team_authorizations" team_id=team.id %}">
                        <i class="fas fa-file-archive"></i> {% trans "Download all submitted authorizations" %}
                    </a>
                </div>
            {% endif %}
        {% endif %}

        {% if team.participation.valid %}
            <hr class="my-3">
            {% for student in team.students.all %}
                {% for payment in student.payments.all %}
                    <dt class="col-sm-6 text-sm-end">
                        {% trans "Payment of" %} {{ student }}
                        {% if payment.grouped %}({% trans "grouped" %}){% endif %}
                        {% if payment.final %} ({% trans "final" %}){% endif %} :
                    </dt>
                    <dd class="col-sm-6">
                        Valide : {{ payment.valid|yesno }}
                        {% if payment.valid is False or user.registration.is_volunteer %}
                            {% if user.registration in payment.registrations.all or user.registration.is_coach or user.registration.is_volunteer %}
                                <a href="{% url "registration:update_payment" pk=payment.pk %}" class="btn btn-secondary">
                                    <i class="fas fa-money-bill-wave"></i> {% trans "Update payment" %}
                                </a>
                            {% endif %}
                        {% endif %}
                    </dd>
                {% endfor %}
            {% endfor %}
        {% endif %}
        </dl>
    </div>
    <div class="card-footer text-center">
        <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#updateTeamModal">{% trans "Update" %}</button>
        {% if not team.participation.valid %}
            <button class="btn btn-danger" data-bs-toggle="modal" data-bs-target="#leaveTeamModal">{% trans "Leave" %}</button>
        {% endif %}
    </div>
    </div>

    <hr>

    {% if team.participation.valid %}
        <div class="text-center">
            <a class="btn btn-info" href="{% url "participation:participation_detail" pk=team.participation.pk %}">
                <i class="fas fa-file-pdf"></i> {% trans "Access to team participation" %}
            </a>
        </div>
    {% elif team.participation.valid == None %} {# Team did not ask for validation #}
        {% if user.registration.participates %}
            {% if can_validate %}
                <div class="alert alert-info">
                    {% trans "Your team has at least 4 members and a coach and all authorizations were given: the team can be validated." %}
                    <div class="text-center">
                        <form method="post">
                            {% csrf_token %}
                            {{ request_validation_form|crispy }}
                            <button class="btn btn-success" name="request-validation">{% trans "Submit my team to validation" %}</button>
                        </form>
                    </div>
                </div>
            {% else %}
                <div class="alert alert-warning">
                    {% trans "Your team must be composed of 4 members and a coach and each member must upload their authorizations and confirm its email address." %}
                </div>
            {% endif %}
        {% else %}
            <div class="alert alert-warning">
            {% trans "This team didn't ask for validation yet." %}
            </div>
        {% endif %}
    {% else %}  {# Team is waiting for validation #}
        {% if user.registration.participates %}
            <div class="alert alert-warning">
                {% trans "Your validation is pending." %}
            </div>
        {% else %}
            <div class="alert alert-info">
                {% trans "The team requested to be validated. You may now control the authorizations and confirm that they can participate." %}
            </div>
             <form method="post">
                {% csrf_token %}
                {{ validation_form|crispy }}
                <div class="input-group btn-group">
                    <button class="btn btn-success" name="validate" type="submit">{% trans "Validate" %}</button>
                    <button class="btn btn-danger" name="invalidate" type="submit">{% trans "Invalidate" %}</button>
                </div>
             </form>
        {% endif %}
    {% endif %}

    {% trans "Upload motivation letter" as modal_title %}
    {% trans "Upload" as modal_button %}
    {% url "participation:upload_team_motivation_letter" pk=team.pk as modal_action %}
    {% include "base_modal.html" with modal_id="uploadMotivationLetter" modal_enctype="multipart/form-data" %}

    {% trans "Update team" as modal_title %}
    {% trans "Update" as modal_button %}
    {% url "participation:update_team" pk=team.pk as modal_action %}
    {% include "base_modal.html" with modal_id="updateTeam" %}

    {% trans "Leave team" as modal_title %}
    {% trans "Leave" as modal_button %}
    {% url "participation:team_leave" as modal_action %}
    {% include "base_modal.html" with modal_id="leaveTeam" modal_button_type="danger" %}
{% endblock %}

{% block extrajavascript %}
    <script>
        document.addEventListener('DOMContentLoaded', () => {
            initModal("uploadMotivationLetter", "{% url "participation:upload_team_motivation_letter" pk=team.pk %}")
            initModal("updateTeam", "{% url "participation:update_team" pk=team.pk %}")
            initModal("leaveTeam", "{% url "participation:team_leave" %}")
        })
    </script>
{% endblock %}
