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

{% block page_title %}
InvenTree | Allocate Parts
{% endblock %}

{% block details %}

{% include "build/tabs.html" with tab='allocate' %}

<h4>{% trans "Incomplete Build Ouputs" %}</h4>

<hr>

{% if build.is_complete %}
<div class='alert alert-block alert-success'>
    {% trans "Build order has been completed" %}
</div>
{% else %}
<div class='btn-group' role='group'>
    {% if build.active %}
    <button class='btn btn-primary' type='button' id='btn-create-output' title='{% trans "Create new build output" %}'>
        <span class='fas fa-plus-circle'></span> {% trans "Create New Output" %}
    </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>
    -->
    <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>
    {% endif %}
</div>

<hr>
{% if build.incomplete_outputs %}
<div class="panel-group" id="build-output-accordion" role="tablist" aria-multiselectable="true">
    {% for item in build.incomplete_outputs %}
    {% include "build/allocation_card.html" with item=item %}
    {% endfor %}
</div>
{% else %}
<div class='alert alert-block alert-info'>
    <b>{% trans "Create a new build output" %}</b><br>
    {% trans "No incomplete build outputs remain." %}<br>
    {% trans "Create a new build output using the button above" %}
</div>
{% endif %}

{% endif %} 

{% endblock %}

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

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

    {% for item in build.incomplete_outputs %}
    // Get the build output as a javascript object
    inventreeGet('{% url 'api-stock-detail' item.pk %}', {},
        {
            success: function(response) {
                loadBuildOutputAllocationTable(buildInfo, response);
            }
        }
    );
    {% endfor %}

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

    $('#btn-create-output').click(function() {
        launchModalForm('{% url "build-output-create" build.id %}',
            {
                reload: true,
            }
        );
    });
        
    $("#btn-order-parts").click(function() {
        launchModalForm("/order/purchase-order/order-parts/", {
            data: {
                build: {{ build.id }},
            },
        });
    });
        
    {% endif %}

{% endblock %}
        