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

<form action="{% url 'django_ledger:bill-update-items' entity_slug=entity_slug bill_pk=bill_pk %}"
      method="post">
    <div class="columns is-multiline">
        <div class="column is-12">
            <h1 class="is-size-1">{% trans 'Bill Items' %}</h1>
        </div>
        <div class="column is-12">
            <div class="table-container">
                {% csrf_token %}
                {{ item_formset.non_form_errors }}
                {{ item_formset.management_form }}
                <table class="table is-fullwidth is-narrow is-striped is-bordered">
                    <thead>
                    <tr>
                        <th>{% trans 'Item' %}</th>
                        <th>{% trans 'PO Qty' %}</th>
                        <th>{% trans 'PO Amount' %}</th>
                        <th>{% trans 'Quantity' %}</th>
                        <th>{% trans 'Unit Cost' %}</th>
                        <th>{% trans 'Unit' %}</th>
                        <th>{% trans 'Total' %}</th>
                        <th>{% trans 'Delete' %}</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for f in item_formset %}
                        <tr>
                            <td>
                                {% for hidden_field in f.hidden_fields %}
                                    {{ hidden_field }}
                                {% endfor %}
                                {{ f.item_model }}
                                {% if f.errors %}
                                    <span class="has-text-danger">{{ f.errors }}</span>
                                {% endif %}
                            </td>
                            <td>{% if f.instance.po_quantity %}{{ f.instance.po_quantity }}{% endif %}</td>
                            <td>{% if f.instance.po_total_amount %}{% currency_symbol %}
                                {{ f.instance.po_total_amount | currency_format }}
                                <a class="button is-small is-info is-outlined"
                                   href="{% url 'django_ledger:po-detail' entity_slug=entity_slug po_pk=f.instance.po_model_id %}">
                                    {% trans 'View PO' %}
                                </a>{% endif %}</td>
                            <td id="{{ f.instance.html_id_quantity }}">{{ f.quantity }}</td>
                            <td id="{{ f.instance.html_id_unit_cost }}">{{ f.unit_cost }}</td>
                            <td>{{ f.entity_unit }}</td>
                            <td class="has-text-right" id="{{ f.instance.html_id_total_amount }}">
                                {% currency_symbol %}{{ f.instance.total_amount | currency_format }}</td>
                            <td class="has-text-centered">
                                {% if item_formset.can_delete %}
                                    {{ f.DELETE }}
                                {% endif %}
                            </td>
                        </tr>
                    {% endfor %}
                    </tbody>
                    <tfoot>
                    <tr>
                        <th></th>
                        <th></th>
                        <th></th>
                        <th></th>
                        <th></th>
                        <th class="has-text-right">{% trans 'Total' %}</th>
                        <th class="has-text-right">{% currency_symbol %}{{ total_amount__sum | currency_format }}</th>
                        <th></th>
                    </tr>
                    </tfoot>

                </table>
            </div>
        </div>
        <div class="column is-12">
            {% if not item_formset.has_po %}
                <a href="{% url 'django_ledger:product-create' entity_slug=entity_slug %}"
                   class="button is-primary">{% trans 'New Item' %}</a>
            {% endif %}
            <button class="button is-primary">{% trans 'Save' %}</button>
        </div>
    </div>
</form>