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

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

{% block details %}

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

{% if editing %}
{% include "build/allocate_edit.html" %}
{% else %}
{% include "build/allocate_view.html" %}
{% endif %}

{% endblock %}

{% block js_load %}
{{ block.super }}
<script src="{% static 'script/inventree/part.js' %}"></script>
<script src="{% static 'script/inventree/build.js' %}"></script>
{% endblock %}

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

    {% if editing %}

    {% for bom_item in bom_items.all %}

    loadAllocationTable(
        $("#allocate-table-id-{{ bom_item.sub_part.id }}"),
        {{ bom_item.sub_part.id }},
        "{{ bom_item.sub_part.full_name }}",
        "{% url 'api-build-item-list' %}?build={{ build.id }}&part={{ bom_item.sub_part.id }}",
        {% multiply build.quantity bom_item.quantity %},
        $("#new-item-{{ bom_item.sub_part.id }}")
    );

    {% endfor %}

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

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

    {% else %}

    $("#build-list").bootstrapTable({
        search: true,
        sortable: true,
    });

    $("#btn-allocate").click(function() {
        location.href = "{% url 'build-allocate' build.id %}?edit=1";
    });

    $("#btn-order-parts").click(function() {
        launchModalForm("/order/purchase-order/order-parts/", {
            data: {
                build: {{ build.id }},
            },
        });
    });

    {% endif %}

{% endblock %}
