{% extends "elections/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">
    {% if election.start_date > current_time %}

    {# Lien pour la modification #}
    <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>
    {% elif election.end_date < current_time %}
    {% if not election.tallied %}

    {# Lien pour le dépouillement #}
    <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>
    {% else %}

    {# Lien pour la publication des résultats #}
    <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">
  <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">
      <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">
      <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">
    {% if election.start_date > current_time %}
    <div class="tags has-addons">
      <a class="tag is-danger" title="{% trans "Supprimer" %}">
        <span class="icon">
          <i class="fas fa-times"></i>
        </span>
      </a>
      <a class="tag is-info" title="{% trans "Modifier" %}">
        <span class="icon">
          <i class="fas fa-edit"></i>
        </span>
      </a>
    </div>&nbsp;&nbsp;
    {% 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>&nbsp;&nbsp;
    {% endif %}
    {{ o.text }}
  </div>
  {% endfor %}

  {# Rajout d'une option #}
  {% if election.start_date > current_time %}
  <form action="" 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">
  <div class="column is-two-thirds">
    <form action="" method="POST">
      {% csrf_token %}

      <div class="field has-addons">
        <div class="control has-icons-left is-expanded">
          <input class="input" 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">{% trans "Valider" %}</button>
        </div>
      </div>
    </form>
  </div>
</div>
{% endif %}

{% endblock %}
