{% extends "company/company_base.html" %}
{% load static %}
{% load i18n %}
{% load inventree_extras %}

{% block sidebar %}
{% include 'company/sidebar.html' %}
{% endblock sidebar %}

{% block page_content %}

<div class='panel panel-hidden' id='panel-supplier-parts'>
    <div class='panel-heading'>
        <div class='d-flex flex-wrap'>
            <h4>{% trans "Supplier Parts" %}</h4>
            {% include "spacer.html" %}
            <div class='btn-group' role='group'>
                {% if roles.purchase_order.add and not part.virtual %}
                    <button class="btn btn-success" id='supplier-part-create' title='{% trans "Create new supplier part" %}'>
                        <span class='fas fa-plus-circle'></span> {% trans "New Supplier Part" %}
                    </button>
                {% endif %}
            </div>
        </div>
    </div>
    <div class='panel-content'>
        {% if roles.purchase_order.change %}
        <div id='supplier-part-button-toolbar'>
            {% include "filter_list.html" with id="supplier-part" %}
        </div>
        {% endif %}

        <table class='table table-striped table-condensed' id='supplier-part-table' data-toolbar='#supplier-part-button-toolbar'>
        </table>
    </div>
</div>

<div class='panel panel-hidden' id='panel-manufacturer-parts'>
    <div class='panel-heading'>
        <div class='d-flex flex-wrap'>
            <h4>{% trans "Manufacturer Parts" %}</h4>
            {% include "spacer.html" %}
            <div class='btn-group' role='group'>
                {% if roles.purchase_order.add and not part.virtual %}
                    <button type="button" class="btn btn-success" id='manufacturer-part-create' title='{% trans "Create new manufacturer part" %}'>
                        <span class='fas fa-plus-circle'></span> {% trans "New Manufacturer Part" %}
                    </button>
                {% endif %}
            </div>
        </div>
    </div>
    <div class='panel-content'>
        {% if roles.purchase_order.change %}
        <div id='manufacturer-part-button-toolbar'>
            {% include "filter_list.html" with id="manufacturer-part" %}
        </div>
        {% endif %}
        <table class='table table-striped table-condensed' id='manufacturer-part-table' data-toolbar='#manufacturer-part-button-toolbar'>
        </table>
    </div>
</div>

<div class='panel panel-hidden' id='panel-company-stock'>
    <div class='panel-heading'>
        <h4>{% trans "Supplier Stock" %}</h4>
    </div>
    <div class='panel-content'>
        {% include "stock_table.html" %}
    </div>
</div>

<div class='panel panel-hidden' id='panel-purchase-orders'>
    <div class='panel-heading'>
        <div class='d-flex flex-wrap'>
            <h4>{% trans "Purchase Orders" %}</h4>
            {% include "spacer.html" %}
            <div class='btn-group' role='group'>
                {% if roles.purchase_order.add %}
                <button class='btn btn-success' type='button' id='company-order2' title='{% trans "Create new purchase order" %}'>
                    <span class='fas fa-plus-circle'></span> {% trans "New Purchase Order" %}
                </button>
                {% endif %}
            </div>
        </div>
    </div>
    <div class='panel-content'>
        <div id='po-button-bar'>
            {% include "filter_list.html" with id="purchaseorder" %}
        </div>

        <table class='table table-striped table-condensed po-table' id='purchase-order-table' data-toolbar='#po-button-bar'>
        </table>
    </div>
</div>

{% if company.is_customer %}
{% if roles.sales_order.view %}
<div class='panel panel-hidden' id='panel-sales-orders'>
    <div class='panel-heading'>
        <div class='d-flex flex-wrap'>
            <h4>{% trans "Sales Orders" %}</h4>
            {% include "spacer.html" %}
            <div class='btn-group' role='group'>
                {% if roles.sales_order.add %}
                <button class='btn btn-success' type='button' id='new-sales-order' title='{% trans "Create new sales order" %}'>
                    <div class='fas fa-plus-circle'></div> {% trans "New Sales Order" %}
                </button>
                {% endif %}
            </div>
        </div>
    </div>
    <div class='panel-content'>
        <div id='so-button-bar'>
            {% include "filter_list.html" with id="salesorder" %}
        </div>

        <table class='table table-striped table-condensed po-table' id='sales-order-table' data-toolbar='#so-button-bar'>
        </table>
    </div>
</div>
{% endif %}

