{% extends "base.html" %}

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

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

{% block breadcrumb_list %}
{% endblock %}

{% 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 %}

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

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

    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" %}');

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

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

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

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

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

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

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

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

    {% endif %}

    {% if roles.build.view %}

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

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

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

    {% endif %}

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

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

    loadStockTable($('#table-stock'), {
        filterKey: 'stocksearch',
        url: "{% url 'api-stock-list' %}",
        params: {
            original_search: "{{ query }}",
            part_detail: true,
            location_detail: true
        }
    });

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

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

    {% endif %}

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

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

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

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

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

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

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

    {% endif %}

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

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

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

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

    {% endif %}

    {% endif %}

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

{% endblock %}
