{% extends "InvenTree/settings/settings.html" %}

{% block tabs %}
{% include "InvenTree/settings/tabs.html" with tab='part' %}
{% endblock %}

{% block settings %}
<h4>Part Parameter Templates</h4>

<div id='param-buttons'>
    <button class='btn btn-success' id='new-param'>New Parameter</button>
</div>

<table class='table table-striped table-condensed' id='param-table' data-toolbar='#param-buttons'>
</table>

{% endblock %}

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

    $("#param-table").bootstrapTable({
        url: "{% url 'api-part-param-template-list' %}",
        queryParams: {
            ordering: 'name',
        },
        sortable: true,
        search: true,
        pagination: true,
        pageSize: 25,
        formatNoMatches: function() { return "No part parameter templates found"; },
        columns: [
            {
                field: 'pk',
                title: 'ID',
                visible: false,
            },
            {
                field: 'name',
                title: 'Name',
                sortable: 'true',
            },
            {
                field: 'units',
                title: 'Units',
                sortable: 'true',
            },
            {
                formatter: function(value, row, index, field) {
                    var bEdit = "<button title='Edit Template' class='template-edit btn btn-default btn-glyph' type='button' pk='" + row.pk + "'><span class='glyphicon glyphicon-edit'></span></button>";
                    var bDel = "<button title='Delete Template' class='template-delete btn btn-default btn-glyph' type='button' pk='" + row.pk + "'><span class='glyphicon glyphicon-trash'></span></button>";

                    var html = "<div class='btn-group' role='group'>" + bEdit + bDel + "</div>";

                    return html;
                }
            }
        ]
    });

    $("#new-param").click(function() {
        launchModalForm("{% url 'part-param-template-create' %}", {
            success: function() {
                $("#param-table").bootstrapTable('refresh');
            },
        });
    });

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

        var url = "/part/parameter/template/" + button.attr('pk') + "/edit/";

        launchModalForm(url, {
            success: function() {
                $("#param-table").bootstrapTable('refresh');
            }
        });
    });

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

        var url = "/part/parameter/template/" + button.attr('pk') + "/delete/";
        
        launchModalForm(url, {
            success: function() {
                $("#param-table").bootstrapTable('refresh');
            }
        });
    });

{% endblock %}