{% if roles.stock.view %}
<div class='panel panel-hidden' id='panel-assigned-stock'>
    <div class='panel-heading'>
        <h4>{% trans "Assigned Stock" %}</h4>
    </div>
    <div class='panel-content'>
        <div id='assigned-stock-button-toolbar'>
            {% include "filter_list.html" with id="customerstock" %}
        </div>

        <table class='table table-striped table-condensed' id='assigned-stock-table' data-toolbar='#assigned-stock-button-toolbar'></table>
    </div>
</div>
{% endif %}

{% if roles.return_order.view and return_order_enabled %}
<div class='panel panel-hidden' id='panel-return-orders'>
    <div class='panel-heading'>
        <div class='d-flex flex-wrap'>
            <h4>{% trans "Return Orders" %}</h4>
            {% include "spacer.html" %}
            <div class='btn-group' role='group'>
                {% if roles.return_order.add %}
                <button class='btn btn-success' type='button' id='new-return-order' title='{% trans "Create new return order" %}'>
                    <div class='fas fa-plus-circle'></div> {% trans "New Return Order" %}
                </button>
                {% endif %}
            </div>
        </div>
    </div>
    <div class='panel-content'>
        <div id='table-buttons'>
            {% include "filter_list.html" with id="returnorder" %}
        </div>
        <table class='table table-striped table-condensed' data-toolbar='#table-buttons' id='return-order-table'>
        </table>
    </div>
</div>
{% endif %}

{% endif %}

<div class='panel panel-hidden' id='panel-company-notes'>
    <div class='panel-heading'>
        <div class='d-flex flex-wrap'>
            <h4>{% trans "Company Notes" %}</h4>
            {% include "spacer.html" %}
            <div class='btn-group' role='group'>
                {% include "notes_buttons.html" %}
            </div>
        </div>
    </div>
    <div class='panel-content'>
        <textarea id='company-notes'></textarea>
    </div>
</div>

<div class='panel panel-hidden' id='panel-company-contacts'>
    <div class='panel-heading'>
        <div class='d-flex flex-wrap'>
            <h4>{% trans "Company Contacts" %}</h4>
            {% include "spacer.html" %}
            <div class='btn-group' role='group'>
                {% if roles.purchase_order.add or roles.sales_order.add %}
                <button class='btn btn-success' type='button' id='new-contact' title='{% trans "Add Contact" %}'>
                    <div class='fas fa-plus-circle'></div> {% trans "Add Contact" %}
                </button>
                {% endif %}
            </div>
        </div>
    </div>
    <div class='panel-content'>
        <div id='contacts-button-toolbar'>
            {% include "filter_list.html" with id="contacts" %}
        </div>

        <table class='table table-striped table-condensed' id='contacts-table' data-toolbar='#contacts-button-toolbar'></table>
    </div>
</div>

<div class='panel panel-hidden' id='panel-company-addresses'>
    <div class='panel-heading'>
        <div class='d-flex flex-wrap'>
            <h4>{% trans "Company addresses" %}</h4>
            {% include "spacer.html" %}
            <div class='btn-group' role='group'>
                {% if roles.purchase_order.add or roles.sales_order.add %}
                <button class='btn btn-success' type='button' id='new-address' title='{% trans "Add Address" %}'>
                    <div class='fas fa-plus-circle'></div> {% trans "Add Address" %}
                </button>
                {% endif %}
            </div>
        </div>
    </div>
    <div class='panel-content'>
        <div id='addresses-button-toolbar'>
            {% include "filter_list.html" with id="addresses" %}
        </div>

        <table class='table table-striped table-condensed' id='addresses-table' data-toolbar='#addresses-button-toolbar'></table>
    </div>
</div>

<div class='panel panel-hidden' id='panel-attachments'>
    <div class='panel-heading'>
        <div class='d-flex flex-wrap'>
            <h4>{% trans "Attachments" %}</h4>
            {% include "spacer.html" %}
            <div class='btn-group' role='group'>
                {% include "attachment_button.html" %}
            </div>
        </div>
    </div>
    <div class='panel-content'>
        {% include "attachment_table.html" %}
    </div>
</div>

