{% extends "page_base.html" %}

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

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

{% block breadcrumbs %}
<li class='breadcrumb-item'><a href='{% url "build-index" %}'>{% trans "Build Orders" %}</a></li>
<li class="breadcrumb-item active" aria-current="page"><a href='{% url "build-detail" build.id %}'>{{ build }}</a></li>
{% endblock breadcrumbs %}

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

{% block heading %}
{% trans "Build Order" %} {{ build }}
{% endblock %}

{% block actions %}
<!-- Admin Display -->
{% if user.is_staff and roles.build.change %}
{% url 'admin:build_build_change' build.pk as url %}
{% include "admin_button.html" with url=url %}
{% endif %}
<!-- Printing options -->
{% if report_enabled %}
<div class='btn-group'>
    <button id='print-options' title='{% trans "Print actions" %}' class='btn btn-outline-secondary dropdown-toggle' type='button' data-bs-toggle='dropdown'>
        <span class='fas fa-print'></span> <span class='caret'></span>
    </button>
    <ul class='dropdown-menu' role='menu'>
        <li><a class='dropdown-item' href='#' id='print-build-report'><span class='fas fa-file-pdf'></span> {% trans "Print build order report" %}</a></li>
    </ul>
</div>
{% endif %}
<!-- Build actions -->
{% if roles.build.change %}
<div class='btn-group'>
    <button id='build-options' title='{% trans "Build actions" %}' class='btn btn-outline-secondary dropdown-toggle' type='button' data-bs-toggle='dropdown'>
        <span class='fas fa-tools'></span> <span class='caret'></span>
    </button>
    <ul class='dropdown-menu' role='menu'>
        <li><a class='dropdown-item' href='#' id='build-edit'><span class='fas fa-edit icon-green'></span> {% trans "Edit Build" %}</a></li>
        {% if build.is_active %}
        <li><a class='dropdown-item' 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 class='dropdown-item' href='#' id='build-delete'><span class='fas fa-trash-alt icon-red'></span> {% trans "Delete Build" %}</a>
        {% endif %}
    </ul>
</div>
{% if build.active %}
<button id='build-complete' title='{% trans "Complete Build" %}' class='btn btn-success'>
    <span class='fas fa-check-circle'></span> {% trans "Complete Build" %}
</button>
{% endif %}
{% endif %}
{% endblock actions %}

{% block details %}

<table class='table table-striped table-condensed'>
    <col width='25'>
    <tr>
        <td><span class='fas fa-shapes'></span></td>
        <td>{% trans "Part" %}</td>
        <td><a href="{% url 'part-detail' build.part.id %}?display=build-orders">{{ 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-circle'></span></td>
        <td>{% trans "Build Description" %}</td>
        <td>{{ build.title }}</td>
    </tr>
</table>

<div class='info-messages'>
    {% if not build.has_build_outputs %}
    <div class='alert alert-block alert-danger'>
        {% trans "No build outputs have been created for this build order" %}<br>
    </div>
    {% endif %}
    {% 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 %}

    {% 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.are_untracked_parts_allocated %}
    <div class='alert alert-block alert-warning'>
        {% trans "Stock has not been fully allocated to this Build Order" %}
    </div>
    {% endif %}
    {% endif %}
</div>
{% endblock %}

{% block details_right %}
<table class='table table-striped table-condensed'>
    <col width='25'>
    <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>
            {% render_date build.target_date %}
            {% if build.is_overdue %}
            <span title='{% blocktrans with target=build.target_date %}This build was due on {{target}}{% endblocktrans %}' class='badge badge-right rounded-pill bg-danger'>{% trans "Overdue" %}</span>
            {% endif %}
        </td>
    </tr>
    {% endif %}
    <tr>
        <td><span class='fas fa-check-circle'></span></td>
        <td>{% trans "Completed" %}</td>
        <td>{% progress_bar build.completed build.quantity id='build-completed' max_width='150px' %}</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 page_data %}
<h3>
    {% build_status_label build.status large=True %}
    {% if build.is_overdue %}
    <span class='badge rounded-pill bg-danger'>{% trans "Overdue" %}</span>
    {% endif %}
</h3>
<hr>
<p>{{ build.title }}</p>


{% endblock %}

{% block js_ready %}

    $("#build-edit").click(function () {
        editBuildOrder({{ build.pk }});
    });

    $("#build-cancel").click(function() {

        cancelBuildOrder(
            {{ build.pk }},
            {
                reload: true,
            }
        );
    });

    $("#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" %}',
            {
                alert_style: 'danger',
            }
        );
        {% else %}

        completeBuildOrder({{ build.pk }}, {
            overallocated: {% if build.has_overallocated_parts %}true{% else %}false{% endif %},
            allocated: {% if build.are_untracked_parts_allocated %}true{% else %}false{% endif %},
            completed: {% if build.remaining == 0 %}true{% else %}false{% endif %},
        });
        {% endif %}
    });

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

    $("#build-delete").on('click', function() {
        constructForm(
            '{% url "api-build-detail" build.pk %}',
            {
                method: 'DELETE',
                title: '{% trans "Delete Build Order" %}',
                redirect: "{% url 'build-index' %}",
            }
        );
    });

{% endblock %}
