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

{% block sidebar %}
{% include "build/sidebar.html" %}
{% endblock sidebar %}

{% block page_content %}

<div class='panel panel-hidden' id='panel-details'>
    <div class='panel-heading'>
        <h4>{% trans "Build Details" %}</h4>
    </div>
    <div class='panel-content'>
        <div class='row'>
            <div class='col-sm-6'>
                <table class='table table-striped table-condensed'>
                    <col width='25'>
                <tr>
                    <td><span class='fas fa-info'></span></td>
                    <td>{% trans "Description" %}</td>
                    <td>{{ build.title }}{% include "clip.html" %}</td>
                </tr>
                <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>{% include "clip.html" %}</td>
                </tr>
                <tr>
                    <td></td>
                    <td>{% trans "Quantity" %}</td><td>{{ build.quantity }}</td>
                </tr>
                <tr>
                    <td><span class='fas fa-map-marker-alt'></span></td>
                    <td>{% trans "Stock Source" %}</td>
                    <td>
                        {% if build.take_from %}
                        <a href="{% url 'stock-location-detail' build.take_from.id %}">{{ build.take_from }}</a>{% include "clip.html" %}
                        {% else %}
                        <em>{% trans "Stock can be taken from any available location." %}</em>
                        {% endif %}
                    </td>
                </tr>
                <tr>
                    <td><span class='fas fa-map-marker-alt'></span></td>
                    <td>{% trans "Destination" %}</td>
                    <td>
                        {% if build.destination %}
                        <a href="{% url 'stock-location-detail' build.destination.id %}">
                            {{ build.destination }}
                        </a>{% include "clip.html" %}
                        {% else %}
                        <em>{% trans "Destination location not specified" %}</em>
                        {% endif %}
                    </td>
                </tr>
                <tr>
                    <td><span class='fas fa-info'></span></td>
                    <td>{% trans "Status" %}</td>
                    <td>{% display_status_label 'build' build.status_custom_key build.status %}</td>
                </tr>
                <tr>
                    <td><span class='fas fa-check-circle'></span></td>
                    <td>{% trans "Completed Outputs" %}</td>
                    <td>{% progress_bar build.completed build.quantity id='build-completed-2' max_width='150px' %}</td>
                </tr>
                {% if build.active %}
                <tr>
                    <td><span class='fas fa-list'></span></td>
                    <td>{% trans "Allocated Parts" %}</td>
                    <td id='output-progress-untracked'><span class='fas fa-spinner fa-spin'></span></td>
                </tr>
                {% endif %}
                {% if build.batch %}
                <tr>
                    <td><span class='fas fa-layer-group'></span></td>
                    <td>{% trans "Batch" %}</td>
                    <td>{{ build.batch }}{% include "clip.html" %}</td>
                </tr>
                {% endif %}
                {% 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>{% include "clip.html" %}</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 %}
                {% 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>{% include "clip.html" %}</td>
                </tr>
                {% endif %}
                {% if build.link %}
                <tr>
                    <td><span class='fas fa-link'></span></td>
                    <td>{% trans "External Link" %}</td>
                    <td>{% include 'clip_link.html' with link=build.link new_window=True %}</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 %}
                </table>
            </div>
            <div class='col-sm-6'>
                <table class='table table-striped table-condensed'>
                    <col width='25'>
                    <tr>
                        <td><span class='fas fa-calendar-alt'></span></td>
                        <td>{% trans "Created" %}</td>
                        <td>{% render_date build.creation_date %}</td>
                    </tr>
                    <tr>
                        <td><span class='fas fa-calendar-alt'></span></td>
                        <td>{% trans "Target Date" %}</td>
                        {% if build.target_date %}
                        <td>
                            {% render_date build.target_date %}{% if build.is_overdue %} <span class='fas fa-calendar-times icon-red'></span>{% endif %}
                        </td>
                        {% else %}
                        <td><em>{% trans "No target date set" %}</em></td>
                        {% endif %}
                    </tr>
                    <tr>
                        <td><span class='fas fa-calendar-alt'></span></td>
                        <td>{% trans "Completed" %}</td>
                        {% if build.completion_date %}
                        <td>{% render_date build.completion_date %}{% if build.completed_by %}<span class='badge badge-right rounded-pill bg-dark'>{{ build.completed_by }}</span>{% endif %}</td>
                        {% else %}
                        <td><em>{% trans "Build not complete" %}</em></td>
                        {% endif %}
                    </tr>
                </table>
            </div>
        </div>
    </div>
</div>

