{% extends "stock/item_base.html" %}

{% load static %}
{% load i18n %}

{% block details %}

{% include "stock/tabs.html" with tab='tests' %}

<h4>{% trans "Test Data" %}</h4>
<hr>

<div id='button-toolbar'>
    <div class='button-toolbar container-fluid' style="float: right;">
        <div class='btn-group' role='group'>
            {% if user.is_staff %}
            <button type='button' class='btn btn-danger' id='delete-test-results'>
                <span class='fas fa-trash-alt'></span> {% trans "Delete Test Data" %}
            </button>
            {% endif %}
            <button type='button' class='btn btn-success' id='add-test-result'>
                <span class='fas fa-plus-circle'></span> {% trans "Add Test Data" %}
            </button>
            <button type='button' class='btn btn-default' id='test-report'>
                <span class='fas fa-tasks'></span> {% trans "Test Report" %}
            </button>
        </div>
        <div class='filter-list' id='filter-list-stocktests'>
            <!-- Empty div -->
        </div>
    </div>
</div>
        
<table class='table table-striped table-condensed' data-toolbar='#button-toolbar' id='test-result-table'></table>                

{% endblock %}

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

loadStockTestResultsTable(
    $("#test-result-table"), {
        part: {{ item.part.id }},
        stock_item: {{ item.id }},
    }
);

function reloadTable() {
    location.reload();
    //$("#test-result-table").bootstrapTable("refresh");
}

{% if item.part.has_test_report_templates %}
$("#test-report").click(function() {
    launchModalForm(
        "{% url 'stock-item-test-report-select' item.id %}",
        {
            follow: true,
        }
    );
});
{% endif %}

{% if user.is_staff %}
$("#delete-test-results").click(function() {
    launchModalForm(
        "{% url 'stock-item-delete-test-data' item.id %}",
        {
            success: reloadTable,
        }
    );
});
{% endif %}

$("#add-test-result").click(function() {
    launchModalForm(
        "{% url 'stock-item-test-create' %}", {
            data: {
                stock_item: {{ item.id }},
            },
            success: reloadTable,
            focus: 'test',
        }
    );
});

$("#test-result-table").on('click', '.button-test-add', function() {
    var button = $(this);

    var test_name = button.attr('pk');

    launchModalForm(
        "{% url 'stock-item-test-create' %}", {
            data: {
                stock_item: {{ item.id }},
                test: test_name
            },
            success: reloadTable,
            focus: 'value',
        }
    );
});

$("#test-result-table").on('click', '.button-test-edit', function() {
    var button = $(this);

    var url = `/stock/item/test/${button.attr('pk')}/edit/`;

    launchModalForm(url, {
        success: reloadTable,
    });
});

$("#test-result-table").on('click', '.button-test-delete', function() {
    var button = $(this);

    var url = `/stock/item/test/${button.attr('pk')}/delete/`;

    launchModalForm(url, {
        success: reloadTable,
    });
});

{% endblock %}