{% extends "base.html" %}

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

{% block title %}{% trans "Motion" %} {{ motion.identifier|default:'' }} – {{ block.super }}{% endblock %}

{% block header %}
    {{ block.super }}
    <link type="text/css" rel="stylesheet" media="all" href="{% static 'styles/motion.css' %}" />
{% endblock %}

{% block content %}
<h1>
    {{ title }}
    <br>
    <small>{% trans "Motion" %} {{ motion.identifier|default:'' }}
    {% if motion.versions.count > 1 %}
        | {% trans "Version" %} {{ version.version_number }}
    {% endif %}
    </small>
    <small class="pull-right">
        <a href="{% url 'motion_list' %}" class="btn btn-mini">
            <i class="icon-chevron-left"></i> {% trans "Back to overview" %}</a>
        <a href="{% url 'motion_detail_pdf' motion.id  %}" class="btn btn-mini"
            rel="tooltip" data-original-title="{% trans 'Print motion as PDF' %}" target="_blank">
            <i class="icon-print"></i> PDF</a>
        <!-- activate projector -->
        {% if perms.projector.can_manage_projector %}
            <a href="{{ motion|absolute_url:'projector' }}" class="activate_link btn {% if motion.is_active_slide %}btn-primary{% endif %} btn-mini" rel="tooltip" data-original-title="{% trans 'Show motion' %}">
                <i class="icon-facetime-video {% if motion.is_active_slide %}icon-white{% endif %}"></i>
            </a>
        {% endif %}
        {% if perms.motion.can_manage_motion or perms.agenda.can_manage_agenda or allowed_actions.edit %}
        <div class="btn-group">
            <a data-toggle="dropdown" class="btn btn-mini dropdown-toggle">
                {% trans 'More actions' %} <span class="caret"></span>
            </a>
            <ul class="dropdown-menu pull-right">
                <!-- edit -->
                {% if allowed_actions.edit %}
                     <li><a href="{{ motion|absolute_url:'update' }}"><i class="icon-pencil"></i> {% trans 'Edit motion' %}</a></li>
                {% endif %}
                {% if perms.motion.can_manage_motion %}
                    <!-- delete -->
                    {% if allowed_actions.delete %}
                        <li><a href="{{ motion|absolute_url:'delete' }}"><i class="icon-remove"></i> {% trans 'Delete motion' %}</a></li>
                    {% endif %}
                {% endif %}
                <!-- create agenda item -->
                {% if perms.agenda.can_manage_agenda %}
                    <li><a href="{% url 'motion_create_agenda' motion.id  %}"><i class="icon-plus"></i> {% trans 'New agenda item' %}</a></li>
                {% endif %}
            </ul>
        </div>
        {% endif %}
    </small>
</h1>

