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

<form action="{% url 'django_ledger:customer-estimate-update-items' entity_slug=entity_slug ce_pk=ce_pk %}"
      method="post">
    <div class="columns is-multiline">
        <div class="column is-12">
            <h1 class="is-size-1">{% trans 'Estimate 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 'Quantity' %}</th>
                        <th>{% trans 'Unit Cost' %}</th>
                        <th>{% trans 'Unit Sales Price' %}</th>
                        <th>{% trans 'Business Unit' %}</th>
                        <th>{% trans 'Total Cost' %}</th>
                        <th>{% trans 'Total Revenue' %}</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 id="{{ f.instance.html_id_quantity }}">{{ f.ce_quantity }}</td>
                            <td id="{{ f.instance.html_id_unit_cost }}">{{ f.ce_unit_cost_estimate }}</td>
                            <td>{{ f.ce_unit_revenue_estimate }}</td>
                            <td>{{ f.entity_unit }}</td>
                            <td class="has-text-right" id="{{ f.instance.html_id_total_amount }}">
                                {% currency_symbol %}{{ f.instance.ce_cost_estimate | currency_format }}</td>
                            <td class="has-text-right" id="{{ f.instance.html_id_total_amount }}">
                                {% currency_symbol %}{{ f.instance.ce_revenue_estimate | 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 class="has-text-right">{% trans 'Total' %}</th>
                        <th class="has-text-right">{% currency_symbol %}{{ ce_cost_estimate__sum | currency_format }}</th>
                        <th class="has-text-right">{% currency_symbol %}{{ ce_revenue_estimate__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>