{% extends "part/part_app_base.html" %}
{% load static %}
{% load i18n %}

{% block content %}

<div class='row'>
    <div class='col-sm-6'>
        {% if category %}
        <h3>
            {{ category.name }}
            {% if user.is_staff and roles.part.change %}
            <a href="{% url 'admin:part_partcategory_change' category.pk %}"><span title="{% trans 'Admin view' %}" class='fas fa-user-shield'></span></a>
            {% endif %}
        </h3>
        <p>{{ category.description }}</p>
        {% else %}
        <h3>{% trans "Part Categories" %}</h3>
        <p>{% trans "All parts" %}</p>
        {% endif %}
        <p>
            <div class='btn-group action-buttons'>
                {% if roles.part.add %}
                <button class='btn btn-default' id='cat-create' title='{% trans "Create new part category" %}'>
                    <span class='fas fa-plus-circle icon-green'/>
                </button>
                {% endif %}
                {% if category %}
                {% if roles.part.change %}
                <button class='btn btn-default' id='cat-edit' title='{% trans "Edit part category" %}'>
                    <span class='fas fa-edit icon-blue'/>
                </button>
                {% endif %}
                {% if roles.part.delete %}
                <button class='btn btn-default' id='cat-delete' title='{% trans "Delete part category" %}'>
                    <span class='fas fa-trash-alt icon-red'/>
                </button>
                {% endif %}
                {% endif %}
            </div>
        </p>
    </div>
    <div class='col-sm-6'>
        {% if category %}
        <h3>{% trans "Category Details" %}</h3>
        <table class='table table-condensed table-striped'>
            <col width='25'>
            <tr>
                <td><span class='fas fa-sitemap'></span></td>
                <td>{% trans "Category Path" %}</td>
                <td>{{ category.pathstring }}</td>
            </tr>
            <tr>
                <td><span class='fas fa-info-circle'></span></td>
                <td>{% trans "Category Description" %}</td>
                <td>{{ category.description }}</td>
            </tr>
            {% if category.default_location %}
            <tr>
                <td><span class='fas fa-map-marker-alt'></span></td>
                <td>{% trans "Default Location" %}</td>
                <td><a href="{% url 'stock-location-detail' category.default_location.pk %}">{{ category.default_location.pathstring }}</a></td>
            </tr>
            {% endif %}
            {% if category.default_keywords %}
            <tr>
                <td><span class='fas fa-key'></span></td>
                <td>{% trans "Keywords" %}</td>
                <td>{{ category.default_keywords }}</td>
            </tr>
            {% endif %}
            <tr>
                <td><span class='fas fa-sitemap'></span></td>
                <td>{% trans "Subcategories" %}</td>
                <td>{{ category.children.count }}</td>
            </tr>
            <tr>
                <td><span class='fas fa-shapes'></span></td>
                <td>{% trans "Parts (Including subcategories)" %}</td>
                <td>{{ category.partcount }}</td>
            </tr>
        </table>
        {% else %}
        <h3>{% trans "Category Details" %}</h3>
        <table class='table table-striped table-condensed'>
            <col width='25'>
            <tr>
                <td><span class='fas fa-sitemap'></span></td>
                <td>{% trans "Part Categories" %}</td>
                <td>{{ category_count }}</td>
            </tr>
            <tr>
                <td><span class='fas fa-shapes'></span></td>
                <td>{% trans "Parts" %}</td>
                <td>{{ part_count }}</td>
            </tr>
        </table>
        {% endif %}
    </div>
</div>

{% if category and category.children.all|length > 0 %}
{% include "part/subcategories.html" with children=category.children.all collapse_id="categories" %}
{% elif children|length > 0 %}
{% include "part/subcategories.html" with children=children collapse_id="categories" %}
{% endif %}
<hr>

