{% extends "base.html" %}
{% load i18n %}


{% block content %}

<div class="level">
  <div class="level-left">
    {# Titre de l'élection #}
    <div class="level-item">
      <h1 class="title">{{ election.name }}</h1>
    </div>

    {# Dates d'ouverture de l'élection #}
    <div class="level-item">
      <span class="tag is-medium is-primary">{{ election.start_date|date:"d/m/Y H:i" }}</span>
      <span class="icon">
        <i class="fas fa-long-arrow-alt-right"></i>
      </span>
      <span class="tag is-medium is-primary">{{ election.end_date|date:"d/m/Y H:i" }}</span>
    </div>
  </div>

  <div class="level-right">

    {# Lien pour la modification et l'upload des votant·e·s #}
    {% if election.start_date > current_time %}
    <div class="level-item">
      <a class="button is-light is-outlined is-primary" href="{% url 'election.update' election.pk %}">
        <span class="icon">
          <i class="fas fa-edit"></i>
        </span>
        <span>{% trans "Modifier" %}</span>
      </a>
    </div>

    {% if election.restricted %}
    <div class="level-item">
      <a class="button is-light is-outlined is-primary" href="{% url 'election.upload-voters' election.pk %}">
        <span class="icon">
          <i class="fas fa-file-import"></i>
        </span>
        <span>{% trans "Importer une liste de votant·e·s" %}</span>
      </a>
    </div>
    {% endif %}

    {% elif election.end_date < current_time %}

    {# Lien pour le dépouillement #}
    {% if not election.tallied %}
    <div class="level-item">
      <a class="button is-light is-outlined is-primary" href="{% url 'election.tally' election.pk %}">
        <span class="icon">
          <i class="fas fa-poll-h"></i>
        </span>
        <span>{% trans "Dépouiller" %}</span>
      </a>
    </div>

    {# Lien pour la publication des résultats #}
    {% else %}
    <div class="level-item">
      <a class="button is-outlined is-primary" href="{% url 'election.publish' election.pk %}">
        <span class="icon">
          <i class="fas fa-edit"></i>
        </span>
        {% if not election.results_public %}
        <span>{% trans "Publier" %}</span>
        {% else %}
        <span>{% trans "Dépublier" %}</span>
        {% endif %}
      </a>
    </div>

    {# Lien pour l'archivage #}
    <div class="level-item">
      <a class="button is-light is-outlined is-primary" href="{% url 'election.archive' election.pk %}">
        <span class="icon">
          <i class="fas fa-edit"></i>
        </span>
        <span>{% trans "Archiver" %}</span>
      </a>
    </div>
    {% endif %}
    {% endif %}
  </div>
</div>
<hr>

{# Description de l'élection #}
<div class="message is-primary">
  <div class="message-body">{{ election.description|linebreaksbr }}</div>
</div>

{# Liste des questions #}
{% for q in election.questions.all %}
<div class="panel" id="q_{{ q.pk }}">
  <div class="panel-heading is-size-6">
    <span>{{ q.text }}</span>
    {% if election.start_date > current_time %}
    <a class="tag is-small is-outlined is-light is-danger" href="{% url 'election.del-question' q.pk %}">
      <span class="icon">
        <i class="fas fa-times"></i>
      </span>
      <span>{% trans "Supprimer" %}</span>
    </a>
    <a class="tag is-small is-outlined is-light is-info" href="{% url 'election.mod-question' q.pk %}">
      <span class="icon">
        <i class="fas fa-edit"></i>
      </span>
      <span>{% trans "Modifier" %}</span>
    </a>
    {% endif %}
  </div>

  {# Liste des options possibles #}
  {% for o in q.options.all %}
  <div class="panel-block" id="o_{{ o.pk }}">
    {% if election.start_date > current_time %}
    <span class="tags has-addons mb-0">
      <a class="tag is-danger mb-0" title="{% trans "Supprimer" %}" href="{% url 'election.del-option' o.pk %}">
        <span class="icon">
          <i class="fas fa-times"></i>
        </span>
      </a>
      <a class="tag is-info mb-0" title="{% trans "Modifier" %}" href="{% url 'election.mod-option' o.pk %}">
        <span class="icon">
          <i class="fas fa-edit"></i>
        </span>
      </a>
    </span>
    {% elif election.tallied %}
    <span class="tag {% if o.nb_votes == q.max_votes %}is-success{% else %}is-primary{% endif %}">
      <span class="icon">
        <i class="fas fa-vote-yea"></i>
      </span>
      <span>{{ o.nb_votes }}</span>
    </span>
    {% endif %}
    <span class="ml-2">{{ o.text }}</span>
  </div>
  {% endfor %}

  {# Rajout d'une option #}
  {% if election.start_date > current_time %}
  <form action="{% url 'election.add-option' q.pk %}" method="post">
    <div class="panel-block field has-addons">
      {% csrf_token %}

      <div class="control has-icons-left is-expanded">
        <input class="input is-success" type="text" name="text" id="id_text" placeholder="{% trans "Rajouter une option" %}">
        <span class="icon is-left">
          <i class="fas fa-plus"></i>
        </span>
      </div>
      <div class="control">
        <button class="button is-success">{% trans "Valider" %}</button>
      </div>
    </div>
  </form>
  {% endif %}
</div>
{% endfor %}

{# Rajout d'une question #}
{% if election.start_date > current_time %}
<div class="columns is-centered" id="q_add">
  <div class="column is-two-thirds">
    <form action="{% url 'election.add-question' election.pk %}" method="post">
      {% csrf_token %}

      <div class="field has-addons">
        <div class="control has-icons-left is-expanded">
          <input class="input is-primary" type="text" name="text" id="id_text" placeholder="{% trans "Rajouter une question" %}">
          <span class="icon is-left">
            <i class="fas fa-question"></i>
          </span>
        </div>
        <div class="control">
          <button class="button is-primary is-outlined">{% trans "Valider" %}</button>
        </div>
      </div>
    </form>
  </div>
</div>
{% endif %}

{% endblock %}
