{% extends "request_base.html" %}
{% load sekizai_tags %}
{% load urlbuilder %}
{% load url from future %}
{% load polynum %}

{% block breadcrumb %}
<p>Tableau de bord</p>
{% endblock %}

{% block content %}
{% addtoblock "endscripts" %}
<script type="text/javascript">
	$(function () { 
      $('#from-date-datepicker').datepicker();
      $('#from_date').change(
				function (ev) {
        $('#date-form').submit();
			});
      $('#to-date-datepicker').datepicker();
      $('#to_date').change(
				function (ev) {
        $('#date-form').submit();
			});
		});
</script>
{% endaddtoblock %}


{% comment %}
Here starts the filter block
{% endcomment %}


{% if filters or filter_choices %}
<div id="filters" class="well">
{% if filters %}
	<div>
		<span class="title">Filtres</span>
		{% for name, value, display_list in filters %}
			<div id="filter-group-{{name}}" class="filter-group">
				{% for caption, delete_url in display_list %}
				<span class="filter-element label">{{ caption }} <a class="icon-remove" href="{{delete_url}}">&nbsp;</a></span>
				{% endfor %}
			</div>
		{% endfor %}
	</div>
{% endif %}
{% if filter_choices %}
<div id="filter-choices-container"><a data-toggle="collapse" data-target="#filter-panel" id="filter-choices-title"><h4>Ajouter des filtres</h4></a>
	<div id="filter-panel">
    <div id="filter-text-libre"><label class="filter-title">Texte libre</label>
      <form style="margin-bottom: 0px" class="form-search">
        <div class="input-append">
          <input name="filter_free_text" type="text" value="{{ request.GET.filter_free_text }}" placeholder="Titre du document, Nom du commanditaire, Destinataire, Nom du fichier" class="input-xxlarge search-query"/>
          <button type="submit" class="btn">Filtrer</button>
        </div>
        {% for key, value in request.GET.iteritems %}
        {% if key != "filter_free_text" %}
        <input name="{{key}}" value="{{value}}" type="hidden"/>
        {% endif %}
        {% endfor %}
      </form>
		</div>
		{% for filter_choice in filter_choices %}
    <div id="filter-{{ filter_choice.caption|slugify}}"><label class="filter-title">{{ filter_choice.caption }}</label>
      <p class="filter-choices {% if filter_choice.choices|length > 5 %}long{% endif %}">
        {% for url, caption in filter_choice.choices %}
        <span class="filter-label"><a href="{{ url }}">{{ caption }}</a></span>
        {% endfor %}
      </p>
		</div>
		{% endfor %}
		<form action="{{qs}}" id='date-form' method='post'>
			{% csrf_token %}
			<div id="from-date-datepicker" class="input-append date" data-date="{% if request.GET.filter_from_date %}{{request.GET.filter_from_date}}{% else %}{{ now }}{% endif %}" data-date-format="yyyy-mm-dd">
        <label for="from_date">À livrer après:</label>
        <div class="filter-choices">
          <input name="from_date" id="from_date" type="text" value="{{request.GET.filter_from_date}}" readonly><span class="add-on"><i class="icon-th"></i></span>
        </div>
      </div>
      <div id="to-date-datepicker" class="input-append date" data-date="{% if request.GET.filter_to_date %}{{request.GET.filter_to_date}}{% else %}{{ now }}{% endif %}" data-date-format="yyyy-mm-dd">
        <label for="to_date">À livrer avant:</label>
        <div class="filter-choices">
          <input name="to_date" id="to_date" type="text" value="{{request.GET.filter_to_date}}" readonly><span class="add-on"><i class="icon-th"></i></span>
        </div>
      </div>
		</form>
	</div>
</div>
{% endif %}
</div>
{% endif %}

