{% extends "stock/stock_app_base.html" %}
{% load static %}
{% block content %}

<div class='row'>
<div class='col-sm-6'>
    {% if location %}
    <h3>{{ location.name }}</h3>
    <p>{{ location.description }}</p>
    {% else %}
    <h3>Stock</h3>
    <p>All stock items</p>
    {% endif %}
    <p>
        <div class='btn-group'>
            <button class='btn btn-default btn-glyph' id='location-create' title='Create new stock location'>
                <span class='glyphicon glyphicon-plus'/>
            </button>
            {% if location %}
            {% include "qr_button.html" %}
            <button class='btn btn-default btn-glyph' id='location-count' title='Count stock items'>
                <span class='glyphicon glyphicon-ok-circle'/>
            </button>
            <button class='btn btn-default btn-glyph' id='location-edit' title='Edit stock location'>
                <span class='glyphicon glyphicon-edit'/>
            </button>
            <button class='btn btn-default btn-glyph' id='location-delete' title='Delete stock location'>
                <span class='glyphicon glyphicon-trash'/>
            </button>
            {% endif %}
        </div>
    </p>
</div>
<div class='col-sm-6'>
</div>
</h3>
</div>
</div>

{% if location and location.children.all|length > 0 %}
{% include 'stock/location_list.html' with children=location.children.all collapse_id="locations" %}
{% elif locations|length > 0 %}
{% include 'stock/location_list.html' with children=locations collapse_id="locations" %}
{% endif %}

<hr>

{% include "stock_table.html" %}

{% endblock %}

{% block js_load %}
{{ block.super }}
{% endblock %}
{% block js_ready %}
{{ block.super }}
    $('#location-create').click(function () {
        launchModalForm("{% url 'stock-location-create' %}",
                        {
                            data: {
                                {% if location %}
                                location: {{ location.id }}
                                {% endif %}
                            },
                            follow: true,
                            secondary: [
                                {
                                    field: 'parent',
                                    label: 'New Location',
                                    title: 'Create new location',
                                    url: "{% url 'stock-location-create' %}",
                                },
                            ]
                        });
        return false;
    });

    {% if location %}
    $('#location-edit').click(function() {
        launchModalForm("{% url 'stock-location-edit' location.id %}",
                        {
                            reload: true
                        });
        return false;
    });

    $('#location-delete').click(function() {
        launchModalForm("{% url 'stock-location-delete' location.id %}",
                         {
                             redirect: "{% url 'stock-index' %}"
                         });
        return false;
    });

    {% if location %}
    $("#location-count").click(function() {
        launchModalForm("/stock/adjust/", {
            data: {
                action: "count",
                location: {{ location.id }},
                reload: true,
            }
        });
    });
    {% endif %}

    $('#show-qr-code').click(function() {
        launchModalForm("{% url 'stock-location-qr' location.id %}",
        {
            no_post: true,
        });
    });

    {% endif %}

    $('#item-create').click(function () {
        launchModalForm("{% url 'stock-item-create' %}",
                        {
                            success: function() {
                                $("#stock-table").bootstrapTable('refresh');
                            },
                            data: {
                                {% if location %}
                                location: {{ location.id }}
                                {% endif %}
                            },
                            secondary: [
                                {
                                    field: 'part',
                                    label: 'New Part',
                                    title: 'Create New Part',
                                    url: "{% url 'part-create' %}",
                                },
                                {
                                    field: 'supplier_part',
                                    label: 'New Supplier Part',
                                    title: 'Create new Supplier Part',
                                    url: "{% url 'supplier-part-create' %}"
                                },
                                {
                                    field: 'location',
                                    label: 'New Location',
                                    title: 'Create New Location',
                                    url: "{% url 'stock-location-create' %}",
                                }
                            ]
                        });


        return false;
    });
    
    loadStockTable($("#stock-table"), {
        buttons: [
            '#stock-options',
        ],
        params: {
            {% if location %}
            location: {{ location.id }},
            {% endif %}
            part_detail: true,
            location_detail: true,
        },
        url: "{% url 'api-stock-list' %}",
    });
{% endblock %}
