{% extends "modal_form.html" %}

{% load i18n %}

{% block form %}

<h4>
    {% trans "Step 2 of 2 - Select Purchase Orders" %}
</h4>

<div class='alert alert-info alert-block' role='alert'>
    {% trans "Select existing purchase orders, or create new orders." %}
</div>

<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_purchase_orders'/>

    {% for supplier in suppliers %}
        {% for item in supplier.order_items %}
        <input type='hidden' name='part-supplier-{{ item.id }}' value='{{ item.order_supplier }}'/>
        <input type='hidden' name='part-quantity-{{ item.id }}' value='{{ item.order_quantity }}'/>
        {% endfor %}
    {% endfor %}

    <table class='table table-condensed table-striped' id='order-wizard-po-table'>
        <tr>
            <th>{% trans "Supplier" %}</th>
            <th>{% trans "Items" %}</th>
            <th colspan='2'>{% trans "Select Purchase Order" %}</th>
        </tr>
        {% for supplier in suppliers %}
        <tr id='suppier_row_{{ supplier.id }}'>
            <td>
                {% include 'hover_image.html' with image=supplier.image hover=False %}
                {{ supplier.name }}
            </td>
            <td>{{ supplier.order_items|length }}</td>
            <td>
                <button 
                    class='btn btn-default btn-create'
                    id='new_po_{{ supplier.id }}'
                    title='Create new purchase order for {{ supplier.name }}'
                    type='button'
                    onclick='newPurchaseOrderFromOrderWizard()'>
                    <span supplier-id='{{ supplier.id }}' class='fas fa-plus-circle'></span>
                </button>
            </td>
            <td>
                <div class='control-group'>
                    <div class='controls'>
                        <select
                            class='select'
                            id='id-purchase-order-{{ supplier.id }}'
                            name='purchase-order-{{ supplier.id }}'>
                            <option value=''>---------</option>
                            {% for order in supplier.pending_purchase_orders %}
                            <option value='{{ order.id }}'{% if supplier.selected_purchase_order == order.id %} selected='selected'{% endif %}>
                                {{ order }}
                            </option>
                            {% endfor %}
                        </select>
                    </div>
                    {% if not supplier.selected_purchase_order %}
                    <span class='help-inline'>{% trans "Select a purchase order for" %} {{ supplier.name }}</span>
                    {% endif %}
                </div>
            </td>
        </tr>
        {% endfor %}

    </table>
</form>
{% endblock %}