{% extends "base.html" %}

{% load getconfig i18n django_tables2 %}

{% block content %}
    <div class="card bg-light shadow">
        <div class="card-header text-center">
            <h4>{{ tournament.name }}</h4>
        </div>
        <div class="card-body">
            <dl class="row">
                <dt class="col-xl-6 text-right">{% trans 'organizers'|capfirst %}</dt>
                <dd class="col-xl-6">{{ tournament.organizers.all|join:", " }}</dd>

                <dt class="col-xl-6 text-right">{% trans 'size'|capfirst %}</dt>
                <dd class="col-xl-6">{{ tournament.max_teams }}</dd>

                <dt class="col-xl-6 text-right">{% trans 'place'|capfirst %}</dt>
                <dd class="col-xl-6">{{ tournament.place }}</dd>

                <dt class="col-xl-6 text-right">{% trans 'price'|capfirst %}</dt>
                <dd class="col-xl-6">{% if tournament.price %}{{ tournament.price }} €{% else %}{% trans "Free" %}{% endif %}</dd>

                <dt class="col-xl-6 text-right">{% trans 'remote'|capfirst %}</dt>
                <dd class="col-xl-6">{{ tournament.remote|yesno }}</dd>

                <dt class="col-xl-6 text-right">{% trans 'dates'|capfirst %}</dt>
                <dd class="col-xl-6">{% trans "From" %} {{ tournament.date_start }} {% trans "to" %} {{ tournament.date_end }}</dd>

                <dt class="col-xl-6 text-right">{% trans 'date of registration closing'|capfirst %}</dt>
                <dd class="col-xl-6">{{ tournament.inscription_limit }}</dd>

                <dt class="col-xl-6 text-right">{% trans 'date of maximal solution submission'|capfirst %}</dt>
                <dd class="col-xl-6">{{ tournament.solution_limit }}</dd>

                <dt class="col-xl-6 text-right">{% trans 'date of the random draw'|capfirst %}</dt>
                <dd class="col-xl-6">{{ tournament.solutions_draw }}</dd>

                <dt class="col-xl-6 text-right">{% trans 'date of maximal syntheses submission for the first round'|capfirst %}</dt>
                <dd class="col-xl-6">{{ tournament.syntheses_first_phase_limit }}</dd>

                <dt class="col-xl-6 text-right">{% trans 'date when solutions of round 2 are available'|capfirst %}</dt>
                <dd class="col-xl-6">{{ tournament.solutions_available_second_phase }}</dd>

                <dt class="col-xl-6 text-right">{% trans 'date of maximal syntheses submission for the second round'|capfirst %}</dt>
                <dd class="col-xl-6">{{ tournament.syntheses_second_phase_limit }}</dd>

                <dt class="col-xl-6 text-right">{% trans 'description'|capfirst %}</dt>
                <dd class="col-xl-6">{{ tournament.description }}</dd>

                <dt class="col-xl-6 text-right">{% trans 'To contact organizers' %}</dt>
                <dd class="col-xl-6"><a href="mailto:{{ tournament.organizers_email }}">{{ tournament.organizers_email }}</a></dd>

                <dt class="col-xl-6 text-right">{% trans 'To contact juries' %}</dt>
                <dd class="col-xl-6"><a href="mailto:{{ tournament.jurys_email }}">{{ tournament.jurys_email }}</a></dd>

                <dt class="col-xl-6 text-right">{% trans 'To contact valid teams' %}</dt>
                <dd class="col-xl-6"><a href="mailto:{{ tournament.teams_email }}">{{ tournament.teams_email }}</a></dd>
            </dl>
        </div>

        {% if user.registration.is_admin or user.registration in tournament.organizers.all %}
            <div class="card-footer text-center">
                <a href="{% url "participation:tournament_update" pk=tournament.pk %}"><button class="btn btn-secondary">{% trans "Edit tournament" %}</button></a>
            </div>
        {% endif %}
    </div>

    <hr>

    <h3>{% trans "Teams" %}</h3>
    <div id="teams_table">
        {% render_table teams %}
    </div>

    {% if pools.data %}
        <hr>

        <h3>{% trans "Pools" %}</h3>
        <div id="pools_table">
            {% render_table pools %}
        </div>
    {% endif %}

    {% if user.registration.is_admin %}
        <button class="btn btn-block btn-success" data-toggle="modal" data-target="#addPoolModal">{% trans "Add new pool" %}</button>
    {% endif %}

    {% if notes %}
        <hr>

        <div class="card bg-light shadow">
            <div class="card-header text-center">
                <h5>{% trans "Ranking" %}</h5>
            </div>
            <div class="card-body">
                <ul>
                    {% for participation, note in notes %}
                        <li><strong>{{ participation.team }} :</strong> {{ note }}</li>
                    {% endfor %}
                </ul>
            </div>
        </div>
    {% endif %}

    {% if user.registration.is_admin %}
        {% trans "Add pool" as modal_title %}
        {% trans "Add" as modal_button %}
        {% url "participation:pool_create" as modal_action %}
        {% include "base_modal.html" with modal_id="addPool" %}
    {% endif %}
{% endblock %}

{% block extrajavascript %}
    <script>
        $(document).ready(function () {
            {% if user.registration.is_admin %}
                $('button[data-target="#addPoolModal"]').click(function() {
                    let modalBody = $("#addPoolModal div.modal-body");
                    if (!modalBody.html().trim())
                        modalBody.load("{% url "participation:pool_create" %} #form-content")
                });
            {% endif %}
        });
    </script>
{% endblock %}