{% endblock page_content %}

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

    onPanelLoad("attachments", function() {
        loadAttachmentTable('{% url "api-company-attachment-list" %}', {
            filters: {
                company: {{ company.pk }},
            },
            fields: {
                company: {
                    value: {{ company.pk }},
                    hidden: true
                }
            }
        });
    });

    // Callback function when the 'contacts' panel is loaded
    onPanelLoad('company-contacts', function() {
        loadContactTable('#contacts-table', {
            params: {
                company: {{ company.pk }},
            },
            allow_edit: {% js_bool roles.purchase_order.change %} || {% js_bool roles.sales_order.change %},
            allow_delete: {% js_bool roles.purchase_order.delete %} || {% js_bool roles.sales_order.delete %},
        });

        $('#new-contact').click(function() {
            createContact({
                company: {{ company.pk }},
                onSuccess: function() {
                    $('#contacts-table').bootstrapTable('refresh');
                }
            });
        });
    });

    // Callback function for when the 'addresses' panel is loaded
    onPanelLoad('company-addresses', function(){
        loadAddressTable('#addresses-table', {
            params: {
                company: {{ company.pk }},
            },
            allow_edit: {% js_bool roles.purchase_order.change %} || {% js_bool roles.sales_order.change %},
            allow_delete: {% js_bool roles.purchase_order.delete %} || {% js_bool roles.sales_order.delete %},
        });

        $('#new-address').click(function() {
            createAddress({
                company: {{ company.pk }},
                onSuccess: function() {
                    $('#addresses-table').bootstrapTable('refresh');
                }
            })
        })
    })

    // Callback function when the 'notes' panel is loaded
    onPanelLoad('company-notes', function() {

        setupNotesField(
            'company-notes',
            '{% url "api-company-detail" company.pk %}',
            {
                editable: true,
            }
        );
    });

    onPanelLoad('company-stock', function() {

        loadStockTable($('#stock-table'), {
            url: "{% url 'api-stock-list' %}",
            params: {
                company: {{ company.id }},
                part_detail: true,
                supplier_part_detail: true,
                location_detail: true,
            },
            filterKey: "companystock",
        });
    });

    {% if company.is_customer %}

    {% if return_order_enabled %}
    // Callback function when the 'return orders' panel is loaded
    onPanelLoad('return-orders', function() {

        {% if roles.return_order.view %}
        loadReturnOrderTable('#return-order-table', {
            params: {
                customer: {{ company.pk }},
            }
        });
        {% endif %}

        {% if roles.return_order.add %}
        $('#new-return-order').click(function() {
            createReturnOrder({
                customer: {{ company.pk }},
            });
        });
        {% endif %}
    });
    {% endif %}

    // Callback function when the 'assigned stock' panel is loaded
    onPanelLoad('assigned-stock', function() {

        {% if roles.stock.view %}
        loadStockTable($("#assigned-stock-table"), {
            params: {
                customer: {{ company.id }},
                part_detail: true,
                location_detail: true,
            },
            url: "{% url 'api-stock-list' %}",
            filterKey: "customerstock",
            filterTarget: '#filter-list-customerstock',
        });
        {% endif %}
    });

    // Callback function when the 'sales orders' panel is loaded
    onPanelLoad('sales-orders', function() {
        {% if roles.sales_order.view %}
        loadSalesOrderTable("#sales-order-table", {
            url: "{% url 'api-so-list' %}",
            params: {
                customer: {{ company.id }},
            }
        });
        {% endif %}

        {% if roles.sales_order.add %}
        $("#new-sales-order").click(function() {

            createSalesOrder({
                customer: {{ company.pk }},
            });
        });
        {% endif %}
    });
    {% endif %}

    {% if company.is_supplier %}
    loadPurchaseOrderTable("#purchase-order-table", {
        url: "{% url 'api-po-list' %}",
        params: {
            supplier: {{ company.id }},
        }
    });

    function newOrder() {
        createPurchaseOrder({
            supplier: {{ company.pk }},
        });
    }

    $("#company-order").click(function() {
        newOrder();
    });

    $("#company-order2").click(function() {
        newOrder();
    });

    {% endif %}

    {% if company.is_manufacturer %}

    function reloadManufacturerPartTable() {
        $('#manufacturer-part-table').bootstrapTable('refresh');
    }

    $("#manufacturer-part-create").click(function () {

        createManufacturerPart({
            manufacturer: {{ company.pk }},
            onSuccess: function() {
                $("#part-table").bootstrapTable('refresh');
            }
        });
    });

    loadManufacturerPartTable(
        "#manufacturer-part-table",
        "{% url 'api-manufacturer-part-list' %}",
        {
            params: {
                part_detail: true,
                manufacturer_detail: true,
                manufacturer: {{ company.id }},
            },
        }
    );

    {% endif %}

    {% if company.is_supplier %}

    function reloadSupplierPartTable() {
        $('#supplier-part-table').bootstrapTable('refresh');
    }

    $("#supplier-part-create").click(function () {

        createSupplierPart({
            supplier: {{ company.pk }},
            onSuccess: reloadSupplierPartTable,
        });
    });

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

    enableSidebar('company');

{% endblock js_ready %}
