{% extends "base.html" %}
{% load i18n %}
{% block page_title %}
InvenTree | {% trans "Index" %}
{% endblock %}

{% block content %}
<h3>InvenTree</h3>
<hr>

<div class='col-sm-6'>
    {% if roles.part.view %}
    {% include "InvenTree/latest_parts.html" with collapse_id="latest_parts" %}
    {% include "InvenTree/bom_invalid.html" with collapse_id="bom_invalid" %}
    {% include "InvenTree/starred_parts.html" with collapse_id="starred" %}
    {% endif %}
    {% if roles.build.view %}
    {% include "InvenTree/build_pending.html" with collapse_id="build_pending" %}
    {% endif %}
</div>
<div class='col-sm-6'>
    {% if roles.stock.view %}
    {% include "InvenTree/low_stock.html" with collapse_id="order" %}
    {% include "InvenTree/required_stock_build.html" with collapse_id="stock_to_build" %}
    {% endif %}
    {% if roles.purchase_order.view %}
    {% include "InvenTree/po_outstanding.html" with collapse_id="po_outstanding" %}
    {% endif %}
    {% if roles.sales_order.view %}
    {% include "InvenTree/so_outstanding.html" with collapse_id="so_outstanding" %}
    {% endif %}
</div>

{% endblock %}

{% block js_load %}
{{ block.super }}
{% endblock %}

{% block js_ready %}

{{ block.super }}

loadSimplePartTable("#latest-parts-table", "{% url 'api-part-list' %}", {
    params: {
        ordering: "-creation_date",
        limit: 10,
    },
    name: 'latest_parts',
});

loadSimplePartTable("#starred-parts-table", "{% url 'api-part-list' %}", {
    params: {
        "starred": true,
    },
    name: 'starred_parts',
});

loadSimplePartTable("#bom-invalid-table", "{% url 'api-part-list' %}", {
    params: {
        "bom_valid": false,
    },
    name: 'bom_invalid_parts',
});

loadBuildTable("#build-pending-table", {
    url: "{% url 'api-build-list' %}",
    params: {
        part_detail: true,
        active: true,
    },
    disableFilters: true,
});

loadSimplePartTable("#low-stock-table", "{% url 'api-part-list' %}", {
    params: {
        low_stock: true,
    },
    name: "low_stock_parts",
});

loadSimplePartTable("#stock-to-build-table", "{% url 'api-part-list' %}", {
    params: {
        stock_to_build: true,
    },
    name: "to_build_parts",
});

loadPurchaseOrderTable("#po-outstanding-table", {
    url: "{% url 'api-po-list' %}",
    params: {
        supplier_detail: true,
        outstanding: true,
    }
});

loadSalesOrderTable("#so-outstanding-table", {
    url: "{% url 'api-so-list' %}",
    params: {
        customer_detail: true,
        outstanding: true,
    },
});

$("#latest-parts-table").on('load-success.bs.table', function() {
    var count = $("#latest-parts-table").bootstrapTable('getData').length;

    $("#latest-parts-count").html(count);
});

$("#starred-parts-table").on('load-success.bs.table', function() {
    var count = $("#starred-parts-table").bootstrapTable('getData').length;

    $("#starred-parts-count").html(count);
});

$("#bom-invalid-table").on('load-success.bs.table', function() {
    var count = $("#bom-invalid-table").bootstrapTable('getData').length;

    $("#bom-invalid-count").html(count);
});

$("#build-pending-table").on('load-success.bs.table', function() {
    var count = $("#build-pending-table").bootstrapTable('getData').length;

    $("#build-pending-count").html(count);
});

$("#low-stock-table").on('load-success.bs.table', function() {
    var count = $("#low-stock-table").bootstrapTable('getData').length;

    $("#low-stock-count").html(count);
});

$("#stock-to-build-table").on('load-success.bs.table', function() {
    var count = $("#stock-to-build-table").bootstrapTable('getData').length;

    $("#stock-to-build-count").html(count);
});

$("#po-outstanding-table").on('load-success.bs.table', function() {
    var count = $("#po-outstanding-table").bootstrapTable('getData').length;

    $("#po-outstanding-count").html(count);
});

$("#so-outstanding-table").on('load-success.bs.table', function() {
    var count = $("#so-outstanding-table").bootstrapTable('getData').length;

    $("#so-outstanding-count").html(count);
});

{% endblock %}