{% load currency_display %}
{% load i18n %}
<c-ui.fab-single-action
        url="{% url 'automatic_exchange_rate_add' %}"
        hx_target="#generic-offcanvas">
</c-ui.fab-single-action>
<div class="container">
  <div class="text-3xl font-bold font-mono w-full mb-3">
    {% spaceless %}
      <div>{% translate 'Automatic Exchange Rates' %}</div>
    {% endspaceless %}
  </div>

  <div class="card bg-base-100 shadow-xl">
    <div class="card-header rounded-box">
      <button type="button" hx-get="{% url 'automatic_exchange_rate_force_fetch' %}"
              class="btn btn-primary btn-sm">{% trans 'Fetch all' %}</button>
    </div>
    <div class="card-body">
      {% if services %}
        <c-config.search></c-config.search>
        <div class="overflow-x-auto">
          <table class="table table-hover">
            <thead>
            <tr>
              <th scope="col" class="table-col-auto"></th>
              <th scope="col" class="table-col-auto"></th>
              <th scope="col" class="w-auto">{% translate 'Name' %}</th>
              <th scope="col">{% translate 'Service' %}</th>
              <th scope="col">{% translate 'Targeting' %}</th>
              <th scope="col">{% translate 'Last fetch' %}</th>
            </tr>
            </thead>
            <tbody>
            {% for service in services %}
              <tr class="services">
                <td class="table-col-auto">
                  <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 'automatic_exchange_rate_edit' pk=service.id %}"
                       hx-target="#generic-offcanvas">
                      <i class="fa-solid fa-pencil fa-fw"></i></a>
                    <a class="btn btn-error btn-sm join-item"
                       role="button"
                       data-tippy-content="{% translate "Delete" %}"
                       hx-delete="{% url 'automatic_exchange_rate_delete' pk=service.id %}"
                       hx-trigger='confirmed'
                       data-bypass-on-ctrl="true"
                       data-title="{% translate "Are you sure?" %}"
                       data-text="{% translate "You won't be able to revert this!" %}"
                       data-confirm-text="{% translate "Yes, delete it!" %}"
                       _="install prompt_swal"><i class="fa-solid fa-trash fa-fw"></i></a>
                  </div>
                </td>
                <td class="table-col-auto">{% if service.is_active %}<i class="fa-solid fa-circle text-success"></i>{% else %}
                  <i class="fa-solid fa-circle text-error"></i>{% endif %}</td>
                <td>
                  {{ service.name }}
                  {% if service.failure_count > 0 %}
                    <span class="badge badge-error gap-1" data-tippy-content="{% blocktrans count counter=service.failure_count %}{{ counter }} consecutive failure{% plural %}{{ counter }} consecutive failures{% endblocktrans %}">
                      <i class="fa-solid fa-triangle-exclamation fa-fw"></i>
                      {{ service.failure_count }}
                    </span>
                  {% endif %}
                </td>
                <td>{{ service.get_service_type_display }}</td>
                <td>{{ service.target_currencies.count }} {% trans 'currencies' %}, {{ service.target_accounts.count }} {% trans 'accounts' %}</td>
                <td>{{ service.last_fetch|date:"SHORT_DATETIME_FORMAT" }}</td>
              </tr>
            {% endfor %}
            </tbody>
          </table>
        </div>
      {% else %}
        <c-msg.empty title="{% translate "No services configured" %}" remove-padding></c-msg.empty>
      {% endif %}
    </div>
  </div>
</div>
