{% extends 'dashboard/layout.html' %}
{% load currency_filters %}
{% load sorting_tags %}
{% load i18n %}

{% block body_class %}orders{% endblock %}
{% block title %}
{% trans "Order management" %} | {{ block.super }}
{% endblock %}

{% block breadcrumbs %}
<ul class="breadcrumb">
    <li>
	<a href="{% url dashboard:index %}">{% trans "Dashboard" %}</a>
        <span class="divider">/</span>
    </li>
	<li class="active">{% trans "Order management" %}</li>
</ul>
{% endblock %}

{% block header %}
{% endblock header %}

{% block dashboard_content %}

<div class="well well-info">
    <h3><i class="icon-search"></i> {% trans "Search" %}</h3>
    <form action="." method="get" class="form-inline">
        {% for field in form %}
            {% if "order" in field.id_for_label %}
                {% if field.is_hidden %}
                    {{ field }}
                {% else %}
                <span class="control-group {% if field.errors %}error{% endif %}">
                    {{ field.label_tag }}
                    {{ field }}
                    {% for error in field.errors %}
                        <ul class="help-block">
                            <li>{{ error }}</li>
                        </ul>
                    {% endfor %}
                </span>
                {% endif %}
            {% endif %}
        {% endfor %}
        <input type="submit" value="{% trans "Search" %}" class="btn btn-primary" />
        <a data-toggle="modal" href="#SearchModal">{% trans "Advanced Search" %}</a>
    </form>

    <div class="modal hide fade" id="SearchModal">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">×</button>
            <h3>{% trans "Advanced Search" %}</h3>
        </div>
        <form action="." method="get" class="form-horizontal">
            <div class="modal-body">
                {% csrf_token %}
                {% include "partials/form_fields.html" with form=form %}
            </div>
            <div class="modal-footer">
                <a href="#" class="btn" data-dismiss="modal">{% trans "Close" %}</a>
                <button type="submit" class="btn btn-primary">{% trans "Search" %}</button>
            </div>
        </form>
    </div>
</div>

{% if queryset_description != "All orders" %}
<div class="alert alert-info">
    <strong>{{ queryset_description }}</strong>
</div>
{% endif %}

<div class="tabbable dashboard">
    <ul class="nav nav-tabs">
        <li {% if not active_status %}class="active"{% endif %}>
            <a href="{% url dashboard:order-list %}">{% trans "All Orders" %}</a>
        </li>
        {% for slug, name in status_lookup.items %}
        <li {% if slug == active_status %}class="active"{% endif %}>
            <a href="{% url dashboard:order-list slug %}">{{ name }}</a>
        </li>
        {% endfor %}
    </ul>
</div>

{% if orders|length %}
<form action="." method="post" class="order_table">
    {% csrf_token %}
    {% include "dashboard/orders/partials/bulk_edit_form.html" with status=active_status %}

<table class="table table-striped table-bordered">
    <tr>
        <th>
			<input name="select_all_orders" type="checkbox">
            {% trans "Select all" %}
		</th>
        <th>{% anchor 'number' 'Order number' %}</th>
		<th>{% trans "Total inc tax" %}</th>
		<th>{% trans "Date of purchase" %}</th>
		<th>{% trans "Number of items" %}</th>
		<th>{% trans "Status" %}</th>
		<th>{% trans "Customer" %}</th>
		<th>{% trans "Shipping address" %}</th>
		<th>{% trans "Billing address" %}</th>
        <th></th>
    </tr>
    {% for order in orders %}
    <tr>
        <td><input type="checkbox" name="selected_order" class="selected_order" value="{{ order.id }}"/>
        <td>{{ order.number }}</td>
        <td>{{ order.total_incl_tax|currency }}</td>
		<td>{{ order.date_placed }}</td>
		<td>{{ order.num_items }}</td>
        <td>{{ order.status }}</td>
        <td>{{ order.email }}</td>
        <td>{{ order.shipping_address|default:"-" }}</td>
        <td>{{ order.billing_address|default:"-" }}</td>
        <td>
			<a class="btn btn-info" href="{% url dashboard:order-detail order.number %}">{% trans "View" %}</a>
        </td>
    </tr>
    {% endfor %}
</table>

<div class="well well-danger">
    <h3><i class="icon-download-alt"></i> {% trans "Download" %}</h3>
	<label>{% trans "Download selected orders as a CSV" %}</label>
	<input type="hidden" name="action" value="download_selected_orders" />
	<button type="submit" class="btn btn-primary">{% trans "Download" %}</button>
</div>

{% include "dashboard/orders/partials/bulk_edit_form.html" with status=active_status %}
{% include "partials/pagination.html" %}

</form>

{% else %}

<p>{% trans "No orders found." %}</p>

{% endif %}

{% endblock dashboard_content %}

{% block onbodyload %}
oscar.dashboard.orders.initTable();
oscar.dashboard.search.init();
{% endblock onbodyload %}
