{% extends "two_column.html" %}

{% load i18n %}
{% load static %}
{% load inventree_extras %}
{% load status_codes %}

{% block page_title %}
InvenTree | {% trans "Purchase Order" %}
{% endblock %}

{% block thumbnail %}
<img class='part-thumb'
{% if order.supplier.image %}
src="{{ order.supplier.image.url }}"
{% else %}
src="{% static 'img/blank_image.png' %}"
{% endif %}
/>
{% endblock %}

{% block page_data %}
<h3>
    {% trans "Purchase Order" %} {{ order.reference }}
    {% if user.is_staff and roles.purchase_order.change %}
    <a href="{% url 'admin:order_purchaseorder_change' order.pk %}"><span title='{% trans "Admin view" %}' class='fas fa-user-shield'></span></a>
    {% endif %}
</h3>
<h3>{% purchase_order_status_label order.status large=True %}</h3>
<hr>
<p>{{ order.description }}</p>
<div class='btn-row'>
    <div class='btn-group action-buttons'>
        {% if roles.purchase_order.change %}
        <button type='button' class='btn btn-default' id='edit-order' title='{% trans "Edit order information" %}'>
            <span class='fas fa-edit icon-green'></span>
        </button>
        {% if order.status == PurchaseOrderStatus.PENDING and order.lines.count > 0 %}
        <button type='button' class='btn btn-default' id='place-order' title='{% trans "Place order" %}'>
            <span class='fas fa-paper-plane icon-blue'></span>
        </button>
        {% elif order.status == PurchaseOrderStatus.PLACED %}
        <button type='button' class='btn btn-default' id='receive-order' title='{% trans "Receive items" %}'>
            <span class='fas fa-clipboard-check'></span>
        </button>
        <button type='button' class='btn btn-default' id='complete-order' title='{% trans "Mark order as complete" %}'>
            <span class='fas fa-check-circle'></span>
        </button>
        {% endif %}
        {% if order.status == PurchaseOrderStatus.PENDING or order.status == PurchaseOrderStatus.PLACED %}
        <button type='button' class='btn btn-default' id='cancel-order' title='{% trans "Cancel order" %}'>
            <span class='fas fa-times-circle icon-red'></span>
        </button>
        {% endif %}
        {% endif %}
        <button type='button' class='btn btn-default' id='export-order' title='{% trans "Export order to file" %}'>
            <span class='fas fa-file-download'></span>
        </button>
    </div>
</div>
{% endblock %}

{% block page_details %}
<h4>{% trans "Purchase Order Details" %}</h4>
<table class='table'>
    <col width='25'>
    <tr>
        <td><span class='fas fa-hashtag'></span></td>
        <td>{% trans "Order Reference" %}</td>
        <td>{{ order.reference }}</td>
    </tr>
    <tr>
        <td><span class='fas fa-info'></span></td>
        <td>{% trans "Order Status" %}</td>
        <td>{% purchase_order_status_label order.status %}</td>
    </tr>
    <tr>
        <td><span class='fas fa-building'></span></td>
        <td>{% trans "Supplier" %}</td>
        <td><a href="{% url 'company-detail' order.supplier.id %}">{{ order.supplier.name }}</a></td>
    </tr>
    {% if order.supplier_reference %}
    <tr>
        <td><span class='fas fa-hashtag'></span></td>
        <td>{% trans "Supplier Reference" %}</td>
        <td>{{ order.supplier_reference }}</td>
    </tr>
    {% endif %}
    {% if order.link %}
    <tr>
        <td><span class='fas fa-link'></span></td>
        <td>External Link</td>
        <td><a href="{{ order.link }}">{{ order.link }}</a></td>
    </tr>
    {% endif %}
    <tr>
        <td><span class='fas fa-calendar-alt'></span></td>
        <td>{% trans "Created" %}</td>
        <td>{{ order.creation_date }}<span class='badge'>{{ order.created_by }}</span></td>
    </tr>
    {% if order.issue_date %}
    <tr>
        <td><span class='fas fa-calendar-alt'></span></td>
        <td>{% trans "Issued" %}</td>
        <td>{{ order.issue_date }}</td>
    </tr>
    {% endif %}
    {% if order.status == PurchaseOrderStatus.COMPLETE %}
    <tr>
        <td><span class='fas fa-calendar-alt'></span></td>
        <td>{% trans "Received" %}</td>
        <td>{{ order.complete_date }}<span class='badge'>{{ order.received_by }}</span></td>
    </tr>
    {% endif %}
</table>
{% endblock %}

{% block js_ready %}
{{ block.super }}

{% if order.status == PurchaseOrderStatus.PENDING and order.lines.count > 0 %}
$("#place-order").click(function() {
    launchModalForm("{% url 'po-issue' order.id %}",
    {
        reload: true,
    });
});
{% endif %}

$("#edit-order").click(function() {
    launchModalForm("{% url 'po-edit' order.id %}",
        {
            reload: true,
        }
    );
});

$("#cancel-order").click(function() {
    launchModalForm("{% url 'po-cancel' order.id %}", {
        reload: true,
    });
});


{% endblock %}