{% extends "base.html" %}

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

{% block page_title %}
InvenTree | {% trans "Search Results" %}
{% endblock %}

{% block content %}

<h3>{% trans "Search Results" %}</h3>

<div>
{% include "search_form.html" with query_text=query %}
</div>

<br><br>
<hr>

{% if query %}

<div id='no-search-results'>
<h4><i>{% trans "No results found for " %}'{{ query }}'</i></h4>
</div>

{% include "InvenTree/search_part_category.html" with collapse_id="categories" %}

{% include "InvenTree/search_parts.html" with collapse_id='parts' %}

{% include "InvenTree/search_company.html" with collapse_id='companies' %}

{% include "InvenTree/search_supplier_parts.html" with collapse_id='supplier_parts' %}

{% include "InvenTree/search_stock_location.html" with collapse_id="locations" %}

{% include "InvenTree/search_stock_items.html" with collapse_id="stock" %}

{% else %}
    
<div id='empty-search-query'>
<h4><i>{% trans "Enter a search query" %}</i></h4>
</div>

{% endif %}

{% endblock %}

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

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

    function onSearchResults(table, output) {

        $(table).on('load-success.bs.table', function() {

            var panel = $(output).closest('.panel-group');

            var n = $(table).bootstrapTable('getData').length;

            var text = '';
            if (n == 0) {
                text = '<i>No results</i>'

                $(panel).hide();

            } else {
                text = n + ' result';

                if (n > 1) {
                    text += 's';
                }

                $(panel).show();

                var collapse = panel.find('.panel-collapse');

                collapse.collapse('show');

                $("#no-search-results").hide();
            }

            $(output).html(`<i>${text}</i>`);
        });
    }

    onSearchResults("#category-results-table", "#category-results-count");
    
    onSearchResults("#location-results-table", "#location-results-count");
    
    onSearchResults("#stock-results-table", "#stock-results-count");
    
    onSearchResults('#part-results-table', '#part-result-count');

    onSearchResults('#company-results-table', '#company-result-count');
    
    onSearchResults('#supplier-part-results-table', '#supplier-part-result-count');

    $("#category-results-table").inventreeTable({
        url: "{% url 'api-part-category-list' %}",
        queryParams: {
            search: "{{ query }}",
        },
        columns: [
            {
                field: 'name',
                title: 'Name',
                formatter: function(value, row, index, field) {
                    return renderLink(value, '/part/category/' + row.pk + '/');
                },
            },
            {
                field: 'description',
                title: 'Description',
            },
        ],
    });

    $('#stock-results-table').inventreeTable({
        url: "{% url 'api-stock-list' %}",
        queryParams: {
            search: "{{ query }}",
            part_detail: true,
            location_detail: true,
        },
        columns: [
            {
                field: 'part',
                title: "{% trans "Part" %}",
                sortable: true,
                formatter: function(value, row) {
                    var url = `/stock/item/${row.pk}/`;
                    var thumb = row.part_detail.thumbnail;
                    var name = row.part_detail.full_name;

                    html = imageHoverIcon(thumb) + renderLink(name, url);
                    
                    return html;
                }
            },
            {
                field: 'part_description',
                title: '{% trans "Description" %}',
                sortable: true,
                formatter: function(value, row, index, field) {
                    return row.part_detail.description;
                }
            },
            {
                field: 'quantity',
                title: '{% trans "Stock" %}',
                sortable: true,
                formatter: function(value, row, index, field) {

                    var val = parseFloat(value);

                    // If there is a single unit with a serial number, use the serial number
                    if (row.serial && row.quantity == 1) {
                        val = '# ' + row.serial;
                    } else {
                        val = +val.toFixed(5);
                    }

                    var html = renderLink(val, `/stock/item/${row.pk}/`);

                    return html;
                }
            },
            {
                field: 'status',
                title: '{% trans "Status" %}',
                sortable: 'true',
                formatter: function(value, row, index, field) {
                    return stockStatusDisplay(value);
                },
            },
            {
                field: 'location_detail.pathstring',
                title: '{% trans "Location" %}',
                sortable: true,
                formatter: function(value, row, index, field) {
                    if (value) {
                        return renderLink(value, `/stock/location/${row.location}/`);
                    }
                    else {
                        if (row.customer) {
                            var text = "{% trans "Shipped to customer" %}";
                            return renderLink(text, `/company/${row.customer}/assigned-stock/`);
                        } else {
                            return '<i>{% trans "No stock location set" %}</i>';
                        }
                    }
                }
            },
        ]
    });


    $("#location-results-table").inventreeTable({
        url: "{% url 'api-location-list' %}",
        queryParams: {
            search: "{{ query }}",
        },
        columns: [
            {
                field: 'name',
                title: 'Name',
                formatter: function(value, row, index, field) {
                    return renderLink(row.pathstring, '/stock/location/' + row.pk + '/');
                },
            },
            {
                field: 'description',
                title: 'Description',
            },
        ],
    });


    loadPartTable("#part-results-table",
        "{% url 'api-part-list' %}",
        {
            params: {
                search: "{{ query }}",
            },
            checkbox: false,
            disableFilters: true,
        }
    );
    

    loadCompanyTable('#company-results-table', "{% url 'api-company-list' %}", {
        params: {
            search: "{{ query }}",
        }
    });

    loadSupplierPartTable(
        "#supplier-part-results-table",
        "{% url 'api-supplier-part-list' %}",
        {
            params: {
                search: "{{ query }}",
                part_detail: true,
                supplier_detail: true,
                manufacturer_detail: true
            },
        }
    );

{% endblock %}