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


{% block content %}

<div class="level">
  <div class="level-left is-flex-shrink-1">
    {# 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 is-flex-shrink-1 is-flex-grow-1">
    {# Liste des votant·e·s #}
    <div class="level-item">
      <a class="button is-primary is-light is-outlined" href="{% url 'election.voters' election.pk %}">
        <span class="icon is-small">
          <i class="fas fa-list"></i>
        </span>
        <span>{% trans "Votant·e·s" %}</span>
      </a>
    </div>

    {% if election.start_date < current_time %}
    {# Statut de l'élection #}
    <div class="level-item">
      <span class="tag is-medium is-outlined is-light is-primary">
        {% if election.end_date < current_time %}
        {% trans "Élection terminée" %}
        {% else %}
        {% trans "Élection en cours" %}
        {% endif %}
      </span>
    </div>
    {% endif %}

    {# Lien vers la page d'administration #}
    {% if election.created_by == user %}
    <div class="level-item">
      <a class="tag is-medium is-primary is-light is-outlined has-tooltip-primary" href="{% url 'election.admin' election.pk %}" data-tooltip="{% trans "Administrer" %}">
        <span class="icon">
          <i class="fas fa-cog"></i>
        </span>
      </a>
    </div>
    {% endif %}
  </div>
</div>
<hr>

{# Confirmation de vote #}
{% if has_voted %}
<div class="block tag is-medium is-outlined is-success is-light">
  <span class="icon">
    <i class="fas fa-check"></i>
  </span>
  <span>{% trans "Votre vote a bien été enregistré." %}</span>
</div>
{% endif %}

{# Indications de connexion #}
{% if election.start_date < current_time and election.end_date > current_time %}
{% if can_vote %}
<div class="columns is-centered tile is-ancestor">
  <div class="column is-one-third tile is-parent">
    <a class="tile is-child notification is-primary" href="{% url 'election.vote' election.questions.first.pk %}">
      <div class="subtitle has-text-centered">
        <span class="icon has-text-white">
          <i class="fas fa-vote-yea"></i>
        </span>
        <span class="ml-3">{% trans "Voter" %}</span>
      </div>
    </a>
  </div>
</div>
{% else %}

<div class="message is-warning">
  <div class="message-body">
    {% if election.restricted %}
    {% trans "Pour voter lors de cette élection, vous devez vous connecter à l'aide des identifiants reçus par mail." %}
    {% else %}
    {% trans "Pour voter lors de cette élection, vous devez vous connecter à l'aide du CAS élève, d'autres restrictions peuvent s'appliquer et votre vote pourra être supprimé si vous n'avez pas le droit de vote." %}
    {% endif %}
  </div>
</div>

<div class="columns is-centered">
  <div class="column is-half">
    <div class="tile is-ancestor">
      <div class="tile is-parent">
        {% if election.restricted %}
        <a class="tile is-child notification is-primary" href="{% url 'auth.election' election.pk %}?next={% url 'election.view' election.pk %}">
          <div class="subtitle has-text-centered mb-2">
            <span class="icon has-text-white">
              <i class="fas fa-unlock"></i>
            </span>
            <span class="ml-3">{% trans "Connexion par identifiants" %}</span>
          </div>
        </a>
        {% else %}
        <a class="tile is-child notification is-primary" href="{% url 'authens:login.cas' %}">
          <div class="subtitle has-text-centered mb-2">
            <span class="icon has-text-white">
              <i class="fas fa-school"></i>
            </span>
            <span class="ml-3">{% trans "Connexion via CAS" %}</span>
          </div>
        </a>
        {% endif %}
      </div>
    </div>
  </div>
</div>
{% endif %}
{% endif %}

{# 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 class="ml-2">{{ q.text }}</span>

    {% if q in cast_questions %}
    <span class="tag is-outlined is-light is-success is-pulled-right">{% trans "A voté" %}</span>
    {% endif %}
  </div>

  {# Liste des options possibles #}
  {% for o in q.options.all %}
  <div class="panel-block">
    {% if election.tallied and election.results_public %}
    <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 and election.results_public %}
  {{ q.get_results_data }}
  {% endif %}
</div>
{% endfor %}

{% endblock %}
