{% extends "admin/base_site.html" %}
{% load i18n l10n staticfiles admin_urls utils orders %}

{% block extrastyle %}
{{ block.super }}
<link rel="stylesheet" type="text/css" href="{% static "admin/css/forms.css" %}" />
<style type="text/css">
.account {
    float: right;
    margin-right: 400px;
}
</style>
{% endblock %}

{% block extrahead %}
{{ block.super }}
<script src="{% static "admin/js/jquery.min.js" %}" type="text/javascript"></script>
<script src="{% static "admin/js/jquery.init.js" %}" type="text/javascript"></script>
<script>
var $ = django.jQuery;

$(document).ready( function () {
  $('#id_select_all').click( function() {
    $(":checkbox").attr('checked', $(this).is(':checked'));
  });
});
</script>
{% endblock %}

{% block breadcrumbs %}
<div class="breadcrumbs">
<a href="{% url 'admin:index' %}">{% trans 'Home' %}</a>
&rsaquo; <a href="{% url 'admin:app_list' app_label='orders' %}">Orders</a>
&rsaquo; <a href="{% url 'admin:orders_order_changelist' %}">Order</a>
&rsaquo; {{ title }}
</div>
{% endblock %}


{% block content %}
    <form action="" method="post">{% csrf_token %}
    <div>
    <div style="margin:20px;">
        {% if bills %}
            {% for account, total, lines in bills %}
            <div class="inline-group" id="rates-group">
             <div class="tabular inline-related last-related">
            <fieldset class="module">
            <h2><a href="{% url 'admin:accounts_account_change' account.pk %}">{{ account }}</a><span style="float:right">{{ total | floatformat:"-2" }} &euro;</span></h2>
            {% if not lines %}
                <table>
                <thead>
                    <tr><th>{% trans 'Nothing to bill, all lines have size&times;quantity 0.' %}</th></tr>
                </thead>
                </table>
            {% else %}
                <table>
                <thead>
                    <tr><th style="width:30%;">Description</th> <th style="width:30%;">Period</th> <th style="width:10%;">Size&times;Quantity</th> <th style="width:10%;">Price</th></tr>
                </thead>
                <tbody>
                {% for line in lines %}
                <tr class="form-row {% if forloop.counter|divisibleby:2 %}row2{% else %}row1{% endif %}">
                    <td>
                        <a href="{{ line.order | admin_url }}">{{ line.order.description|safe }}</a>
                        {% for discount in line.discounts %}
                            <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Discount per {{ discount.type }}
                        {% endfor %}
                    </td>
                    <td>{{ line | periodformat }}</td>
                    <td>{{ line.size | floatformat:"-2" }}&times;{{ line.metric | floatformat:"-2"}}</td>
                    <td>
                        &nbsp;{{ line.subtotal | floatformat:"-2" }} &euro;
                        {% for discount in line.discounts %}<br>{{ discount.total | floatformat:"-2" }} &euro;{% endfor %}
                    </td>
                </tr>
                {% endfor %}
            {% endif %}
            </tbody>
            </table>
            </fieldset>
            </div>
            </div>
            {% endfor %}
            {{ form.as_table }}
        {% else %}
            {{ form.as_admin }}
        {% endif %}
    </div>
    {% for obj in queryset %}
    <input type="hidden" name="{{ action_checkbox_name }}" value="{{ obj.pk|unlocalize }}" />
    {% endfor %}
    <input type="hidden" name="action" value="bill_selected_orders" />
    <input type="hidden" name="step" value="{{ step }}" />
    <input type="submit" value="{% if step == 3 %}{% trans "Yes, create bills" %}{% else %}{% trans "Next" %}{% endif %}" />
    </div>
    </form>
{% endblock %}