<div class='panel panel-hidden' id='panel-children'>
    <div class='panel-heading'>
        <h4>{% trans "Child Build Orders" %}</h4>
    </div>
    <div class='panel-content'>
        <div id='child-button-toolbar'>
            {% include "filter_list.html" with id='sub-build' %}
        </div>
        <table class='table table-striped table-condensed' id='sub-build-table' data-toolbar='#child-button-toolbar'></table>
    </div>
</div>

<div class='panel panel-hidden' id='panel-allocate'>
    <div class='panel-heading'>
        <div class='d-flex flex-wrap'>
            <h4>{% trans "Build Order Line Items" %}</h4>
            {% include "spacer.html" %}
            <div class='btn-group' role='group'>
                {% if roles.build.add and build.active %}
                <button class='btn btn-danger' type='button' id='btn-unallocate' title='{% trans "Deallocate stock" %}'>
                    <span class='fas fa-minus-circle'></span> {% trans "Deallocate Stock" %}
                </button>
                <button class='btn btn-primary' type='button' id='btn-auto-allocate' title='{% trans "Automatically allocate stock to build" %}'>
                    <span class='fas fa-magic'></span> {% trans "Auto Allocate" %}
                </button>
                <button class='btn btn-success' type='button' id='btn-allocate' title='{% trans "Manually allocate stock to build" %}'>
                    <span class='fas fa-sign-in-alt'></span> {% trans "Allocate 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>
                -->
                {% endif %}
            </div>
        </div>
    </div>
    <div class='panel-content'>
        <div id='build-lines-toolbar'>
            {% include "filter_list.html" with id='buildlines' %}
        </div>
        {% if build.take_from %}
        <div class='alert alert-block alert-info'>
            {% trans "Available stock has been filtered based on specified source location for this build order" %}
        </div>
        {% endif %}
        <table class='table table-striped table-condensed' id='build-lines-table' data-toolbar='#build-lines-toolbar'></table>
    </div>
</div>

<div class='panel panel-hidden' id='panel-outputs'>
    <div class='panel-heading'>
        <div class='d-flex flex-wrap'>
            <h4>{% trans "Incomplete Build Outputs" %}</h4>
            {% include "spacer.html" %}
            <div class='btn-group' role='group'>
                {% if roles.build.add and build.active %}
                <button class='btn btn-success' type='button' id='btn-create-output' title='{% trans "Create new build output" %}'>
                    <span class='fas fa-plus-circle'></span> {% trans "New Build Output" %}
                </button>
                {% endif %}
            </div>
        </div>
    </div>
    <div class='panel-content'>
        <div id='build-output-toolbar'>
            {% include "filter_list.html" with id='incompletebuilditems' %}
        </div>
        <table class='table table-striped table-condensed' id='build-output-table' data-toolbar='#build-output-toolbar'></table>
    </div>
</div>

<div class='panel panel-hidden' id='panel-allocated'>
    <div class='panel-heading'>
        <h4>{% trans "Allocated Stock" %}</h4>
    </div>
    <div class='panel-content'>
        <div id='build-allocated-stock-toolbar'>
            {% include "filter_list.html" with id='buildorderallocatedstock' %}
        </div>
        <table class='table table-striped table-condensed' id='allocated-stock-table' data-toolbar='#build-allocated-stock-toolbar'></table>
    </div>
</div>

<div class='panel panel-hidden' id='panel-consumed'>
    <div class='panel-heading'>
        <h4>
            {% trans "Consumed Stock" %}
        </h4>
    </div>

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

<div class='panel panel-hidden' id='panel-completed'>
    <div class='panel-heading'>
        <h4>
            {% trans "Completed Build Outputs" %}
        </h4>
    </div>

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

<div class='panel panel-hidden' id='panel-test-statistics'>
    <div class='panel-heading'>
        <h4>
            {% trans "Build test statistics" %}
        </h4>
    </div>

    <div class='panel-content'>
        <div id='teststatistics-button-toolbar'>
            {% include "filter_list.html" with id="buildteststatistics" %}
        </div>
        {% include "test_statistics_table.html" with prefix="build-" %}
    </div>
</div>

<div class='panel panel-hidden' id='panel-attachments'>
    <div class='panel-heading'>
        <div class='d-flex flex-wrap'>
            <h4>{% trans "Attachments" %}</h4>
            {% include "spacer.html" %}
            <div class='btn-group' role='group'>
                {% include "attachment_button.html" %}
            </div>
        </div>
    </div>
    <div class='panel-content'>
        {% include "attachment_table.html" %}
    </div>
</div>

<div class='panel panel-hidden' id='panel-notes'>
    <div class='panel-heading'>
        <div class='d-flex flex-wrap'>
            <h4>{% trans "Build Notes" %}</h4>
            {% include "spacer.html" %}
            <div class='btn-group' role='group'>
                {% include "notes_buttons.html" %}
            </div>
        </div>
    </div>
    <div class='panel-content'>
        <textarea id='build-notes'></textarea>
    </div>
</div>

{% endblock page_content %}

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

onPanelLoad('allocated', function() {
    loadBuildOrderAllocatedStockTable($('#allocated-stock-table'), {{ build.pk }});
});

onPanelLoad('consumed', function() {
    loadStockTable($('#consumed-stock-table'), {
        filterTarget: '#filter-list-consumed-stock',
        params: {
            location_detail: true,
            part_detail: true,
            consumed_by: {{ build.pk }},
            in_stock: false,
        },
    });
});

onPanelLoad('completed', function() {
    loadStockTable($("#build-stock-table"), {
        filterTarget: '#filter-list-build-stock',
        params: {
            location_detail: true,
            part_detail: true,
            build: {{ build.id }},
            is_building: false,
        },
    });
});

onPanelLoad('children', function() {
    loadBuildTable($('#sub-build-table'), {
        locale: '{{ request.LANGUAGE_CODE }}',
        filterTarget: "#filter-list-sub-build",
        parentBuild: {{ build.pk }},
        params: {
            ancestor: {{ build.pk }},
        }
    });
});

onPanelLoad('attachments', function() {
    loadAttachmentTable('build', {{ build.pk }});
});

onPanelLoad('notes', function() {

    setupNotesField(
        'build-notes',
        '{% url "api-build-detail" build.pk %}',
        {
            model_type: 'build',
            model_id: {{ build.pk }},
            {% if roles.build.change %}
            editable: true,
            {% else %}
            editable: false,
            {% endif %}
        }
    );
});

onPanelLoad('outputs', function() {
    {% if build.active %}

    var build_info = {
        pk: {{ build.pk }},
        part: {{ build.part.pk }},
        quantity: {{ build.quantity }},
        {% if build.take_from %}
        source_location: {{ build.take_from.pk }},
        {% endif %}
        tracked_parts: true,
        testable: {% js_bool build.part.testable %},
        trackable: {% js_bool build.part.trackable %}
    };

    loadBuildOutputTable(build_info);

    {% endif %}
});

onPanelLoad('allocate', function() {
    // Load the table of line items for this build order
    loadBuildLineTable(
        "#build-lines-table",
        {{ build.pk }},
        {
            {% if build.take_from %}
            location: {{ build.take_from.pk }},
            {% endif %}
            {% if build.project_code %}
            project_code: {{ build.project_code.pk }},
            {% endif %}
        }
    );
});

$('#btn-create-output').click(function() {

    createBuildOutput(
        {{ build.pk }},
        {
            trackable_parts: {% js_bool build.part.has_trackable_parts %},
        }
    );
});

{% if build.active %}

$("#btn-auto-allocate").on('click', function() {

    autoAllocateStockToBuild(
        {{ build.pk }},
        [],
        {
            {% if build.take_from %}
            location: {{ build.take_from.pk }},
            {% endif %}
            onSuccess: function() {
                $('#build-lines-table').bootstrapTable('refresh');
            },
        }
    );
});

function allocateSelectedLines() {

    let data = getTableData('#build-lines-table');

    let unallocated_lines = [];

    data.forEach(function(line) {
        if (line.allocated < line.quantity) {
            unallocated_lines.push(line);
        }
    });

    if (unallocated_lines.length == 0) {
        showAlertDialog(
            '{% trans "Allocation Complete" escape %}',
            '{% trans "All lines have been fully allocated" escape %}',
        );
    } else {

        allocateStockToBuild(
            {{ build.pk }},
            unallocated_lines,
            {
                {% if build.take_from %}
                source_location: {{ build.take_from.pk }},
                {% endif %}
                success: function() {
                    $('#build-lines-table').bootstrapTable('refresh');
                },
            }
        );
    }
}

$('#btn-unallocate').on('click', function() {
    deallocateStock({{ build.id }}, {
        table: '#allocation-table-untracked',
        onSuccess: function() {
            $('#build-lines-table').bootstrapTable('refresh');
        },
    });
});

$("#btn-allocate").on('click', function() {
    allocateSelectedLines();
});

{% endif %}

enableSidebar('buildorder');

{% endblock js_ready %}
