{% extends "base.html" %}

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

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

{% block breadcrumb_list %}
{% endblock breadcrumb_list %}

{% block content %}

<div class='panel panel-inventree'>
    <div class='panel-content'>
        {% include "search_form.html" with query_text=query %}
    </div>
</div>

<div id='detail-panels'>
</div>

{% endblock content %}

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

    function addItemTitle(title) {
        addSidebarHeader({
            text: title,
        });
    }

    var search_text = sanitizeInputString("{{ query|escapejs }}");

    function addItem(label, title, icon, options) {

        // Construct a "badge" to add to the sidebar item
        var badge = `
        <span id='sidebar-badge-${label}' class='sidebar-item-badge badge rounded-pill badge-right bg-dark'>
            <span class='fas fa-spin fa-spinner'></span>
        </span>
        `;

        addSidebarItem({
            label: label,
            text: title,
            icon: icon,
            content_after: badge
        });

        // Add a results table
        $('#detail-panels').append(
            `<div class='panel panel-inventree panel-hidden' id='panel-${label}'>
                <div class='panel-heading'>
                    <h4>${title}</h4>
                </div>
                <div class='panel-content'>
                    <table class='table table-condensed table-striped' id='table-${label}'></table>
                </div>
            </div>`
        );

        // Connect a callback to the table
        $(`#table-${label}`).on('load-success.bs.table', function() {
            var count = $(`#table-${label}`).bootstrapTable('getData').length;

            var badge = $(`#sidebar-badge-${label}`);

            badge.html(count);

            if (count > 0) {
                badge.removeClass('bg-dark');
                badge.addClass('bg-primary');
            }
        });
    }

    {% if roles.part.view %}
    addItemTitle('{% trans "Part" escape %}');

    addItem('part', '{% trans "Parts" escape %}', 'fa-shapes');

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

    addItem('category', '{% trans "Part Categories" escape %}', 'fa-sitemap');

    loadPartCategoryTable($("#table-category"), {
        params: {
            original_search: search_text,
        }
    });

    addItem('manufacturer-part', '{% trans "Manufacturer Parts" escape %}', 'fa-toolbox');

    loadManufacturerPartTable(
        "#table-manufacturer-part",
        "{% url 'api-manufacturer-part-list' %}",
        {
            params: {
                original_search: search_text,
                part_detail: true,
                supplier_detail: true,
                manufacturer_detail: true
            },
        }
    );

    addItem('supplier-part', '{% trans "Supplier Parts" escape %}', 'fa-pallet');

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

    {% endif %}

    {% if roles.build.view %}

    addItemTitle('{% trans "Build" escape %}');

    addItem('build-order', '{% trans "Build Orders" escape %}', 'fa-tools');

    loadBuildTable('#table-build-order', {
        locale: '{{ request.LANGUAGE_CODE }}',
        params: {
            original_search: search_text,
        }
    });

    {% endif %}

    {% if roles.stock.view %}
    addItemTitle('{% trans "Stock" escape %}');

    addItem('stock', '{% trans "Stock Items" escape %}', 'fa-boxes');

    loadStockTable($('#table-stock'), {
        filterKey: 'stocksearch',
        params: {
            original_search: search_text,
            part_detail: true,
            location_detail: true
        }
    });

    addItem('location', '{% trans "Stock Locations" escape %}', 'fa-map-marker-alt');

    loadStockLocationTable($("#table-location"), {
        filterKey: 'locationsearch',
        params: {
            original_search: search_text,
        },
    });

    {% endif %}

    {% if roles.purchase_order.view or roles.sales_order.view %}
    addItemTitle('{% trans "Company" escape %}');

    addItem('manufacturer', '{% trans "Manufacturers" escape %}', 'fa-industry');

    loadCompanyTable('#table-manufacturer', "{% url 'api-company-list' %}", {
        params: {
            original_search: search_text,
            is_manufacturer: true,
        }
    });

    {% if roles.purchase_order.view %}
    addItem('supplier', '{% trans "Suppliers" escape %}', 'fa-building');

    loadCompanyTable('#table-supplier', "{% url 'api-company-list' %}", {
        params: {
            original_search: search_text,
            is_supplier: true,
        }
    });

    addItem('purchase-order', '{% trans "Purchase Orders" escape %}', 'fa-shopping-cart');

    loadPurchaseOrderTable('#table-purchase-order', {
        params: {
            original_search: search_text,
        }
    });

    {% endif %}

    {% if roles.sales_order.view %}
    addItem('customer', '{% trans "Customers" escape %}', 'fa-user-tie');

    loadCompanyTable('#table-customer', "{% url 'api-company-list' %}", {
        params: {
            original_search: search_text,
            is_customer: true,
        }
    });

    addItem('sales-orders', '{% trans "Sales Orders" escape %}', 'fa-truck');

    loadSalesOrderTable('#table-sales-orders', {
        params: {
            original_search: search_text,
        }
    });

    {% endif %}

    {% endif %}

    enableSidebar(
        'search',
        {
            hide_toggle: true,
        }
    );

{% endblock js_ready %}
