{% extends "part/part_app_base.html" %}

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

{% block menubar %}
{% include "part/navbar.html" %}
{% endblock %}

{% block content %}

<div class='panel panel-default panel-inventree'>

{% if part.variant_of %}
<div class='alert alert-info alert-block'>
    {% trans "This part is a variant of" %} <strong><a href="{% url 'part-variants' part.variant_of.id %}">{{ part.variant_of.full_name }}</a></strong>
</div>
{% endif %}

    <div class="row">
    <div class="col-sm-6">
        {% include "part/part_thumb.html" %}
        <div class="media-body">
            <h3>
                {{ part.full_name }}
                {% if user.is_staff and roles.part.change %}
                <a  href="{% url 'admin:part_part_change' part.pk %}"><span title="{% trans 'Admin view' %}" class='fas fa-user-shield'></span></a>
                {% endif %}
                {% if not part.active %}
                <div class='label label-large label-large-red'>
                    {% trans 'Inactive' %}
                </div>
                {% endif %}
            </h3>   
            <p><em>{{ part.description }}</em></p>

            <div class='btn-group action-buttons' role='group'>
                <button type='button' class='btn btn-default' id='toggle-starred' title='{% trans "Star this part" %}'>
                    <span id='part-star-icon' class='fas fa-star {% if starred %}icon-yellow{% endif %}'/>
                </button>

                {% if barcodes %}
                <!-- Barcode actions menu -->
                <div class='btn-group'>
                    <button id='barcode-options' title='{% trans "Barcode actions" %}' class='btn btn-default dropdown-toggle' type='button' data-toggle='dropdown'><span class='fas fa-qrcode'></span> <span class='caret'></span></button>
                    <ul class='dropdown-menu'>
                        <li><a href='#' id='show-qr-code'><span class='fas fa-qrcode'></span> {% trans "Show QR Code" %}</a></li>
                        <li><a href='#' id='print-label'><span class='fas fa-tag'></span> {% trans "Print Label" %}</a></li>
                    </ul>
                </div>
                {% endif %}
                {% if part.active %}
                <button type='button' class='btn btn-default' id='price-button' title='{% trans "Show pricing information" %}'>
                    <span id='part-price-icon' class='fas fa-dollar-sign'/>
                </button>
                {% if roles.stock.change %}
                <button type='button' class='btn btn-default' id='part-count' title='{% trans "Count part stock" %}'>
                    <span class='fas fa-clipboard-list'/>
                </button>
                {% endif %}
                {% if part.purchaseable %}
                {% if roles.purchase_order.add %}
                <button type='button' class='btn btn-default' id='part-order' title='{% trans "Order part" %}'>
                    <span id='part-order-icon' class='fas fa-shopping-cart'/>
                </button>
                {% endif %}
                {% endif %}
                {% endif %}
                <!-- Part actions -->
                {% if roles.part.add or roles.part.change or roles.part.delete %}
                <div class='btn-group'>
                    <button id='part-actions' title='{% trans "Part actions" %}' class='btn btn-default dropdown-toggle' type='button' data-toggle='dropdown'> <span class='fas fa-shapes'></span> <span class='caret'></span></button>
                    <ul class='dropdown-menu'>
                        {% if roles.part.add %}
                        <li><a href='#' id='part-duplicate'><span class='fas fa-copy'></span> {% trans "Duplicate part" %}</a></li>
                        {% endif %}
                        {% if roles.part.change %}
                        <li><a href='#' id='part-edit'><span class='fas fa-edit icon-blue'></span> {% trans "Edit part" %}</a></li>
                        {% endif %}
                        {% if not part.active and roles.part.delete %}
                        <li><a href='#' id='part-delete'><span class='fas fa-trash-alt icon-red'></span> {% trans "Delete part" %}</a></li>
                        {% endif %}
                    </ul>
                </div>
                {% endif %}
            </div>
            <table class='table table-condensed'>
                <col width='25'>
                {% if part.IPN %}
                <tr>
                    <td></td>
                    <td>{% trans "IPN" %}</td>
                    <td>{{ part.IPN }}</td>
                </tr>
                {% endif %}
                {% if part.link %}
                <tr>
                    <td><span class='fas fa-link'></span></td>
                    <td>{% trans "External Link" %}</td>
                    <td><a href="{{ part.link }}">{{ part.link }}</a></td>
                </tr>
                {% endif %}
                <tr>
                </tr>
            </table>
        </div>
        </div>
    </div>
    <div class="col-sm-6">
        <table class="table table-striped">
            <col width='25'>
            <tr>
                <td><span class='fas fa-boxes'></span></td>
                <td>
                    <h4>{% trans "Available Stock" %}</h4>
                </td>
                <td><h4>{% decimal available %}{% if part.units %} {{ part.units }}{% endif %}</h4></td>
            </tr>
            <tr>
                <td><span class='fas fa-map-marker-alt'></span></td>
                <td>{% trans "In Stock" %}</td>
                <td>{% include "part/stock_count.html" %}</td>
            </tr>
            {% if on_order > 0 %}
            <tr>
                <td><span class='fas fa-shopping-cart'></span></td>
                <td>{% trans "On Order" %}</td>
                <td>{% decimal on_order %}</td>
            </tr>
            {% endif %}
            {% if required_build_order_quantity > 0 %}
            <tr>
                <td><span class='fas fa-clipboard-list'></span></td>
                <td>{% trans "Required for Build Orders" %}</td>
                <td>{% decimal required_build_order_quantity %}
            </tr>
            {% endif %}
            {% if required_sales_order_quantity > 0 %}
            <tr>
                <td><span class='fas fa-clipboard-list'></span></td>
                <td>{% trans "Required for Sales Orders" %}</td>
                <td>{% decimal required_sales_order_quantity %}
            </tr>
            {% endif %}
            {% if allocated > 0 %}
            <tr>
                <td><span class='fas fa-dolly'></span></td>
                <td>{% trans "Allocated to Orders" %}</td>
                <td>{% decimal allocated %}</td>
            </tr>
            {% endif %}
            
            {% if not part.is_template %}
            {% if part.assembly %}
            <tr>
                <td><span class='fas fa-tools'></span></td>
                <td colspan='2'>
                    <strong>{% trans "Build Status" %}</strong>
                </td>
            </tr>
            <tr>
                <td></td>
                <td>{% trans "Can Build" %}</td>
                <td>{% decimal part.can_build %}</td>
            </tr>
            {% if quantity_being_built > 0 %}
            <tr>
                <td></td>
                <td>{% trans "Building" %}</td>
                <td>{% decimal quantity_being_built %}</td>
            </tr>
            {% endif %}
            {% endif %}
        {% endif %}
        </table>
    </div>
    </div>
        
