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

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

{% block content_panels %}

{% if not build.is_complete %}
<div class='panel panel-default panel-inventree'>
    <div class='panel-heading'>
        <h4>
            {% trans "Incomplete Build Outputs" %}
        </h4>
    </div>

    <div class='panel-content'>
        <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>
            {% endif %}
        </div>

        {% 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 tracked_items=build.has_tracked_bom_items %}
            {% 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 %}

    </div>
</div>
{% endif %}

<div class='panel panel-default panel-inventree'>
    <div class='panel-heading'>
        <h4>
            {% trans "Completed Build Outputs" %}
        </h4>
    </div>

    <div class='panel-content'>
        {% include "stock_table.html" with read_only=True %}
    </div>
</div>

{% endblock %}

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

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

loadStockTable($("#stock-table"), {
    params: {
        location_detail: true,
        part_detail: true,
        build: {{ build.id }},
    },
    groupByField: 'location',
    buttons: [
        '#stock-options',
    ],
    url: "{% url 'api-stock-list' %}",    
});

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 %}


{% endblock %}