{% extends "two_column.html" %}

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

{% block page_title %}
{% inventree_title %} | {% trans "Build Order" %} - {{ build }}
{% endblock %}

{% block header_pre_content %}
{% if build.sales_order %}
<div class='alert alert-block alert-info'>
    {% object_link 'so-detail' build.sales_order.id build.sales_order as link %}
    {% blocktrans %}This Build Order is allocated to Sales Order {{link}}{% endblocktrans %}
</div>
{% endif %}
{% if build.parent %}
<div class='alert alert-block alert-info'>
    {% object_link 'build-detail' build.parent.id build.parent as link %}
    {% blocktrans %}This Build Order is a child of Build Order {{link}}{% endblocktrans %}
</div>
{% endif %}
{% endblock %}

{% block header_post_content %}
{% if build.active %}
{% if build.can_complete %}
<div class='alert alert-block alert-success'>
    {% trans "Build Order is ready to mark as completed" %}
</div>
{% endif %}
{% if build.incomplete_count > 0 %}
<div class='alert alert-block alert-danger'>
    {% trans "Build Order cannot be completed as outstanding outputs remain" %}
</div>
{% endif %}
{% if build.completed < build.quantity %}
<div class='alert alert-block alert-warning'>
    {% trans "Required build quantity has not yet been completed" %}
</div>
{% endif %}
{% if not build.areUntrackedPartsFullyAllocated %}
<div class='alert alert-block alert-warning'>
    {% trans "Stock has not been fully allocated to this Build Order" %}
</div>
{% endif %}
{% endif %}
{% endblock %}

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

{% block page_data %}
<h3>
    {% trans "Build Order" %} {{ build }}
    {% if user.is_staff and roles.build.change %}
    <a  href="{% url 'admin:build_build_change' build.pk %}"><span title="{% trans 'Admin view' %}" class='fas fa-user-shield'></span></a>
    {% endif %}
</h3>
<h3>
    {% build_status_label build.status large=True %}
    {% if build.is_overdue %}
    <span class='label label-large label-large-red'>{% trans "Overdue" %}</span>
    {% endif %}
</h3>
<hr>
<p>{{ build.title }}</p>

<div class='btn-group action-buttons' role='group'>
    <!-- Printing options -->
    <div class='btn-group'>
        <button id='print-options' title='{% trans "Print actions" %}' class='btn btn-default dropdown-toggle' type='button' data-toggle='dropdown'>
            <span class='fas fa-print'></span> <span class='caret'></span>
        </button>
        <ul class='dropdown-menu' role='menu'>
            <li><a href='#' id='print-build-report'><span class='fas fa-file-pdf'></span> {% trans "Print Build Order" %}</a></li>
        </ul>
    </div>
    <!-- Build actions -->
    {% if roles.build.change %}
    {% if build.active %}
    <button id='build-complete' title='{% trans "Complete Build" %}' class='btn btn-success'>
        <span class='fas fa-paper-plane'></span>
    </button>
    {% endif %}
    <div class='btn-group'>
        <button id='build-options' title='{% trans "Build actions" %}' class='btn btn-default dropdown-toggle' type='button' data-toggle='dropdown'>
            <span class='fas fa-tools'></span> <span class='caret'></span>
        </button>
        <ul class='dropdown-menu' role='menu'>
            <li><a href='#' id='build-edit'><span class='fas fa-edit icon-green'></span> {% trans "Edit Build" %}</a></li>
            {% if build.is_active %}
            <li><a href='#' id='build-cancel'><span class='fas fa-times-circle icon-red'></span> {% trans "Cancel Build" %}</a></li>
            {% endif %}
            {% if build.status == BuildStatus.CANCELLED and roles.build.delete %}
            <li><a href='#' id='build-delete'><span class='fas fa-trash-alt'></span> {% trans "Delete Build"% }</a>
                {% endif %}
        </ul>
    </div>
    {% endif %}
</div>
{% endblock %}

{% block page_details %}

<h4>{% trans "Build Details" %}</h4>
<table class='table table-striped table-condensed'>
    <tr>
        <td><span class='fas fa-shapes'></span></td>
        <td>{% trans "Part" %}</td>
        <td><a href="{% url 'part-detail' build.part.id %}">{{ build.part.full_name }}</a></td>
    </tr>
    <tr>
        <td></td>
        <td>{% trans "Quantity" %}</td>
        <td>{{ build.quantity }}</td>
    </tr>
    <tr>
        <td><span class='fas fa-info'></span></td>
        <td>{% trans "Status" %}</td>
        <td>
            {% build_status_label build.status %}
        </td>
    </tr>
    {% if build.target_date %}
    <tr>
        <td><span class='fas fa-calendar-alt'></span></td>
        <td>{% trans "Target Date" %}</td>
        <td>
            {{ build.target_date }}
            {% if build.is_overdue %}
            <span title='{% blocktrans with target=build.target_date %}This build was due on {{target}}{% endblocktrans %}' class='label label-red'>{% trans "Overdue" %}</span>
            {% endif %}
        </td>
    </tr>
    {% endif %}
    <tr>
        <td><span class='fas fa-spinner'></span></td>
        <td>{% trans "Progress" %}</td>
        <td> {{ build.completed }} / {{ build.quantity }}</td>
    </tr>
    {% if build.parent %}
    <tr>
        <td><span class='fas fa-sitemap'></span></td>
        <td>{% trans "Parent Build" %}</td>
        <td><a href="{% url 'build-detail' build.parent.id %}">{{ build.parent }}</a></td>
    </tr>
    {% endif %}
    {% if build.sales_order %}
    <tr>
        <td><span class='fas fa-dolly'></span></td>
        <td>{% trans "Sales Order" %}</td>
        <td><a href="{% url 'so-detail' build.sales_order.id %}">{{ build.sales_order }}</a></td>
    </tr>
    {% endif %}
    {% if build.issued_by %}
    <tr>
        <td><span class='fas fa-user'></span></td>
        <td>{% trans "Issued By" %}</td>
        <td>{{ build.issued_by }}</td>
    </tr>
    {% endif %}
    {% if build.responsible %}
    <tr>
        <td><span class='fas fa-users'></span></td>
        <td>{% trans "Responsible" %}</td>
        <td>{{ build.responsible }}</td>
    </tr>
    {% endif %}
</table>
{% endblock %}

{% block js_ready %}

    enableNavbar({
        label: 'build',
        toggleId: '#build-menu-toggle'
    });

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

    $("#build-cancel").click(function() {
        launchModalForm("{% url 'build-cancel' build.id %}",
                        {
                            reload: true,
                            submit_text: '{% trans "Cancel Build" %}',
                        });
    });

    $("#build-complete").on('click', function() {

        {% if build.incomplete_count > 0 %}
        showAlertDialog(
            '{% trans "Incomplete Outputs" %}',
            '{% trans "Build Order cannot be completed as incomplete build outputs remain" %}'
        );
        {% else %}
        launchModalForm(
            "{% url 'build-complete' build.id %}",
            {
                reload: true,
                submit_text: '{% trans "Complete Build" %}',
            }
        );
        {% endif %}
    });

    $('#print-build-report').click(function() {
        printBuildReports([{{ build.pk }}]);
    });

    $("#build-delete").on('click', function() {
        launchModalForm(
            "{% url 'build-delete' build.id %}",
            {
                redirect: "{% url 'build-index' %}",
            }
        );
    });
    
{% endblock %}