{% extends "base.html" %}

{% load i18n %}
{% load staticfiles %}
{% load tags %}
{% load humanize %}

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


{% block javascript %}
    <script type="text/javascript" src="{% static 'js/assignment.js' %}"></script>
{% endblock %}

{% block content %}
<h1>
    {{ assignment }}
    <br>
    <small>{% trans "Election" %}</small>
    <small class="pull-right">
    <a href="{% url 'assignment_list' %}" class="btn btn-mini"><i class="icon-chevron-left"></i><span class="optional-small"> {% trans "Back to overview" %}</span></a>
    <a href="{% url 'assignment_pdf' assignment.id  %}" class="btn btn-mini" rel="tooltip" data-original-title="{% trans 'Print election as PDF' %}" target="_blank"><i class="icon-print"></i><span class="optional-small"> PDF</span></a>
    <!-- activate projector -->
    {% if perms.core.can_manage_projector %}
        <a href="{{ assignment|absolute_url:'projector' }}"
            class="activate_link btn {% if assignment.is_active_slide %}btn-primary{% endif %} btn-mini"
            rel="tooltip" data-original-title="{% trans 'Show election' %}">
            <i class="icon-facetime-video {% if assignment.is_active_slide %}icon-white{% endif %}"></i>
        </a>
    {% endif %}
    {% if perms.assignment.can_manage_assignment or perms.agenda.can_manage_agenda %}
    <div class="btn-group">
        <a data-toggle="dropdown" class="btn btn-mini dropdown-toggle">
            <span class="optional-small">{% trans 'More actions' %}</span> <span class="caret"></span>
        </a>
        <ul class="dropdown-menu pull-right">
            {% if perms.assignment.can_manage_assignment %}
                <!-- edit -->
                <li><a href="{{ assignment|absolute_url:'update' }}"><i class="icon-pencil"></i> {% trans 'Edit election' %}</a></li>
                <!-- delete -->
                <li><a href="{{ assignment|absolute_url:'delete' }}"><i class="icon-remove"></i> {% trans 'Delete election' %}</a></li>
            {% endif %}
            <!-- create agenda item -->
            {% if perms.agenda.can_manage_agenda %}
                <li><a href="{% url 'assignment_create_agenda' assignment.id  %}"><i class="icon-plus"></i> {% trans 'New agenda item' %}</a></li>
            {% endif %}
        </ul>
    </div>
    {% endif %}
    </small>
</h1>

<!-- Tags -->
{% for tag in assignment.tags.all %}
    <span class="optional label">{{ tag }}</span>
{% endfor %}

<div class="row-fluid">
<div class="span9">
    <!-- Description -->
    <h4>{% trans "Description" %}</h4>
    {% if assignment.description  %}
        {{ assignment.description|linebreaks }}
    {% else %}
        –
    {% endif %}
    <br>
    <!-- Candidates -->
{% if assignment.status != "fin" %}
    <h4>{% trans "Candidates" %}</h4>
    <ol>
    {% for person in assignment.get_participants %}
        <li>
            <a href="{{ person|absolute_url }}">{{ person }}</a>
            {% if perms.assignment.can_manage_assignment %}
                {% if assignment.status == "sea" or assignment.status == "vot" %}
                    <a href="{% url 'assignment_delother' assignment.id person.person_id %}" class="btn btn-mini" rel="tooltip" data-original-title="{% trans 'Remove candidate' %}"><i class="icon-remove"></i></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 class="btn btn-mini" href="{% url 'assignment_user_not_elected' assignment.id person.person_id %}" rel="tooltip" data-original-title="{% trans 'Mark candidate as not elected' %}">
                            <i class="icon-ban-circle"></i>
                        </a>
                    {% endif %}
                {% endif %}
            {% endif %}
        </li>
    {% empty %}
        <li style="list-style: none outside none; margin-left: -25px;"><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 %}' class="btn btn-mini">
                    <i class="icon-remove"></i> {% trans 'Withdraw self candidature' %}
                </a>
            {% else %}
                <a href='{% url 'assignment_run' assignment.id %}' class="btn btn-mini">
                    <i class="icon-plus"></i> {% trans 'Self candidature' %}
                </a>
            {% endif %}
            </p>
        {% endif %}
        {% if perms.assignment.can_nominate_other %}
            {% for field in form %}
                <label>{{ field.label }}:</label>
                <div class="input-append">
                    {{ field }}
                    <button class="btn btn-primary tooltip-bottom" type="submit"
                        data-original-title="{% trans 'Apply' %}"><i class="icon-ok icon-white"></i></button>
                    {% if perms.participant.can_see_participant and perms.participant.can_manage_participant %}
                        <a href="{% url 'user_new' %}" target="_blank" class="btn"
                            rel="tooltip" data-original-title="{% trans 'Add new participant' %}"><i class="icon-add-user"></i></a>
                    {% endif %}
                </div>
            {% endfor %}
        {% endif %}
    </form>
    {% endif %}
    {% endif %}
{% endif %}

