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

{% block body_class %}{{ block.super }} orders{% endblock %}

{% block title %}
  {% trans "Orders" %} | {{ block.super }}
{% endblock %}

{% block extrascripts %}
  {{ block.super }}

  {# Translation support for JavaScript strings. #}
  <script type="text/javascript" src="{% url 'javascript-catalog' %}"></script>

  {% compress js %}
      <script src="{% static 'bower_components/js-cookie/src/js.cookie.js' %}" type="text/javascript"></script>
      <script src="{% static 'vendor-extensions/oscar/js/add_message.js' %}" type="text/javascript"></script>
      <script src="{% static 'vendor-extensions/oscar/js/order_actions.js' %}" type="text/javascript"></script>
  {% endcompress %}
{% endblock %}

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

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

{% block dashboard_content %}
  {% include "dashboard/partials/search_form.html" %}

  {% 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>
                            <button type="submit" class="btn btn-primary" name="action"
                                    value="download_selected_orders">{% trans "Download" %}</button>
                        </div>
                    </div>
                </caption>

                <thead>
                <tr>
                    <th></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 "Username" %}</th>
                    <th>{% trans "Email" %}</th>
                    <th>{% trans "Date of purchase" %}</th>
                    <th></th>
                </tr>
                </thead>
                <tbody>
                {% for order in orders %}
                    <tr data-order-number="{{ order.number }}">
                        <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 class="order-status">{{ order.status|default:"-" }}</td>
                        <td>
                            {% if order.user %}
                                <a href="{% url 'dashboard:user-detail' pk=order.user.id %}">{{ order.user.username }}</a>
                            {% else %}
                                &lt;{% trans "Deleted" %}&gt;
                            {% endif %}
                        </td>
                        <td>
                          {% if order.user %}
                            <a href="{% url 'dashboard:user-detail' pk=order.user.id %}">{{ order.user.email }}</a>
                          {% else %}
                            &lt;{% trans "Deleted" %}&gt;
                          {% endif %}
                        </td>
                        <td>{{ order.date_placed }}</td>
                        <td>
                            <a class="btn btn-info"
                               href="{% url 'dashboard:order-detail' number=order.number %}">{% trans "View" %}</a>
                          {% if order.is_fulfillable %}
                              <a class="btn btn-warning" data-order-number="{{ order.number }}"
                                 data-action="retry-fulfillment">{% trans "Retry Fulfillment" %}</a>
                          {% endif %}
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
        {% endblock order_list %}

        {% block order_actions %}
            <div class="well">
                <h3><i class="icon-warning"></i> {% trans "Change order status" %}:</h3>
              {% if order_statuses %}
                  <div class="control-group">
                      <div class="controls">

                          <select name="new_status">
                              <option value=""> -- {% trans "choose new status" %} --</option>
                            {% for status in order_statuses %}
                                <option>{{ status }}</option>
                            {% endfor %}
                          </select>

                      </div>
                  </div>
                  <button type="submit" name="action" value="change_order_statuses"
                          class="btn btn-primary">{% trans "Change status" %}</button>
              {% else %}
                {% trans "This order can't have its status changed." %}
              {% endif %}
            </div>
        {% endblock %}


        {% 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 %}
