{% extends "modal_form.html" %}

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

{% block form %}
{% default_currency as currency %}
{% settings_value 'PART_SHOW_PRICE_IN_FORMS' as show_price %}

<h4>
    {% trans "Step 1 of 2 - Select Part Suppliers" %}
</h4>

{% if parts|length > 0 %}
<div class='alert alert-info alert-block' role='alert'>
    {% trans "Select suppliers" %}
</div>
{% else %}
<div class='alert alert-warning alert-block' role='alert'>
    {% trans "No purchaseable parts selected" %}
</div>
{% endif %}

<form method="post" action='' class='js-modal-form' enctype="multipart/form-data">
    {% csrf_token %}
    {% load crispy_forms_tags %}

    <input type='hidden' name='form_step' value='select_parts'/>

    <table class='table table-condensed table-striped' id='order-wizard-part-table'>
        <tr>
            <th>{% trans "Part" %}</th>
            <th colspan='2'>{% trans "Select Supplier" %}</th>
            <th>{% trans "Quantity" %}</th>
            <th></th>
        </tr>
        {% for part in parts %}
        <tr id='part_row_{{ part.id }}'>
            <td>
                {% include "hover_image.html" with image=part.image hover=False %}
                {{ part.full_name }} <small><em>{{ part.description }}</em></small>
            </td>
            <td>
                <button class='btn btn-outline-secondary btn-create' onClick='newSupplierPartFromOrderWizard()' id='new_supplier_part_{{ part.id }}' part='{{ part.pk }}' title='{% trans "Create new supplier part" %}' type='button'>
                    <span part='{{ part.pk }}' class='fas fa-plus-circle'></span>
                </button>
            </td>
            <td>
                <div class='control-group'>
                    <div class='controls'>
                        <select class='select' id='id_supplier_part_{{ part.id }}' name="part-supplier-{{ part.id }}">
                            <option value=''>---------</option>
                            {% for supplier in part.supplier_parts.all %}
                            <option value="{{ supplier.id }}"{% if part.order_supplier == supplier.id %} selected="selected"{% endif %}>
                                {% if show_price %}
                                    {% call_method supplier 'get_price' part.order_quantity as price %}
                                    {% if price != None %}{% include "price.html" with price=price %}{% else %}{% trans 'No price' %}{% endif %} - 
                                {% endif %}
                                {{ supplier }}
                            </option>
                            {% endfor %}
                        </select>
                    </div>
                    {% if not part.order_supplier %}
                    <span class='help-inline'>{% blocktrans with name=part.name %}Select a supplier for <em>{{name}}</em>{% endblocktrans %}</span>
                    {% endif %}
                </div>
            </td>
            <td>
                <div class='control-group'>
                    <div class='controls'>
                        <input class='numberinput' type='number' min='0' value='{% decimal part.order_quantity %}' name='part-quantity-{{ part.id }}'/>
                    </div>
                </div>
            </td>
            <td>
                <button class='btn btn-outline-secondary btn-remove' onclick='removeOrderRowFromOrderWizard()' id='del_item_{{ part.id }}' title='{% trans "Remove part" %}' type='button'>
                    <span row='part_row_{{ part.id }}' class='fas fa-trash-alt icon-red'></span>
                </button>
            </td>
        </tr>
        {% endfor %}
    </table>
</form>
{% endblock %}