{% extends "core/base.jinja" %}

{% block title %}
  {{ object.title }}
{% endblock %}

{% block additional_js %}
  <script type="module" src="{{ static('bundled/core/read-more-index.ts') }}"></script>
{% endblock %}

{% block additional_css %}
  <link rel="stylesheet" href="{{ static('election/css/election.scss') }}">
{% endblock %}

{% block content %}
  <h3 class="election__title">{{ election.title }}</h3>
  {% if not user.has_perm("core.view_user") %}
    <div class="alert alert-red">
      {% trans %}Candidate pictures won't display for privacy reasons.{% endtrans %}
    </div>
  {% endif %}
  <p class="election__description">{{ election.description }}</p>
  <hr>
  <section class="election_details">
    <p>
      {%- if election.is_vote_active %}
        {% trans %}Polls close {% endtrans %}
      {%- elif election.is_vote_finished %}
        {% trans %}Polls closed {% endtrans %}
      {%- else %}
        {% trans %}Polls will open {% endtrans %}
        <time datetime="{{ election.start_date }}">{{ election.start_date|localtime|date(DATETIME_FORMAT) }}</time>
        {% trans %}at{% endtrans %}
        <time>{{ election.start_date|localtime|time(DATETIME_FORMAT) }}</time>
        {% trans %}and will close {% endtrans %}
      {%- endif %}
      <time datetime="{{ election.end_date }}">{{ election.end_date|localtime|date(DATETIME_FORMAT) }}</time>
      {% trans %}at{% endtrans %}
      <time>{{ election.end_date|localtime|time(DATETIME_FORMAT) }}</time>
    </p>
    {%- if election.is_vote_finished and user.can_edit(election) %}
      <details class="accordion" name="apply-result">
        <summary>{% trans %}Apply election result{% endtrans %}</summary>
        <div
          class="accordion-content aria-busy-grow"
          hx-get="{{ url("election:apply_result", election_id=election.id) }}"
          hx-trigger="toggle from:closest details once"
        ></div>
      </details>
    {% endif %}
    {%- if user_has_voted %}
      <p class="election__elector-infos">
        {%- if election.is_vote_active %}
          <span>{% trans %}You already have submitted your vote.{% endtrans %}</span>
        {%- else %}
          <span>{% trans %}You have voted in this election.{% endtrans %}</span>
        {%- endif %}
      </p>
    {%- endif %}
  </section>
  <section class="election_vote">
    <form
      action="{{ url('election:vote', election.id) }}"
      method="post"
      class="election__vote-form"
      name="vote-form"
      id="vote-form"
    >
      {% csrf_token %}
      <table class="election_table">
        <thead class="lists">
          <tr>
            <th class="column" style="width: {{ 100 / (election_lists|length + 1) }}%">
              {% trans %}Blank vote{% endtrans %}
            </th>
            {%- for election_list in election_lists %}
              <th class="column" style="width: {{ 100 / (election_lists|length + 1) }}%">
                <span>{{ election_list.title }}</span>
                {% if user.can_edit(election_list) and election.is_vote_editable -%}
                  <a href="{{ url('election:delete_list', list_id=election_list.id) }}">
                    <i class="fa-regular fa-trash-can delete-action"></i>
                  </a>
                {% endif %}
              </th>
            {%- endfor %}
          </tr>
        </thead>
        {%- for role in election_roles %}
          {%- set role_data = election_form.data.getlist(role.title) if role.title in election_form.data else [] %}

          <tbody
            {% if role.max_choice > 1 -%}
              x-data x-limited-choices="{{ role.max_choice }}"
            {%- endif %}
            class="role {% if role.title in election_form.errors %}role_error{% endif %}"
          >
            <tr>
              <td class="role_title">
                <div class="role_text">
                  <h4>{{ role.title }}</h4>
                  <p class="role_description" show-more="300">{{ role.description }}</p>
                  {%- if role.max_choice > 1 and show_vote_buttons %}
                    <strong>
                      {% trans trimmed nb_choices=role.max_choice %}
                        You may choose up to {{ nb_choices }} people.
                      {% endtrans %}
                    </strong>
                  {%- endif %}

                  {%- if election_form.errors[role.title] is defined %}
                    {%- for error in election_form.errors.as_data()[role.title] %}
                      <strong class="error">{{ error.message }}</strong>
                    {%- endfor %}
                  {%- endif %}
                </div>
                {% if user.can_edit(role) and election.is_vote_editable -%}
                  <div class="role_buttons">
                    <a href="{{ url('election:update_role', role_id=role.id) }}">️
                      <i class="fa-regular fa-pen-to-square edit-action"></i>
                    </a>
                    <a href="{{ url('election:delete_role', role_id=role.id) }}">
                      <i class="fa-regular fa-trash-can delete-action"></i>
                    </a>
                    {%- if loop.last -%}
                      <button disabled><i class="fa fa-arrow-down"></i></button>
                      <button disabled><i class="fa fa-caret-down"></i></button>
                    {%- else -%}
                      <button
                        type="button"
                        onclick="window.location.replace('?role={{ role.id }}&action=bottom');"
                      >
                        <i class="fa fa-arrow-down"></i>
                      </button>
                      <button
                        type="button"
                        onclick="window.location.replace('?role={{ role.id }}&action=down');"
                      >
                        <i class="fa fa-caret-down"></i>
                      </button>
                    {%- endif -%}
                    {%- if loop.first -%}
                      <button disabled><i class="fa fa-caret-up"></i></button>
                      <button disabled><i class="fa fa-arrow-up"></i></button>
                    {%- else -%}
                      <button
                        type="button"
                        onclick="window.location.replace('?role={{ role.id }}&action=up');"
                      >
                        <i
                          class="fa fa-caret-up"></i>
                      </button>
                      <button
                        type="button"
                        onclick="window.location.replace('?role={{ role.id }}&action=top');"
                      ><i class="fa fa-arrow-up"></i>
                      </button>
                    {%- endif -%}
                  </div>
                {%- endif -%}
              </td>
            </tr>
            <tr class="role_candidates">
              <td
                class="list_per_role"
                style="width: 100%; max-width: {{ 100 / (election_lists|length + 1) }}%"
              >
                {%- if role.max_choice == 1 and show_vote_buttons %}
                  <div class="radio-btn">
                    {% set input_id = "blank_vote_" + role.id|string %}
                    <input id="{{ input_id }}" type="radio" name="{{ role.title }}" value="" checked>
                    <label for="{{ input_id }}">
                      <span>{% trans %}Choose blank vote{% endtrans %}</span>
                    </label>
                  </div>
                {%- endif %}
                {%- if election.is_vote_finished %}
                  {%- set results = election_results[role.title]['blank vote'] %}
                  <div class="election__results">
                    <strong>
                      {{ results.vote }} {% trans %}votes{% endtrans %} ({{ "%.2f" % results.percent }} %)
                    </strong>
                  </div>
                {%- endif %}
              </td>
              {%- for election_list in election_lists %}
                <td
                  class="list_per_role"
                  style="width: 100%; max-width: {{ 100 / (election_lists|length + 1) }}%"
                >
                  <ul class="candidates">
                    {%- for candidature in election_list.candidatures.select_related("user", "user__profile_pict").filter(role=role) %}
                      <li class="candidate">
                        {%- if show_vote_buttons %}
                          {% set input_id = "candidature_" + candidature.id|string %}
                          <input
                            id="{{ input_id }}"
                            type="{{ 'checkbox' if role.max_choice > 1 else 'radio' }}"
                            {% if candidature.id|string in role_data %}checked{% endif %}
                            {% if user_has_voted %}disabled{% endif %}
                            name="{{ role.title }}"
                            value="{{ candidature.id }}"
                          >
                          <label for="{{ input_id }}">
                        {%- endif %}
                        <figure>
                          {%- if user.can_view(candidature.user) %}
                            {% if candidature.user.profile_pict %}
                              <img
                                class="candidate__picture"
                                src="{{ candidature.user.profile_pict.get_download_url() }}"
                                alt="{% trans %}Profile{% endtrans %}"
                              >
                            {% else %}
                              <img
                                class="candidate__picture"
                                src="{{ static('core/img/unknown.jpg') }}"
                                alt="{% trans %}Profile{% endtrans %}"
                              >
                            {% endif %}
                          {%- endif %}
                          <figcaption class="candidate__details">
                            <h5>{{ candidature.user.first_name }} <em>{{candidature.user.nick_name or ''}} </em>{{ candidature.user.last_name }}</h5>
                            {%- if not election.is_vote_finished %}
                              <q class="candidate_program" show-more="200">
                                {{ candidature.program|markdown }}
                              </q>
                            {%- endif %}
                          </figcaption>
                          {%- if user.can_edit(candidature) -%}
                            {%- if election.is_vote_editable -%}
                              <div class="edit_btns">
                                <a href="{{ url('election:update_candidate', candidature_id=candidature.id) }}">
                                  <i class="fa-regular fa-pen-to-square edit-action"></i>
                                </a>
                                <a href="{{ url('election:delete_candidate', candidature_id=candidature.id) }}">
                                  <i class="fa-regular fa-trash-can delete-action"></i>
                                </a>
                              </div>
                            {%- endif -%}
                          {%- endif -%}
                        </figure>
                        {%- if show_vote_buttons %}
                          </label>
                        {%- endif %}
                        {%- if election.is_vote_finished %}
                          {%- set results = election_results[role.title][candidature.user.username] %}
                          <div class="election__results">
                            <strong>{{ results.vote }} {% trans %}votes{% endtrans %} ({{ "%.2f" % results.percent }} %)</strong>
                          </div>
                        {%- endif %}
                      </li>
                    {%- endfor %}
                  </ul>
                </td>
              {%- endfor %}
            </tr>
          </tbody>
        {%- endfor %}
      </table>
    </form>
  </section>
  {% if not user.is_anonymous %}
    <section class="buttons">
      {%- if (election.can_candidate(user) and election.is_candidature_active) or (user.can_edit(election) and election.is_vote_editable) %}
        <a class="button" href="{{ url('election:candidate', election_id=object.id) }}">{% trans %}Candidate{% endtrans %}</a>
      {%- endif %}
      {%- if election.is_vote_editable %}
        <a class="button" href="{{ url('election:create_list', election_id=object.id) }}">{% trans %}Add a new list{% endtrans %}</a>
      {%- endif %}
      {%- if user.can_edit(election) %}
        {% if election.is_vote_editable %}
          <a  class="button" href="{{ url('election:create_role', election_id=object.id) }}">{% trans %}Add a new role{% endtrans %}</a>
        {% endif %}
        <a  class="button" href="{{ url('election:update', election_id=object.id) }}">{% trans %}Edit{% endtrans %}</a>
      {%- endif %}
      {%- if user.is_root %}
        <a  class="button" href="{{ url('election:delete', election_id=object.id) }}">{% trans %}Delete{% endtrans %}</a>
      {%- endif %}
    </section>
    {%- if show_vote_buttons %}
      <section class="buttons">
        <button class="button button_send" form="vote-form">{% trans %}Submit the vote !{% endtrans %}</button>
      </section>
    {%- endif %}
  {% endif %}
{% endblock %}
