{% extends "page_base.html" %}

{% load static %}
{% load i18n %}
{% load generic %}
{% load inventree_extras %}

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

{% 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 alt="{% trans "Part thumbnail" %}" 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 heading %}

{% block actions %}
<!-- Admin Display -->
{% admin_url user "build.build" build.pk as url %}
{% include "admin_button.html" with url=url %}

{% if barcodes %}
<!-- Barcode actions menu -->
<div class='btn-group' role='group'>
    <button id='barcode-options' title='{% trans "Barcode actions" %}' class='btn btn-outline-secondary dropdown-toggle' type='button' data-bs-toggle='dropdown'>
        <span class='fas fa-qrcode'></span> <span class='caret'></span>
    </button>
    <ul class='dropdown-menu' role='menu'>
        <li><a class='dropdown-item' href='#' id='show-qr-code'><span class='fas fa-qrcode'></span> {% trans "Show QR Code" %}</a></li>
        {% if roles.build.change %}
        {% if order.barcode_hash %}
        <li><a class='dropdown-item' href='#' id='barcode-unlink'><span class='fas fa-unlink'></span> {% trans "Unlink Barcode" %}</a></li>
        {% else %}
        <li><a class='dropdown-item' href='#' id='barcode-link'><span class='fas fa-link'></span> {% trans "Link Barcode" %}</a></li>
        {% endif %}
        {% endif %}
    </ul>
</div>
{% 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 roles.build.add %}
        <li><a class='dropdown-item' href='#' id='build-duplicate'><span class='fas fa-clone'></span> {% trans "Duplicate 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>
    {% include "project_code_data.html" with instance=build %}
    {% include "barcode_data.html" with instance=build %}
</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.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 details %}

{% 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>
            {% status_label 'build' 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>
            {% if build.completed >= build.quantity %}
            <span class='fas fa-check-circle icon-green'></span>
            {% else %}
            <span class='fa fa-times-circle icon-red'></span>
            {% endif %}
        <td>{% trans "Completed Outputs" %}</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 %}
    {% if build.priority != 0 %}
    <tr>
        <td><span class="fa-solid fa-arrow-up-9-1"></span></td>
        <td>{% trans "Priority" %}</td>
        <td>{{ build.priority }}</td>
    </tr>
    {% endif %}
</table>
{% endblock details_right %}

{% block page_data %}
<h3>
    {% status_label 'build' 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 page_data %}

{% block js_ready %}

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

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

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

    $("#build-complete").on('click', function() {
        completeBuildOrder({{ build.pk }});
    });
    {% endif %}

    {% if roles.build.add %}
    $('#build-duplicate').click(function() {
        duplicateBuildOrder({{ build.pk }});
    });
    {% endif %}

    {% if report_enabled %}
    $('#print-build-report').click(function() {
        printReports({
            items: [{{ build.pk }}],
            key: 'build',
            url: '{% url "api-build-report-list" %}',
        });
    });
    {% endif %}

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

    {% if barcodes %}
    <!-- Barcode functionality callbacks -->
    $('#show-qr-code').click(function() {
        showQRDialog(
            '{% trans "Build Order QR Code" escape %}',
            '{"build": {{ build.pk }} }'
        );
    });

    {% if roles.purchase_order.change %}
    $("#barcode-link").click(function() {
        linkBarcodeDialog(
            {
                build: {{ build.pk }},
            },
            {
                title: '{% trans "Link Barcode to Build Order" escape %}',
            }
        );
    });

    $("#barcode-unlink").click(function() {
        unlinkBarcode({
            build: {{ build.pk }},
        });
    });
    {% endif %}
    {% endif %}

{% endblock js_ready %}