<div class="row-fluid">
<div class="span8">
    {# TODO: show only for workflow with versioning #}
    {% with last_version=motion.get_last_version active_version=motion.get_active_version %}
        {% if version.version_number != last_version.version_number %}
            <p><span class="label label-warning">
                <i class="icon-warning-sign icon-white"></i> {% trans "This is not the newest version." %}
            </span>
            <a href="{{ last_version|absolute_url }}" class="btn btn-small">{% trans "Go to the newest version" %}
            (# {{ last_version.version_number }})</a></p>
        {% endif %}
        {% if version.version_number != active_version.version_number %}
            <p><span class="label label-warning">
                <i class="icon-warning-sign icon-white"></i> {% trans "This version is not authorized." %}
            </span>
            <a href="{{ active_version|absolute_url }}" class="btn btn-small">{% trans "Go to the authorized version" %}
            (# {{ active_version.version_number }})</a></p>
        {% endif %}
    {% endwith %}

    <!-- Text -->
    <h4>{% trans "Motion text" %}:</h4>
    {{ text|safe }}
    <br>

    <!-- Reason -->
    <h4>{% trans "Reason" %}:</h4>
        {{ reason|safe|default:'–' }}
    <br>

    <!-- Attachments -->
    {% with attachments=motion.attachments.all %}
        {% if attachments %}
            <h4>{% trans "Attachments" %}:</h4>
            {% for attachment in attachments %}
                <p><a href="{{ attachment.mediafile.url }}">{{ attachment }}</a></p>
            {% endfor %}
            <br>
        {% endif %}
    {% endwith %}

    <!-- Version history -->
    {% with versions=motion.versions.all %}
        {% if versions|length > 1 %}
            {% for version in versions %}
            {% if forloop.first %}
                <h4>{% trans "Version history" %}:</h4>
                <form action="{% url 'motion_version_diff' motion.pk %}" method="get">
                <table class="table table-striped table-bordered">
                <tr>
                    <th></th>
                    <th>#</th>
                    <th>{% trans "Time" %}</th>
                    <th><button class="btn btn-small" type="submit">{% trans 'Difference' %}</button></th>
                    <th>{% trans "Actions" %}</th>
                </tr>
            {% endif %}
                <tr {% if version == motion.use_version %}class="offline"{%endif %}>
                    <td class="nobr">
                    {% if version == motion.active_version %}
                         <span class="badge badge-success" title="{% trans 'This version is authorized' %}"><i class="icon-ok icon-white"></i></span>
                    {% else %}
                         {% if perms.motion.can_manage_motion %}
                             <a class="btn btn-mini" href="{% url 'motion_version_permit' motion.id version.version_number %}" title="{% trans 'Permit this version' %}"><i class="icon-ok"></i></a>
                         {% endif %}
                    {% endif %}
                    </td>
                    <td>{{ version.version_number }}</td>
                    <td><i>{{ version.creation_time }}</i></td>
                    <td>
                        <input type="radio" value="{{ version.version_number }}" name="rev1">
                        <input type="radio" value="{{ version.version_number }}" name="rev2">
                    </td>
                    <td>
                        <a href="{{ version|absolute_url }}" title="{% trans 'Show' %}" class="btn btn-mini">
                            <i class="icon-search"></i>
                        </a>
                        {% if version != motion.active_version %}
                            <a href="{{ version|absolute_url:'delete' }}" title="{% trans 'Delete' %}" class="btn btn-mini">
                                <i class="icon-remove"></i>
                            </a>
                        {% endif %}
                    </td>
                </tr>
                {% if forloop.last %}
                </table>
                </form>
                {% endif %}
            {% endfor %}
        {% endif %}
    {% endwith %}

    <!-- Log -->
    {% if perms.motion.can_manage_motion %}
        <p>
        <div class="btn-group" data-toggle="buttons-checkbox">
        <button type="button" class="btn" data-toggle="collapse" data-target="#log">
            {% trans "Show log" %}
        </button>
        </div>
        <div id="log" class="collapse out">
            <ul class="unstyled">
                {% for message in motion.log_messages.all %}
                <li><small>{{ message }}</small></li>
                {% endfor %}
            </ul>
        </div>
        </p>
    {% endif %}
</div> <!--/span-->

<!-- Box with meta information -->
<div class="span4">
    <div class="well">
        <!-- Submitter -->
        <h5>{% trans "Submitter" %}:</h5>
        {% for submitter in motion.submitter.all %}
            <a href="{{ submitter.person|absolute_url }}">{{ submitter }}</a>{% if not forloop.last %}, {% endif %}
        {% endfor %}

        <!-- Supporters -->
        {% if 'motion_min_supporters'|get_config > 0 %}
            <h5>{% trans "Supporters" %}: *</h5>
            {% if not motion.supporters %}
                -
            {% else %}
                <ol>
                {% for supporter in motion.supporter.all %}
                    <li><a href="{{ supporter.person|absolute_url }}">{{ supporter }}</a></li>
                {% endfor %}
                </ol>
            {% endif %}
        {% endif %}

        <!-- Status -->
        <h5>{% trans "Status" %}:</h5>
        <span class="label label-info">{% trans motion.state.name %}</span>
        <br>

        <!-- Vote results -->
        <h5>{% trans "Vote results" %}:</h5>
        {% with motion.polls.all as polls %}
        <ul id="motion-vote-results">
        {% for poll in polls %}
            {% if perms.motion.can_manage_motion or poll.has_votes %}
                <li><b>{{ poll.poll_number|ordinal|safe }} {% trans "vote" %}</b>
                    {% if perms.motion.can_manage_motion %}
                        <a class="btn btn-mini" href="{{ poll|absolute_url:'update' }}" title="{% trans 'Edit Vote' %}"><i class="icon-pencil"></i></a>
                        <a class="btn btn-mini" href="{{ poll|absolute_url:'delete' }}" title="{% trans 'Delete Vote' %}"><i class="icon-remove"></i></a>
                    {% endif %}
                    <br>
                    {% if poll.has_votes %}
                        {% with poll.get_options.0 as option %}
                            <img src="{% static 'img/voting-yes.png' %}" title="{% trans 'Yes' %}"> {{ option.Yes }}<br>
                            <img src="{% static 'img/voting-no.png' %}" title="{% trans 'No' %}"> {{ option.No }}<br>
                            <img src="{% static 'img/voting-abstention.png' %}" title="{% trans 'Abstention' %}"> {{ option.Abstain }}<br>
                            <img src="{% static 'img/voting-invalid.png' %}" title="{% trans 'Invalid' %}"> {{ poll.print_votesinvalid }}<br>
                            <div style="border-top: 1px solid; padding-top: 5px; margin: 5px 0; width: 10em;">
                                <img src="{% static 'img/voting-total.png' %}" title="{% trans 'Votes cast' %}"> {{ poll.print_votescast }}
                            </div>
                        {% endwith %}
                    {% else %}
                        {% if perms.motion.can_manage_motion %}
                            <span class="label label-info">{% trans 'No results' %}</span>
                         {% endif %}
                    {% endif %}
                </li>
            {% endif %}
        {% empty %}
        <span style="margin-left:-25px;">–</span>
        {% endfor %}
        </ul>
        {% if allowed_actions.create_poll %}
        <a href="{% url 'motion_poll_create' motion.id %}" class="btn btn-mini">
            <i class="icon-assignment"></i> {% trans 'New vote' %}
        </a>
        {% endif %}
        {% endwith %}

        <!-- Category -->
        <h5>{% trans "Category" %}:</h5>
        {% if motion.category %}
            {{ motion.category }}
        {% else %}
            –
        {% endif %}

        <!-- Creation Time -->
        <h5>
            {% if motion.versions.count > 1 %}
                {% trans "Last changes (of this version)" %}:
            {% else %}
                {% trans "Last changes" %}:
            {% endif %}
        </h5>
        {{ version.creation_time }}

        <!-- Widthdraw button -->
        {# TODO: Check this button #}
        {% if  allowed_actions.wit and user in motion.submitters %}
            <br><br>
            <a href="{% url 'motion_set_state' motion.id 'wit' %}" class="btn">
                <span class="icon revert">{% trans 'Withdraw motion' %}</span>
            </a>
        {% endif %}

        <!-- Support/Unsupport button -->
        {% if perms.motion.can_support_motion and 'motion_min_supporters'|get_config > 0 %}
            {% if allowed_actions.unsupport %}
                <br><br>
                <a href="{% url 'motion_unsupport' motion.id %}" class="btn">
                    {% trans 'Unsupport' %}
                </a>
            {% endif %}
            {% if allowed_actions.support %}
                <br><br>
                <a href="{% url 'motion_support' motion.id %}" class="btn">
                    {% trans 'Support' %}
                </a>
            {% endif %}
        {% endif %}

        <!-- Footnote: requried supporters -->
        {% if 'motion_min_supporters'|get_config > 0 %}
            <br><br>
            <small>* {% trans "minimum required supporters" %}: {{ 'motion_min_supporters'|get_config }}</small>
        {% endif %}
    </div> <!--/well-->

    {% if perms.motion.can_manage_motion %}
    <!-- Manage motion box -->
    <div class="well">
    <h4>{% trans "Manage motion" %}</h4>
        {% for state in motion.state.next_states.all %}
            {% if forloop.first %}
                <div class="btn-group btn-group-vertical">
            {% endif %}
            <a href="{% url 'motion_set_state' motion.pk state.pk %}" class="btn btn-small">{% trans state.get_action_word %}</a>
            {% if forloop.last %}
                </div>
            {% endif %}
        {% endfor %}
    <h5>{% trans "For administration only:" %}</h5>
    <a href="{% url 'motion_reset_state' motion.id %}" class="btn btn-mini btn-danger">
        <i class="icon-exclamation-sign icon-white"></i> {% trans 'Reset state' %}
    </a>
    </div> <!--/well-->
    {% endif %}
  </div> <!--/span-->
</div> <!--/row-->

{% endblock %}