</div>

<div class='panel panel-default panel-inventree'>
    
    <div class='panel-heading'>
        <h4>
            {% block heading %}
            <!-- Heading goes here -->
            {% endblock %}
        </h4>
    </div>

    <div class='panel-content'>
        {% block details %}        
        <!-- Specific part details go here... -->
        {% endblock %}
    </div>
</div>

{% endblock %}

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

    enableNavbar({
        label: 'part',
        toggleId: '#part-menu-toggle',
    });

    {% if part.image %}
    $('#part-thumb').click(function() {
        showModalImage('{{ part.image.url }}');
    });
    {% endif %}

    enableDragAndDrop(
        '#part-thumb',
        "{% url 'part-image-upload' part.id %}",
        {
            label: 'image',
            success: function(data, status, xhr) {
                location.reload();
            }
        }
    );

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

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

    $("#price-button").click(function() {
        launchModalForm(
            "{% url 'part-pricing' part.id %}",
            {
                submit_text: 'Calculate',
                hideErrorMessage: true,
            }
        );
    });

    $("#toggle-starred").click(function() {
        toggleStar({
            part: {{ part.id }},
            button: '#part-star-icon',
        });
    });

    $("#part-image-upload").click(function() {
        launchModalForm("{% url 'part-image-upload' part.id %}",
            {
                reload: true
            }
        );
    });


    function onSelectImage(response) {
        // Callback when the image-selection modal form is displayed
        // Populate the form with image data (requested via AJAX)
        
        $("#modal-form").find("#image-select-table").bootstrapTable({
            pagination: true,
            pageSize: 25,
            url: "{% url 'api-part-thumbs' %}",
            showHeader: false,
            clickToSelect: true,
            singleSelect: true,
            columns: [
                {
                    checkbox: true,
                },
                {
                    field: 'image',
                    title: 'Image',
                    formatter: function(value, row, index, field) {
                        return "<img src='/media/" + value + "' class='grid-image'/>"
                    }
                }
            ],
            onCheck: function(row, element) {

                // Update the selected image in the form
                var ipt = $("#modal-form").find("#image-input");
                ipt.val(row.image);

            }
        });
    }
    
    {% if roles.part.change %}
    
    {% settings_value "INVENTREE_DOWNLOAD_FROM_URL" as allow_download %}
    {% if allow_download %}
    $("#part-image-url").click(function() {
        launchModalForm(
            '{% url "part-image-download" part.id %}',
            {
                reload: true,
            }
        );
    });
    {% endif %}

    $("#part-image-select").click(function() {
        launchModalForm("{% url 'part-image-select' part.id %}",
                        {
                            reload: true,
                            after_render: onSelectImage
                        });
    });

    $("#part-edit").click(function() {
        launchModalForm(
            "{% url 'part-edit' part.id %}",
            {
                reload: true,
            }
        );
    });
    {% endif %}

    $("#part-order").click(function() {
        launchModalForm("{% url 'order-parts' %}", {
            data: {
                part: {{ part.id }},
            },
            reload: true,
        });
    });

    {% if roles.part.add %}
    $("#part-duplicate").click(function() {
        launchModalForm(
            "{% url 'part-duplicate' part.id %}",
            {
                follow: true,
            }
        );
    });
    {% endif %}

    {% if not part.active and roles.part.delete %}
    $("#part-delete").click(function() {
        launchModalForm(
            "{% url 'part-delete' part.id %}",
            {
                redirect: {% if part.category %}"{% url 'category-detail' part.category.id %}"{% else %}"{% url 'part-index' %}"{% endif %}
            }
        );
    });
    {% endif %}

{% endblock %}