{% if perms.assignment.can_manage_assignment and blocked_candidates and assignment.status != "fin" %}
    <h4>{% trans "Blocked Candidates" %}</h4>
    <ul>
    {% for person in blocked_candidates %}
        <li>
            <a href="{{ person|absolute_url }}">{{ person }}</a>
            <a class="btn btn-mini" href="{% url 'assignment_delother' assignment.id person.person_id %}"
                rel="tooltip" data-original-title="{% trans 'Remove candidate' %}">
                <i class="icon-ban-circle"></i>
            </a>
        </li>
    {% empty %}
        <li>{% trans "No blocked candidates available." %}</li>
    {% endfor %}
    </ul>
{% endif %}


<!-- Election result -->
{% if assignment.status != "sea" or polls.exists %}
    <h4>{% trans "Election result" %}</h4>
    {% if polls.exists %}
        <table id="election-result-table" class="table table-striped table-bordered">
        <tr>
            <th>{% trans "Candidates" %}</th>
            {% for poll in polls %}
                <th style="white-space:nowrap;" class="span1">
                    {% if perms.assignment.can_manage_assignment %}<p class="text-center">{% endif %}
                    {{ poll.get_ballot|ordinal|safe }} {% trans 'ballot' %}
                    {% if perms.assignment.can_manage_assignment %}
                        <a class="publish_link btn btn-mini btn-danger {% if poll.published %}btn-primary{% endif %}"
                        href="{% url 'assignmentpoll_publish_status' poll.id %}"
                        rel="tooltip" data-original-title="{% trans 'Publish result' %}">
                        {% if poll.published %}
                            <i class="icon-checked-new_white"></i>
                            {% else %}
                            <i class="icon-unchecked-new"></i>
                            {% endif %}
                        </a>
                        </p>
                        <p class="text-center">
                        <a href="{{ poll|absolute_url:'projector' }}"
                            class="btn btn-mini activate_link {% if poll.is_active_slide %}btn-primary{% endif %}"
                            rel="tooltip" data-original-title="{% trans 'Show election result' %}">
                            <i class="icon-facetime-video {% if poll.is_active_slide %}icon-white{% endif %}"></i></a>
                        <a href="{% url 'assignmentpoll_pdf' poll.id %}" class="btn btn-mini" target="_blank"
                            rel="tooltip" data-original-title="{% trans 'Ballot paper as PDF' %}"><i class="icon-print"></i></a>
                        <a href="{% url 'assignmentpoll_update' poll.id %}" class="btn btn-mini"
                            rel="tooltip" data-original-title="{% trans 'Edit' %}"><i class="icon-pencil"></i></a>
                        <a href="{% url 'assignmentpoll_delete' poll.id %}" class="btn btn-mini"
                            rel="tooltip" data-original-title="{% trans 'Delete' %}"><i class="icon-remove"></i></a>
                        </p>
                    {% endif %}
                </th>
            {% endfor %}
            {% if assignment.candidates and perms.assignment.can_manage_assignment and assignment.status == "vot" %}
                <th class="span1 nobr">
                    <a href="{% url 'assignmentpoll_create' assignment.pk %}" class="btn btn-mini">
                       <i class="icon-plus"></i> {% trans 'New ballot' %}
                    </a>
                </th>
            {% endif %}
        </tr>
        {% for candidate, poll_list in vote_results.items %}
        <tr>
            <td>
                {% if candidate in assignment.elected %}
                    {% if perms.assignment.can_manage_assignment %}
                        <a class="election_link elected tooltip-bottom" href="{% url 'assignment_user_not_elected' assignment.id candidate.person_id %}"
                            data-original-title="{% trans 'Mark candidate as elected' %}"></a>
                    {% else %}
                        <a class="elected">
                            <img src="{% static 'img/voting-yes.png' %}" class="tooltip-bottom" data-original-title="{% trans 'Candidate is elected' %}">
                        </a>
                    {% endif %}
                {% else %}
                    {% if perms.assignment.can_manage_assignment %}
                        <a class="election_link tooltip-bottom" href="{% url 'assignment_user_elected' assignment.id candidate.person_id %}"
                           data-original-title="{% trans 'Mark candidate as elected' %}"></a>
                    {% endif %}
                {% endif %}
                <a href="{{ candidate|absolute_url }}">{{ candidate }}</a>
            </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 'img/voting-yes.png' %}" class="tooltip-left" data-original-title="{% trans 'Yes' %}"> {{ vote.Yes }}<br>
                        <img src="{% static 'img/voting-no.png' %}" class="tooltip-left" data-original-title="{% trans 'No' %}"> {{ vote.No }}<br>
                        <img src="{% static 'img/voting-abstention.png' %}" class="tooltip-left" data-original-title="{% trans 'Abstention' %}"> {{ vote.Abstain }}<br>
                    {% elif 'Votes' in vote %}
                        <img src="{% static 'img/voting-yes.png' %}" class="tooltip-left" data-original-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>
            <td>{% trans 'Valid 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 'img/voting-yes-grey.png' %}" class="tooltip-left" data-original-title="{% trans 'Valid votes' %}">
                            {{ poll.print_votesvalid }}
                        {% endif %}
                    </td>
                {% endif %}
            {% endfor %}
            {% if assignment.candidates and perms.assignment.can_manage_assignment and assignment.status == "vot" %}
                <td></td>
            {% endif %}
        </tr>
        <tr>
            <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 'img/voting-invalid.png' %}" class="tooltip-left" data-original-title="{% trans 'Invalid votes' %}">
                            {{ 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="info total">
            <td>{% trans 'Votes cast' %}</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 'img/voting-total.png' %}" class="tooltip-left" data-original-title="{% trans 'Votes cast' %}">
                            {{ poll.print_votescast }}
                        {% 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 ballots available." %}</i>
        {% if assignment.candidates and perms.assignment.can_manage_assignment and assignment.status == "vot" %}
            <p>
            <a href='{% url 'assignmentpoll_create' assignment.id %}' class="btn">
               <i class="icon-plus"></i> {% trans 'New ballot' %}
            </a>
            </p>
        {% endif %}
    {% endif %}
{% endif %}
</div> <!--/span-->

<div class="span3">
    <div class="well">
        <!-- Text -->
        <h5>{% trans "Status" %}:</h5>
        {% trans assignment.get_status_display %}
        <!-- Posts -->
        <h5>{% trans "Number of available posts" %}:</h5>
        {{ assignment.posts }}
    </div> <!--/well-->

    {% if perms.assignment.can_manage_assignment %}
    <div class="well">
        <h4>{% trans "Change status" %}:</h4>
        <div class="btn-group btn-group-vertical" data-toggle="buttons-radio">
            <a href="{% url 'assignment_set_status' assignment.id 'sea' %}"
                class="btn btn-small {% if 'sea' in assignment.status %}active{% endif %}">
                {% trans 'Searching for candidates' %}</a>
            <a href="{% url 'assignment_set_status' assignment.id 'vot' %}"
                class="btn btn-small {% if 'vot' in assignment.status %}active{% endif %}">
                {% trans 'Voting' %}</a>
            <a href="{% url 'assignment_set_status' assignment.id 'fin' %}"
                class="btn btn-small {% if 'fin' in assignment.status %}active{% endif %}">
                {% trans 'Finished' %}</a>
        </div>
    </div> <!--/well-->
    {% endif %}
</div> <!--/span-->
</div> <!--/row-->
{% endblock %}
