{% load trans from i18n %}
{% load django_ledger %}

<form action="{% url 'django_ledger:po-update-items' entity_slug=entity_slug po_pk=po_model.uuid %}"
      method="post">
    <div class="columns is-multiline">
        <div class="column is-12">
            <h1 class="is-size-1">{% trans 'Purchase Order Items' %}</h1>
        </div>
        <div class="column is-12">
            <div class="table-container">
                {% csrf_token %}
                {{ itemtxs_formset.non_form_errors }}
                {{ itemtxs_formset.management_form }}
                <table class="table is-fullwidth is-narrow is-striped1 is-bordered">
                    <thead>
                    <tr>
                        <th>{% trans 'Item' %}</th>
                        <th>{% trans 'Unit Cost' %}</th>
                        <th>{% trans 'Quantity' %}</th>
                        <th>{% trans 'Unit' %}</th>
                        <th>{% trans 'Amount' %}</th>
                        <th>{% trans 'Status' %}</th>
                        {% if itemtxs_formset.can_delete %}
                            <th>{% trans 'Delete' %}</th>
                        {% endif %}
                        <th>{% trans 'Create Bill' %}</th>
                        <th>{% trans 'Bill Paid?' %}</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for f in itemtxs_formset %}
                        <tr>
                            <td>
                                {% for hidden_field in f.hidden_fields %}
                                    {{ hidden_field }}
                                {% endfor %}
                                {{ f.item_model }}
                                {% if f.errors %}
                                    {{ f.errors }}
                                {% endif %}
                            </td>

                            <td id="{{ f.instance.html_id_unit_cost }}">{{ f.po_unit_cost }}</td>
                            <td id="{{ f.instance.html_id_quantity }}">{{ f.po_quantity }}</td>
                            <td>{{ f.entity_unit }}</td>
                            <td class="has-text-right" id="{{ f.instance.html_id_total_amount }}">
                                {% currency_symbol %}{{ f.instance.po_total_amount | currency_format }}</td>
                            <td>{{ f.po_item_status }}</td>
                            {% if itemtxs_formset.can_delete %}
                                <td class="has-text-centered">
                                    {{ f.DELETE }}
                                </td>
                            {% endif %}
                            <td class="has-text-centered">
                                {% if f.instance.can_create_bill %}
                                    {{ f.create_bill }}
                                {% elif f.instance.bill_model %}
                                    <a class="button is-small is-light"
                                       href="{% url 'django_ledger:bill-detail' entity_slug=entity_slug bill_pk=f.instance.bill_model_id %}">
                                    {% trans 'View Bill' %}
                                    </a>
                                    {#                                {% else %}#}
                                {% endif %}
                            </td>
                            <td class="has-text-centered">
                            {% if f.instance.bill_model %}
                                {% if f.instance.bill_model.is_paid %}
                                    <span class="icon is-small has-text-success">{% icon 'bi:check-circle-fill' 24 %}</span>
                                {% else %}
                                    <span class="icon is-small has-text-danger">{% icon 'clarity:no-access-solid' 24 %}</span>
                                {% endif %}

                            {% endif %}
                            </td>
                        </tr>
                    {% endfor %}
                    </tbody>

                    <tfoot>
                    <tr>
                        <th></th>
                        <th></th>
                        <th></th>
                        <th class="has-text-right">{% trans 'Total' %}</th>
                        <th class="has-text-right">{% currency_symbol %}{{ po_model.po_amount | currency_format }}</th>
                        <th></th>
                        {% if itemtxs_formset.can_delete %}
                            <th></th>
                        {% endif %}
                        <th></th>
                        <th></th>
                    </tr>
                    </tfoot>

                </table>
            </div>
        </div>
        <div class="column is-12">
            <a href="{% url 'django_ledger:inventory-item-create' entity_slug=entity_slug %}"
               class="button is-primary">{% trans 'New Item' %}</a>
            <button class="button is-primary">{% trans 'Save' %}</button>
        </div>
    </div>
</form>