{% if not paginator.count %}
<h1>Aucune demande trouvée</h1>
{% else %}
<p class="alert alert-info">
Pour obtenir des détails sur une demande, cliquer sur sa ligne.
</p>
<p>{{ paginator.count }} demande{% if paginator.count > 1 %}s{% endif %} trouvée{% if paginator.count > 1 %}s{% endif %} pour un coût de {{ total_cost|floatformat }} €</p>
<p><a href="csv/?{{request.GET.urlencode}}" rel="tooltip" title="exportez le résultat de votre recherche au format « tableur »">Export tableur</a></p>
{% with has_role_reprography=user.has_role_reprography %}
<table id="request-list-table" class="table table-bordered table-striped table-condensed">
    <thead>
        <tr>
            <th></th>
            <th><a href="{% sort_url 'id' %}">{% sort_indicator 'id' '↑' '↓' '' %}N&deg;</a></th>
            {% if has_role_reprography %}
            <th>Valideur</th>
            {% else %}
            <th><a href="{% sort_url 'user' %}">{% sort_indicator 'user' '↑' '↓' '' %}Demandeur (commanditaire)</a></th>
            {% endif %}
            <th><a href="{% sort_url 'entity__name' %}">{% sort_indicator 'entity__name' '↑' '↓' '' %}Service</a></th>
            <th><a href="{% sort_url 'nb_pages' %}" title="Nombre de pages du document">{% sort_indicator 'nb_pages' '↑' '↓' '' %}Nb. pages</a></th>
            <th><a href="{% sort_url 'copies' %}" title="Nombre d'exemplaires">{% sort_indicator 'copies' '↑' '↓' '' %}Nb. d'ex.</a></th>
            <th><a href="{% sort_url 'status' %}">{% sort_indicator 'status' '↑' '↓' '' %}ÉTAT</a></th>
            <th><a href="{% sort_url 'creation_date' %}" title="Date de création">{% sort_indicator 'creation_date' '↑' '↓' '' %}Création</a></th>
            <th><a href="{% sort_url 'delivery_date' %}" title="Date de livraison">{% sort_indicator 'delivery_date' '↑' '↓' '' %}Livraison</a></th>
            <th><a href="{% sort_url 'financial_code' %}">{% sort_indicator 'financial_code' '↑' '↓' '' %}Code d'imputation</a></th>
            <th><a href="{% sort_url 'cost' %}">{% sort_indicator 'cost' '↑' '↓' '' %}Coût</a></th>
        </tr>
    </thead>
    <tbody>
{% for request in requests %}
{% with doc=request.document id=request.id%}
    {% url 'request_detail' pk=request.pk as the_url %}   
    <tr class="request-row request-status-{{request.status.code}}" data-detail="{{the_url}}">
    <td class="see-button"><a href="{{the_url}}" class="btn"><strong>Voir</strong></a></td>
    <td class="number"><a href="{{the_url}}">{{ request.request_number }}</a></td>
    {% if has_role_reprography %}
    <td class="sponsor"><a href="{{the_url}}">{{ request.history_set.last_show_in_details.user.get_full_name }}</a></td>
    {% else %}
    <td class="sponsor"><a href="{{the_url}}">{{ request.sponsor_display|widont }}</a></td>
    {% endif %}
    <td class="entity"><a href="{{the_url}}">{% if request.entity %}{{ request.entity.get_description|title|widont }}{% else %}Aucune{% endif %}</a></td>
    <td class="pages"><a href="{{the_url}}">{{ request.nb_pages }}</a></td>
    <td class="copies"><a href="{{the_url}}">{{ request.copies}}</a></td>
    <td class="status-column status-{{ request.status|widont }}"><a href="{{the_url}}" {% if request.status.description %}rel="tooltip" title="{{request.status.description}}"{% endif %}>{{ request.status }}</a></td>
    <td class="creation-date"><a href="{{the_url}}">{{ request.creation_date|date:"d.m.y" }}</a></td>
    <td class="delivery-date"><a href="{{the_url}}">{% if request.delivery_date %}{{ request.delivery_date|date:"d.m.y" }}{% else %}Aucune{% endif %}</a></td>
    <td class="financial-code">{{ request.financial_code }}</td>
    <td class="cost">{% if request.cost %}{{ request.cost|floatformat:"-2" }}{% else %}&#8776;&nbsp;{{ request.estimated_cost|floatformat:"-2" }}{% endif %}&nbsp;€</td>
</tr>
{% endwith %}
{% endfor %}
</tbody>
<caption>
{% if is_paginated %}
  {% if page_obj.has_previous %}
    <span class="page-link"><a href="{% param_url "page" page_obj.previous_page_number %}">précédents</a></span> |
  {% endif %}
  <span class="page-current">{{ page_obj.number }}</span>
  {% if page_obj.has_next%}
        | <span class="page-link"><a href="{% param_url "page" page_obj.next_page_number %}">suivants</a></span>
  {% endif %}
{% endif %}
</caption>
</table>
{% endwith %}
{% endif %}

{% addtoblock "endscripts" %}
<script type="text/javascript" src="{{ STATIC_URL }}list_request.js"></script>
{% endaddtoblock %}

{% endblock %}
