{% extends "assignment/base_assignment.html" %}

{% load i18n %}
{% load staticfiles %}

{% block title %}{{ block.super }} – {% trans "Election" %} "{{ assignment }}"{% endblock %}

{% block content %}
<div id="sidebar">
    <div class="box">
        <h4>{% trans "Status" %}:</h4>
        {% trans assignment.get_status_display %}
        <h4>{% trans "Number of available posts" %}:</h4>
        {{ assignment.posts }}
    </div>

    <br><br>

    {% if perms.assignment.can_manage_assignment %}
        <div class="box">
        <h4>{% trans "Change status" %}:</h4>
        <input type="radio" name="status" onclick="window.location.href='{% url assignment_set_status assignment.id 'sea' %}';"
            {% if 'sea' in assignment.status %}checked{% endif %}>{% trans 'Searching for candidates' %}<br>
        <input type="radio" name="status" onclick="window.location.href='{% url assignment_set_status assignment.id 'vot' %}';"
            {% if 'vot' in assignment.status %}checked{% endif %}>{% trans 'Voting' %}<br>
        <input type="radio" name="status" onclick="window.location.href='{% url assignment_set_status assignment.id 'fin' %}';"
            {% if 'fin' in assignment.status %}checked{% endif %}>{% trans 'Finished' %}
        </div>
    {% endif %}
</div>

<div style="margin-right: 250px; min-width: 400px;">
    <h1>{{ assignment }}</h1>
    <p>{{ assignment.description|linebreaks }}</p>

{% if assignment.status != "fin" %}
    <h3>{% trans "Candidates" %}</h3>
    <ol>
    {% for person in assignment.get_participants %}
        <li>
            {{ person }}
            {% if perms.assignment.can_manage_assignment %}
                {% if assignment.status == "sea" or assignment.status == "vot" %}
                    <a href="{% url assignment_delother assignment.id person.person_id %}"><img src="{% static 'images/icons/delete.png' %}" title="{% trans 'Remove candidate' %}"></a>
                {% endif %}
            {% endif %}
            {% if person in assignment.elected %}
                | <b>{% trans "elected" %}</b>
                {% if perms.assignment.can_manage_assignment %}
                    {% if assignment.status == "sea" or assignment.status == "vot" %}
                        <a href="{% url assignment_user_not_elected assignment.id person.person_id %}"><img src="{% static 'images/icons/dialog-cancel.png' %}" title="{% trans 'Mark candidate as not elected' %}"></a>
                    {% endif %}
                {% endif %}
            {% endif %}
        </li>
    {% empty %}
        <li style="list-style: none outside none;"><i>{% trans "No candidates available." %}</i></li>
    {% endfor %}
    </ol>

    {% if assignment.status == "sea" or perms.assignment.can_manage_assignment and assignment.status == "vot" %}
    {% if  perms.assignment.can_nominate_self or perms.assignment.can_nominate_other %}
    <form action="" method="post">{% csrf_token %}
        {% if perms.assignment.can_nominate_self %}
            <p>
            {% if user_is_candidate %}
                <a href='{% url assignment_delrun assignment.id %}'>
                    <span class="button">
                        <span class="icon removeuser">{% trans 'Withdraw self candidature' %}</span>
                    </span>
                </a>
            {% else %}
                <a href='{% url assignment_run assignment.id %}'>
                    <span class="button">
                        <span class="icon adduser">{% trans 'Self candidature' %}</span>
                    </span>
                </a>
            {% endif %}
            </p>
        {% endif %}
        {% if perms.assignment.can_nominate_other %}
            {% for field in form %}
            <label>{{ field.label }}:</label>
            <nobr>{{ field }}
            {% if perms.participant.can_see_participant and perms.participant.can_manage_participant %}
            <a href="{% url user_new %}" target="_blank"><img src="{% static 'images/icons/add-user.png' %}" title="{% trans 'Add new participant' %}"></a>
            {% endif %}
            </nobr>
            {% endfor %}
            <p>
            <button class="button" type="submit">
                <span class="icon ok">{% trans 'Apply' %}</span>
            </button>
            </p>
        {% endif %}
    </form>
    {% endif %}
    {% endif %}
{% endif %}

{% if perms.assignment.can_manage_assignments and blocked_candidates and assignment.status != "fin" %}
    <h3>{% trans "Blocked Candidates" %}</h3>
    <ul>
    {% for person in blocked_candidates %}
        <li>
            {{ person }}<a href="{% url assignment_delother assignment.id person.person_id %}"><img src="{% static 'images/icons/delete.png' %}" title="{% trans 'Remove candidate' %}"></a>
        </li>
    {% empty %}
        <li>{% trans "No blocked candidates available." %}</li>
    {% endfor %}
    </ul>
{% endif %}

