{% extends "build/build_base.html" %}
{% load static %}
{% load i18n %}
{% load inventree_extras %}

{% block page_title %}
{% inventree_title %} | {% trans "Allocate Parts" %}
{% endblock %}

{% block menubar %}
{% include "build/navbar.html" with tab='allocate' %}
{% endblock %}

{% block heading %}
{% trans "Allocate Stock to Build" %}
{% endblock %}

{% block details %}
{% if build.has_untracked_bom_items %}
{% if build.active %}
<div class='btn-group' role='group'>
    <button class='btn btn-success' type='button' id='btn-auto-allocate' title='{% trans "Allocate stock to build" %}'>
        <span class='fas fa-magic'></span> {% trans "Auto Allocate" %}
    </button>
    <button class='btn btn-danger' type='button' id='btn-unallocate' title='{% trans "Unallocate stock" %}'>
        <span class='fas fa-minus-circle'></span> {% trans "Unallocate Stock" %}
    </button>
    <!--
    <button class='btn btn-primary' type='button' id='btn-order-parts' title='{% trans "Order required parts" %}'>
        <span class='fas fa-shopping-cart'></span> {% trans "Order Parts" %}
    </button>
    -->
</div>
{% if build.areUntrackedPartsFullyAllocated %}
<div class='alert alert-block alert-success'>
    {% trans "Untracked stock has been fully allocated for this Build Order" %}
</div>
{% else %}
<div class='alert alert-block alert-danger'>
    {% trans "Untracked stock has not been fully allocated for this Build Order" %}
</div>
{% endif %}
{% endif %}
<table class='table table-striped table-condensed' id='allocation-table-untracked'></table>
{% else %}
<div class='alert alert-block alert-info'>
    {% trans "This Build Order does not have any associated untracked BOM items" %}
</div>
{% endif %}
{% endblock %}

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

    var buildInfo = {
        pk: {{ build.pk }},
        quantity: {{ build.quantity }},
        completed: {{ build.completed }},
        part: {{ build.part.pk }},
    };

    {% if build.has_untracked_bom_items %}
    // Load allocation table for un-tracked parts
    loadBuildOutputAllocationTable(buildInfo, null);
    {% endif %}

    function reloadTable() {
        $('#allocation-table-untracked').bootstrapTable('refresh');
    }

    {% if build.active %}
    $("#btn-auto-allocate").on('click', function() {
        launchModalForm(
            "{% url 'build-auto-allocate' build.id %}",
            {
                success: reloadTable,
            }
        );
    });

    $('#btn-unallocate').on('click', function() {
        launchModalForm(
            "{% url 'build-unallocate' build.id %}",
            {
                success: reloadTable,
            }
        );
    });
        
    $("#btn-order-parts").click(function() {
        launchModalForm("/order/purchase-order/order-parts/", {
            data: {
                build: {{ build.id }},
            },
        });
    });
        
    {% endif %}

{% endblock %}
        