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

{% block body_class %}{{ block.super }} 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 %}
    <div class="page-header">
        <h1>{% trans "Order management" %}</h1>
    </div>
{% endblock header %}

{% block dashboard_content %}
    <div class="table-header">
        <h3><i class="icon-search icon-large"></i>{% trans "Search" %}</h3>
    </div>
    <div class="well">
        <form action="." method="get" class="form-inline" id="search_form">
            {% 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="error-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 orders %}
        <form action="." method="post" class="order_table" id="orders_form">
            {% csrf_token %}
            {% include "dashboard/orders/partials/bulk_edit_form.html" with status=active_status %}

            {% block order_list %}
            <table class="table table-striped table-bordered table-hover">
                <caption>
                    <h3 class="pull-left"><i class="icon-shopping-cart icon-large"></i>{{ queryset_description }}</h3>
                    <div class="pull-right">
                        <div class="form-inline">
                            <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" name="download_selected">{% trans "Download" %}</button>
                        </div>
                    </div>
                </caption>

                <thead>
                    <tr>
                        <th>{% trans "Select all" %}</th>
                        <th>{% anchor 'number' _("Order number") %}</th>
                        <th>{% anchor 'total_incl_tax' _("Total inc tax") %}</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>{% trans "Date of purchase" %}</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                {% for order in orders %}
                    <tr>
                        <td><input type="checkbox" name="selected_order" class="selected_order" value="{{ order.id }}"/></td>
                        <td><a href="{% url 'dashboard:order-detail' number=order.number %}">{{ order.number }}</a></td>
                        <td>{{ order.total_incl_tax|currency:order.currency }}</td>
                        <td>{{ order.num_items }}</td>
                        <td>{{ order.status|default:"-" }}</td>
                        <td>
                            {% if order.guest_email %}
                                {{ order.guest_email }}
                            {% elif order.user %}
                                <a href="{% url 'dashboard:user-detail' pk=order.user.id %}">{{ order.user.get_full_name|default:"-" }}</a>
                            {% else %}
                                &lt;{% trans "Deleted" %}&gt;
                            {% endif %}
                        </td>
                        <td>{{ order.shipping_address|default:"-" }}</td>
                        <td>{{ order.billing_address|default:"-" }}</td>
                        <td>{{ order.date_placed }}</td>
                        <td>
                            <a class="btn btn-info" href="{% url 'dashboard:order-detail' number=order.number %}">{% trans "View" %}</a>
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
            {% endblock order_list %}

            {% include "dashboard/orders/partials/bulk_edit_form.html" with status=active_status %}
            {% include "partials/pagination.html" %}
        </form>
    {% else %}
        <table class="table table-striped table-bordered">
            <caption><i class="icon-shopping-cart icon-large"></i>{{ queryset_description }}</caption>
            <tr><td>{% trans "No orders found." %}</td></tr>
        </table>
    {% endif %}

    {% endblock dashboard_content %}

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