{% if assignment.status != "sea" or polls.exists %}
    <h3>{% trans "Election results" %}</h3>

    {% if polls.exists %}
    <table id="election_table" style="width: auto;">
    <tr>
        <th>{% trans "Candidates" %}</th>
        {% for poll in polls %}
            <th style="vertical-align: top; white-space:nowrap;">
                {{ poll.get_ballot }}. {% trans 'ballot' %}
                {% if perms.assignment.can_manage_assignment %}
                    <br>
                    <a class="publish_link {% if poll.published %}published{% endif %}"
                    href="{% url assignment_poll_publish_status poll.id %}"
                    title="{% trans 'Publish/unpublish results' %}">
                    <span></span>
                    </a>
                    <a href="{% url assignment_poll_view poll.id %}"><img src="{% static 'images/icons/edit.png' %}" title="{% trans 'Edit' %}"></a>
                    <a href="{% url assignment_poll_delete poll.id %}"><img src="{% static 'images/icons/delete.png' %}" title="{% trans 'Delete' %}"></a>
                {% endif %}
            </th>
        {% endfor %}
        {% if assignment.candidates and perms.assignment.can_manage_assignment and assignment.status == "vot" %}
            <th>
                <a href='{% url assignment_gen_poll assignment.id %}'>
                    <span class="button">
                       <span class="icon statistics">{% trans 'New ballot' %}</span>
                    </span>
                </a>
            </th>
        {% endif %}
    </tr>

    {% for candidate, poll_list in vote_results.items %}
    <tr class="{% cycle 'odd' '' as rowcolors %}">
        <td class="candidate">
            {% if candidate in assignment.elected %}
                {% if perms.assignment.can_manage_assignment %}
                    <a class="election_link elected" href='{% url assignment_user_not_elected assignment.id candidate.person_id %}'></a>
                {% else %}
                    <a class="elected">
                        <img src="{% static 'images/icons/voting-yes.png' %}" title="{% trans 'Candidate is elected' %}">
                    </a>
                {% endif %}
            {% else %}
                {% if perms.assignment.can_manage_assignment %}
                    <a class="election_link" href='{% url assignment_user_elected assignment.id candidate.person_id %}'></a>
                {% endif %}
            {% endif %}
            {{ candidate }}
        </td>
        {% for vote in poll_list %}
            <td style="white-space:nowrap;">
                {% if 'Yes' in vote and 'No' in vote and 'Abstain' in vote %}
                    <img src="{% static 'images/icons/voting-yes.png' %}" title="{% trans 'Yes' %}"> {{ vote.Yes }}<br>
                    <img src="{% static 'images/icons/voting-no.png' %}" title="{% trans 'No' %}"> {{ vote.No }}<br>
                    <img src="{% static 'images/icons/voting-abstention.png' %}" title="{% trans 'Abstention' %}"> {{ vote.Abstain }}<br>
                {% elif 'Votes' in vote %}
                    <img src="{% static 'images/icons/voting-yes.png' %}" title="{% trans 'Yes' %}"> {{ vote.Votes }}
                {% elif vote == None %}
                    {% trans 'was not a <br> candidate'%}
                {% else %}
                    &nbsp;
                {% endif %}
            </td>
        {% endfor %}
        {% if assignment.candidates and perms.assignment.can_manage_assignment and assignment.status == "vot" %}
            <td></td>
        {% endif %}
    </tr>
    {% endfor %}
    <tr class="{% cycle rowcolors %}">
        <td>{% trans 'Invalid votes' %}</td>
        {% for poll in polls %}
            {% if poll.published or perms.assignment.can_manage_assignment %}
                <td style="white-space:nowrap;">
                    {% if poll.has_votes %}
                        <img src="{% static 'images/icons/voting-invalid.png' %}" title="{% trans 'Invalid' %}">
                        {{ poll.print_votesinvalid }}
                    {% endif %}
                </td>
            {% endif %}
        {% endfor %}
        {% if assignment.candidates and perms.assignment.can_manage_assignment and assignment.status == "vot" %}
            <td></td>
        {% endif %}
    </tr>
    <tr class="total">
        <td><strong>{% trans 'Votes cast' %}</strong></td>
        {% for poll in polls %}
            {% if poll.published or perms.assignment.can_manage_assignment %}
                <td style="white-space:nowrap;">
                    {% if poll.has_votes %}
                        <img src="{% static 'images/icons/voting-total.png' %}" title="{% trans 'Votes cast' %}">
                        <strong>{{ poll.print_votescast }}</strong>
                    {% endif %}
                </td>
            {% endif %}
        {% endfor %}
        {% if assignment.candidates and perms.assignment.can_manage_assignment and assignment.status == "vot" %}
            <td></td>
        {% endif %}
    </tr>
    </table>
    {% else %}
    <i>{% trans "No results available." %}</i>
    {% if assignment.candidates and perms.assignment.can_manage_assignment and assignment.status == "vot" %}
        <p><a href='{% url assignment_gen_poll assignment.id %}'>
        <span class="button">
           <span class="icon statistics">{% trans 'New ballot' %}</span>
        </span>
        </a></p>
    {% endif %}
    {% endif %}
{% endif %}
</div>
{% endblock %}
