{% load currency_display %}
{% load i18n %}
{% if active %}
<div class="show-loading" hx-get="{% url 'active_installment_plans_list' %}" hx-trigger="updated from:window" hx-swap="outerHTML">
{% else %}
<div class="show-loading" hx-get="{% url 'finished_installment_plans_list' %}" hx-trigger="updated from:window" hx-swap="outerHTML">
{% endif %}
  {% if installment_plans %}
  <c-config.search></c-config.search>

  <div class="overflow-x-auto">
    <table class="table table-zebra">
      <thead>
        <tr>
          <th scope="col" class="table-col-auto"></th>
          <th scope="col">{% translate 'Name' %}</th>
          <th scope="col">{% translate 'Account' %}</th>
          <th scope="col">{% translate 'Amount' %}</th>
        </tr>
      </thead>
      <tbody>
        {% for installment_plan in installment_plans %}
        <tr class="installment-plan">
          <td class="table-col-auto text-center">
            <div class="join" role="group" aria-label="{% translate 'Actions' %}">
              <a class="btn btn-secondary btn-sm join-item"
                    role="button"
                    data-tippy-content="{% translate "Edit" %}"
                    hx-get="{% url 'installment_plan_edit' installment_plan_id=installment_plan.id %}"
                    hx-swap="innerHTML"
                    hx-target="#generic-offcanvas">
                <i class="fa-solid fa-pencil fa-fw"></i></a>
              <a class="btn btn-secondary btn-sm join-item"
                    role="button"
                    data-tippy-content="{% translate "Installments" %}"
                    hx-get="{% url 'installment_plan_transactions' installment_plan_id=installment_plan.id %}"
                    hx-swap="innerHTML"
                    hx-target="#persistent-generic-offcanvas-left">
                <i class="fa-solid fa-eye fa-fw"></i></a>
              <a class="btn btn-secondary btn-sm join-item"
                    role="button"
                    data-tippy-content="{% translate "Refresh" %}"
                    hx-get="{% url 'installment_plan_refresh' installment_plan_id=installment_plan.id %}"
                    hx-target="#generic-offcanvas"
                    hx-trigger='confirmed'
                    data-bypass-on-ctrl="true"
                    data-title="{% translate "Are you sure?" %}"
                    data-text="{% translate "This will update all transactions associated with this plan and recreate missing ones" %}"
                    data-confirm-text="{% translate "Yes, refresh it!" %}"
                    _="install prompt_swal">
                <i class="fa-solid fa-arrows-rotate fa-fw"></i></a>
              <a class="btn btn-error btn-sm join-item"
                      role="button"
                      data-tippy-content="{% translate "Delete" %}"
                      hx-delete="{% url 'installment_plan_delete' installment_plan_id=installment_plan.id %}"
                      hx-trigger='confirmed'
                      data-bypass-on-ctrl="true"
                      data-title="{% translate "Are you sure?" %}"
                      data-text="{% translate "This will delete the plan and all transactions associated with it" %}"
                      data-confirm-text="{% translate "Yes, delete it!" %}"
                      _="install prompt_swal"><i class="fa-solid fa-trash fa-fw"></i></a>
            </div>
          </td>
          <td>
            <div class="{% if installment_plan.type == 'EX' %}text-error{% else %}text-success{% endif %}">
              {{ installment_plan.description }}
            </div>
            <div class="text-sm text-base-content/60">{{ installment_plan.notes|linebreaksbr }}</div>
          </td>
          <td class="w-auto">
            {% if installment_plan.account.group %}{{ installment_plan.account.group }} • {% endif %}{{ installment_plan.account }}
          </td>
          <td class="w-auto">
            <c-amount.display
              :amount="installment_plan.installment_amount"
              :prefix="installment_plan.account.currency.prefix"
              :suffix="installment_plan.account.currency.suffix"
              :decimal_places="installment_plan.account.currency.decimal_places"
              color="{% if installment_plan.type == "EX" %}red{% else %}green{% endif %}"></c-amount.display>
          </td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
  </div>
  {% else %}
    <c-msg.empty title="{% translate "No installment plans" %}" remove-padding></c-msg.empty>
  {% endif %}
</div>
