{% extends "base.html" %}
<!-- Load internationalisation utils-->
{% load i18n %}
{% load static %}
{% load rules %}

{% block title %} {% trans 'Orders' %} {% endblock %}

{% block extrajs %}
<script type="application/javascript">
enableGeneratedOrdersCancelButton();
</script>
{% endblock %}

{% block content %}
{% has_perm 'sous_chef.edit' request.user as can_edit_data %}

<div class="ui secondary pointing fluid menu">
    <h1 class="ui header">{% trans "Orders" %}</h1>
</div>

<div class="ui padded basic segment">
    {% if can_edit_data %}
    <a href="{% url 'order:create_batch' %}" class="ui labeled icon right basic big button">
        <i class="add icon"></i>{% trans 'New orders' %}
    </a>
    <div class="ui horizontal divider">
        {% trans 'Or' %}
    </div>
    {% endif %}

    {% include 'order/partials/filters.html' %}
</div>

{% if orders %}
<table class="ui very basic stripped compact celled table">
  <thead>
    <th class="sorted descending">{% trans 'Order' %}
            <i class="help-text question grey icon link" data-content="{% trans 'A unique identifier for the order.' %}"></i>
    </th>
    <th class="">{% trans 'Client' %}
        <i class="help-text question grey icon link" data-content="{% trans 'Quickly access the client file.' %}"></i>
    </th>
    <th class="">{% trans 'Delivery Date' %}
        <i class="help-text question grey icon link" data-content="{% trans 'The delivery date planned for the order.' %}"></i>
    </th>
    <th class="center aligned">{% trans 'Status' %}
        <i class="help-text question grey icon link" data-content="{% trans 'Use the Status filter to filter this view.' %}"></i>
    </th>
    <th class="">{% trans 'Amount' %}
        <i class="help-text question grey icon link" data-content="{% trans 'Total amount in $CAD.' %}"></i>
    </th>
    <th class="">{% trans 'Actions' %}</th>
  </thead>
  <tbody>
    {% for order in orders %}
      <tr {% if order.status == 'O' and order.client_planned_status_at_delivery != 'A' %}class="error"{% endif %}>
        <td><strong><i class="hashtag icon"></i>{{order.id}}</strong></td>
        <td>
            <a href="{% url 'member:client_information' pk=order.client.id %}">{{order.client.member}}</a>
            {% if order.client_planned_status_at_delivery != 'A' %}
            ({{ order.client_planned_status_at_delivery_verbose }})
            {% endif %}
        </td>
        <td>{{order.delivery_date}}</td>
        <td class="center aligned">{{order.get_status_display}}</td>
        <td><i class="dollar icon"></i>{{order.price}}</td>
        <td>
          <a class="ui basic icon button"  href="{% url 'order:view' pk=order.id %}"><i class="icon unhide"></i></a>
          {% if can_edit_data %}
            <a class="ui basic icon button" title="{% trans 'Edit the order.' %}" href="{% url 'order:update' pk=order.id %}"><i class="icon edit"></i></a>
            {% if order.status == 'O' %}
            <a class="ui basic icon order cancel button" title="{% trans 'Cancel the order.' %}" data-url="{% url 'order:update_status' pk=order.id %}"><i class="icon ban"></i></a>
            {% endif %}
            <a class="ui basic icon button order-delete" title="{% trans 'Delete the order.' %}" href="#" data-order-id="{{order.id}}"><i class="icon trash"></i></a>
          {% endif %}
        </td>
      </tr>
      {% include "order_confirm_delete.html" %}
    {% endfor %}
  </tbody>
  <tfoot>
      <tr>
          <th></th>
          <th colspan="5">
              {% include "system/_button_export.html" %}
          </th>
        </tr>
  </tfoot>
</table>

{% if is_paginated %}

<div class="ui pagination menu" style="margin: 2em auto; width=100%;";>
    {% if page_obj.has_previous %}
    <a class="icon item"
       href="{{ request.path }}{{ get }}page={{ page_obj.previous_page_number }}">
        <i class="left arrow icon"></i>
    </a>
    {% endif %}
    <a class="active item">
        {{ page_obj.number }} {% trans "of" %} {{ page_obj.paginator.num_pages }}
    </a>
    {% if page_obj.has_next %}
    <a class="icon item"
       href="{{ request.path }}{{ get }}page={{ page_obj.next_page_number }}">
        <i class="right arrow icon"></i>
    </a>
    {% endif %}
</div>
{% endif %}


{% else %}
    <div class="ui row"><p>{% trans "Sorry, no result found" %}.</p></div>
{% endif %}

{# To display the cancel dialog. #}
{% if can_edit_data %}
  <div class="ui modal status"></div>
{% endif %}

{% endblock %}
