{% extends "base.html" %}

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

{% block title %}{% trans "Motions" %} – {{ block.super }}{% endblock %}

{% block header %}
    <link href="{% static 'css/dataTables/dataTables.bootstrap.css' %}" type="text/css" rel="stylesheet">
{% endblock %}

{% block javascript %}
    <script src="{% static 'js/jquery/jquery.dataTables.min.js' %}" type="text/javascript"></script>
    <script src="{% static 'js/naturalSort.js' %}" type="text/javascript"></script>
    <script src="{% static 'js/jquery/dataTables.bootstrap.js' %}" type="text/javascript"></script>
    <script type="text/javascript" charset="utf-8">
        $(document).ready(function()
        {
            var tblapi = $('#dataTable').dataTable();
            // change sort type to "natural", sadly there seems to be no public API
            // to change the sort type so we have to resort to using a private API
            tblapi._fnColumnOptions(0, {'sType': 'natural'});
            // redraw necessary to apply changed sort settings
            tblapi.fnDraw();
        });
    </script>
{% endblock %}

{% block content %}
    <h1>
        {% trans "Motions" %}
        <small class="pull-right">
        {% if perms.motion.can_create_motion %}
            {% if not 'motion_stop_submitting'|get_config or perms.motion.can_manage_motion %}
                <a href="{% url 'motion_create' %}" class="btn btn-mini btn-primary" rel="tooltip" data-original-title="{% trans 'New motion' %}"><i class="icon-plus icon-white"></i> {% trans 'New' %}</a>
            {% endif %}
        {% endif %}
        {% if perms.core.can_manage_tags %}
            <a href="{% url 'core_tag_list' %}" class="btn btn-mini" rel="tooltip" data-original-title="{% trans 'Manage tags' %}">
                <i class="icon-tags"></i>
                <span class="optional-small"> {% trans 'Tags' %}</span>
            </a>
        {% endif %}
        {% if perms.motion.can_manage_motion %}
            <a href="{% url 'motion_category_list' %}" class="btn btn-mini" rel="tooltip" data-original-title="{% trans 'Manage categories' %}">
                <i class="icon-th-large"></i>
                <span class="optional-small"> {% trans 'Categories' %}</span>
            </a>
            <a href="{% url 'motion_csv_import' %}" class="btn btn-mini" rel="tooltip" data-original-title="{% trans 'Import motions' %}"><i class="icon-import"></i><span class="optional-small"> {% trans 'Import' %}</span></a>
        {% endif %}
        <a href="{% url 'motion_list_pdf' %}" class="btn btn-mini" rel="tooltip" data-original-title="{% trans 'Print all motions as PDF' %}" target="_blank"><i class="icon-print"></i><span class="optional-small"> PDF</span></a>
        </small>
    </h1>

    <table id="dataTable" class="table table-striped table-bordered">
    <thead>
        <tr>
            <th>{% trans "#" %}</th>
            <th>{% trans "Motion title" %}</th>
            <th class="optional">{% trans 'Category' %}</th>
            <th class="optional-small">{% trans "Status" %}</th>
            <th class="optional">{% trans "Submitter" %}</th>
            {% if 'motion_min_supporters'|get_config > 0 %}
                <th class="optional">{% trans "Supporters" %}</th>
            {% endif %}
            <th class="optional">{% trans "Last changes" %}</th>
            {% if perms.motion.can_manage_motion or perms.core.can_manage_projector %}
                <th class="mini_width">{% trans "Actions" %}</th>
            {% endif %}
        </tr>
    </thead>
    {% for motion in motion_list %}
        <tr class="{% if motion.is_active_slide %}activeline{% endif %}">
            <td class="nobr">{{ motion.identifier|default:'' }}
                {% if motion.is_amendment %}
                    <a class="badge badge-success" data-original-title="{% trans 'Amendment' %}" rel="tooltip">
                        {{ 'motion_amendments_prefix'|get_config }}
                    </a>
                {% endif %}
            </td>
            <td>
                <a href="{{ motion|absolute_url }}">{{ motion.title }}</a>
                {% for tag in motion.tags.all %}
                    <span class="optional label">{{ tag }}</span>
                {% endfor %}
            </td>
            <td class="optional">{% if motion.category %}{{ motion.category }}{% else %}–{% endif %}</td>
            <td class="optional-small"><span class="label label-info">{% trans motion.state.name %}</span></td>
            <td class="optional">
                {% for submitter in motion.submitter.all %}
                    {{ submitter.person }}{% if not forloop.last %}, {% endif %}
                {% endfor %}
            </td>
            {% if 'motion_min_supporters'|get_config > 0 %}
                {% with supporters=motion.supporters|length %}
                    <td class="optional">
                    {% if supporters >= 'motion_min_supporters'|get_config %}
                        <a class="badge badge-success" rel="tooltip" data-original-title="{% trans 'Enough supporters' %}">{{ supporters }}</a>
                    {% endif %}
                    {% if supporters < 'motion_min_supporters'|get_config %}
                        <a class="badge badge-warning" rel="tooltip" data-original-title="{% trans 'Needs supporters' %}">{{ supporters }}</a>
                    {% endif %}
                    </td>
                {% endwith %}
            {% endif %}
            <td class="optional">{{ motion.get_last_version.creation_time }}
                {% if motion.get_last_version.version_number != motion.active_version.version_number %}
                    <a href="{{ motion.get_last_version|absolute_url }}" class="label label-warning" rel="tooltip" data-original-title="{% trans 'There is a newer (unauthorized) version.' %}">
                        <i class="icon-warning-sign icon-white"></i>
                    </a>
                {% endif %}</td>
            {% if perms.motion.can_manage_motion or perms.core.can_manage_projector %}
            <td>
                <span style="width: 1px; white-space: nowrap;">
                {% if perms.core.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 %}
                    <a href="{{ motion|absolute_url:'update' }}" rel="tooltip" data-original-title="{% trans 'Edit' %}"
                        class="btn btn-mini"><i class="icon-pencil"></i>
                    </a>
                    <a href="{{ motion|absolute_url:'delete' }}" rel="tooltip" data-original-title="{% trans 'Delete' %}"
                        class="btn btn-mini"><i class="icon-remove"></i>
                    </a>
                {% endif %}
                </span>
            </td>
            {% endif %}
        </tr>
    {% endfor %}
    </table>
{% endblock %}
