{% 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"><a href=".">{% trans "Order management" %}</a></li>
</ul>
{% endblock %}

{% block header %}
<div class="page-header">
	<h1>{% trans "Order management" %}</h1>
</div>
{% endblock header %}

{% block dashboard_content %}

<div class="well well-info">
    <div class="sub-header">
		<h3>{% trans "Quick Order Search" %} &nbsp;&nbsp;<small><a href="#" class="app-ico ico_magnify pull_out">{% trans "Advanced Search" %}</a></small></h3>
    </div>
    <form action="." method="get" class="search-adv form-horizontal">
        {% for field in form %}
            {% if not forloop.last %}
                <div class="control-group {% for error in field.errors %}error{% endfor %} {% if forloop.first %}pull-left{% endif%}">
                    {% if field.is_hidden %}
                        {{ field }}
                    {% else %}
                    <label for="{{ field.auto_id }}" class="control-label">{{ field.label }}</label>
                    <div class="controls">
                        {{ field }}
                            {% for error in field.errors %}
                            <span class="help-block">
                                {{ error|escape }}
                            </span>
                            {% endfor %}
                        {% if field.help_text %}
                            <span class='help-block'>
                                {{ field.help_text }}
                            </span>
                        {% endif %}
                    </div>
                    {% endif %}
                </div>
            {% else%}
                <div class="control-group {% for error in form.response_format_0.errors %}error{% endfor %} pull-left">
					<label for="id_response_format_0" class="control-label">{% trans "Get results as:" %}</label>
                    <div class="controls">
                        <label class="radio inline control-label" for="id_response_format_0">
                            {{ form.response_format }}
                            {% for error in form.response_format.errors %}
                            <span class="help-block">
                                {{ error|escape }}
                            </span>
                            {% endfor %}
                        </label>
                    </div>
                </div>
            {% endif%}
            {% ifequal forloop.counter 1 %}<div class="orders_search" style="display:none">{% endifequal %}
            {% ifequal forloop.revcounter0 1 %}</div>{% endifequal %}
        {% endfor %}
        <button type="submit" class="btn btn-primary pull-left">Search</button>
    </form>
</div>

<div class="sub-header">
    <h2>{{ queryset_description }}</h2>
</div>
{% if orders.count %}
<form action="." method="post" class="order_table">
    {% csrf_token %}
<table class="table table-striped table-bordered">
    <tr>
        <th></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 class="app-ico ico_logout icon">{% 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>

{% if page_obj %}
    {% include "catalogue/partials/pagination.html" %}
{% endif %}

</form>

{% else %}

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

{% endif %}

{% endblock dashboard_content %}

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