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


{% block content %}

<div class="level is-block-tablet is-block-desktop is-flex-fullhd">
  <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>

  {% if election.start_date > current_time or election.end_date < current_time %}
  <div class="level-right">
    <div class="level-item">
      <div class="dropdown is-right">
        <div class="dropdown-trigger">
          <button class="button" aria-haspopup="true" aria-controls="dropdown-menu">
            <span class="icon is-small">
              <i class="fas fa-cog" aria-hidden="true"></i>
            </span>
            <span>{% trans "Actions" %}</span>
          </button>
        </div>

        <div class="dropdown-menu" id="dropdown-menu" role="menu">
          <div class="dropdown-content">
            {% if election.start_date > current_time %}
            {# Modification de l'élection #}
            <a class="dropdown-item" href="{% url 'election.update' election.pk %}">
              <span class="icon">
                <i class="fas fa-edit"></i>
              </span>
              <span>{% trans "Modifier" %}</span>
            </a>

            {# Gestion des votant·e·s #}
            {% if election.restricted %}
            <a class="dropdown-item" href="{% url 'election.upload-voters' election.pk %}">
              <span class="icon">
                <i class="fas fa-file-import"></i>
              </span>
              <span>{% trans "Gestion de la liste de votant·e·s" %}</span>
            </a>
            {% endif %}

            {% elif election.end_date < current_time %}

            {% if not election.tallied %}
            {# Liste des votants #}
            <a class="dropdown-item" href="{% url 'election.voters' election.pk %}">
              <span class="icon">
                <i class="fas fa-list"></i>
              </span>
              <span>{% trans "Liste des votant·e·s" %}</span>
            </a>

            {# Dépouillement #}
            <a class="dropdown-item" href="{% url 'election.tally' election.pk %}">
              <span class="icon">
                <i class="fas fa-poll-h"></i>
              </span>
              <span>{% trans "Dépouiller" %}</span>
            </a>

            {% else %}

            {# Publication des résultats #}
            <a class="dropdown-item" 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>

            {# Archivage #}
            {% if not election.archived %}
            <a class="dropdown-item" href="{% url 'election.archive' election.pk %}">
              <span class="icon">
                <i class="fas fa-archive"></i>
              </span>
              <span>{% trans "Archiver" %}</span>
            </a>
            {% endif %}
            {% endif %}

            {% endif %}

          </div>
        </div>
      </div>
    </div>
  </div>
  {% endif %}
</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-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-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 %}
    <span class="tag is-outlined is-primary is-light is-pulled-right">{{ q.get_type_display }}</span>
  </div>

  {# Liste des options possibles #}
  {% for o in q.options.all %}
  <div class="panel-block">
    {% if election.tallied %}
    <span class="tag {% if o.winner %}is-success{% else %}is-primary{% endif %}">
      {% if q.vote_type == "select" %}
      <span class="icon">
        <i class="fas fa-vote-yea"></i>
      </span>
      <span>{{ o.nb_votes }}</span>

      {% elif q.vote_type == "rank" %}

      <span class="icon">
        <i class="fas fa-layer-group"></i>
      </span>
      <span>{{ forloop.counter }}</span>
      {% endif %}
    </span>
    {% endif %}

    <span class="ml-2">{{ o.text }}</span>
  </div>
  {% endfor %}

  {# Affiche plus d'informations sur le résultat #}
  {% if election.tallied %}
  {{ q.get_results_data }}
  {% endif %}

  {# 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">
          <span class="select is-primary">
            <select name="type" id="id_type">
              {% for val, choice in question_types %}
              <option value="{{ val }}">{{ choice }}</option>
              {% endfor %}
            </select>
          </span>
        </div>

        <div class="control">
          <button class="button is-primary is-outlined">{% trans "Valider" %}</button>
        </div>
      </div>
    </form>
  </div>
</div>
{% endif %}

{% endblock %}