<div id='button-toolbar'>
    <div class='btn-group'>
        <button class='btn btn-default' id='part-export' title='{% trans "Export Part Data" %}'>
            <span class='fas fa-file-download'></span> {% trans "Export" %}
        </button>
        {% if roles.part.add %}
        <button class='btn btn-success' id='part-create' title='{% trans "Create new part" %}'>
            <span class='fas fa-plus-circle'></span> {% trans "New Part" %}
        </button>
        {% endif %}
        <div class='btn-group'>
            <button id='part-options' class='btn btn-primary dropdown-toggle' type='button' data-toggle="dropdown">{% trans "Options" %}<span class='caret'></span></button>
            <ul class='dropdown-menu'>
                {% if roles.part.change %}
                <li><a href='#' id='multi-part-category' title='{% trans "Set category" %}'>{% trans "Set Category" %}</a></li>
                {% endif %}
                <li><a href='#' id='multi-part-order' title='{% trans "Order parts" %}'>{% trans "Order Parts" %}</a></li>
                <li><a href='#' id='multi-part-export' title='{% trans "Export" %}'>{% trans "Export Data" %}</a></li>
            </ul>
        </div>
    </div>
    <div class='filter-list' id='filter-list-parts'>
        <!-- Empty div -->
    </div>
</div>


{% block category_tables %}
<table class='table table-striped table-condensed' data-toolbar='#button-toolbar' id='part-table'>
</table>
{% endblock category_tables %}

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

    if (inventreeLoadInt("show-part-cats") == 1) {
        $("#collapse-item-categories").collapse('show');
    }

    $("#collapse-item-categories").on('shown.bs.collapse', function() {
        inventreeSave('show-part-cats', 1);
    });
    
    $("#collapse-item-categories").on('hidden.bs.collapse', function() {
        inventreeDel('show-part-cats');
    });

    $("#cat-create").click(function() {
        launchModalForm(
            "{% url 'category-create' %}",
            {
                follow: true,
                {% if category %}
                data: {
                    category: {{ category.id }}
                },
                {% endif %}
                secondary: [
                    {
                        field: 'default_location',
                        label: '{% trans "New Location" %}',
                        title: '{% trans "Create new location" %}',
                        url: "{% url 'stock-location-create' %}",
                    },
                    {
                        field: 'parent',
                        label: '{% trans "New Category" %}',
                        title: '{% trans "Create new category" %}',
                        url: "{% url 'category-create' %}",
                    },
                ]
            }
        );
    })

    $("#part-export").click(function() {
        
        var url = "{% url 'part-export' %}?category={{ category.id }}";

        location.href = url;
    });

    {% if roles.part.add %}
    $("#part-create").click(function() {
        launchModalForm(
            "{% url 'part-create' %}",
            {
                follow: true,
                data: {
                {% if category %}
                    category: {{ category.id }}
                {% endif %}
                },
                secondary: [
                    {
                        field: 'category',
                        label: '{% trans "New Category" %}',
                        title: '{% trans "Create new Part Category" %}',
                        url: "{% url 'category-create' %}",
                    },
                    {
                        field: 'default_location',
                        label: '{% trans "New Location" %}',
                        title: '{% trans "Create new Stock Location" %}',
                        url: "{% url 'stock-location-create' %}",
                    }
                ]   
            }
        );
    });
    {% endif %}

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

    {% if category.parent %}
    var redirect = "{% url 'category-detail' category.parent.id %}";
    {% else %}
    var redirect = "{% url 'part-index' %}";
    {% endif %}

    $('#cat-delete').click(function() {
        launchModalForm(
            "{% url 'category-delete' category.id %}",
            {
                redirect: redirect
            }
        );
    });

    {% endif %}

    loadPartTable(
        "#part-table",
        "{% url 'api-part-list' %}",
        {
            params: {
                {% if category %}category: {{ category.id }},
                {% else %}category: "null",
                {% endif %}
            },
            buttons: ['#part-options'],
            checkbox: true,
        },
    );

{% endblock %}