{% extends 'extends/offcanvas.html' %}
{% load i18n %}
{% load crispy_forms_tags %}

{% block title %}{% translate 'Transaction Rule' %}{% endblock %}

{% block body %}
  <div hx-get="{% url 'transaction_rule_view' transaction_rule_id=transaction_rule.id %}"
       hx-trigger="updated from:window" hx-target="closest .offcanvas" class="show-loading">
    <div class="text-2xl">{{ transaction_rule.name }}</div>
    <div class="text-subtle">{{ transaction_rule.description }}</div>
    <hr class="hr my-3">
    <div class="my-3">
      <div class="text-xl mb-2">{% translate 'If transaction...' %}</div>
      <div class="card card-border shadow">
        <div class="card-body">
          {{ transaction_rule.trigger }}
        </div>
        <div class="card-footer text-end">
          <a class="btn btn-ghost btn-xs"
             role="button"
             data-tippy-content="{% translate "Edit" %}"
             hx-get="{% url 'transaction_rule_edit' transaction_rule_id=transaction_rule.id %}"
             hx-target="#generic-offcanvas">
            <i class="fa-solid fa-pencil fa-fw"></i></a>
        </div>
      </div>
    </div>

    <div class="my-3">
      <div class="text-xl mb-2">{% translate 'Then...' %}</div>
      <div class="flex flex-col gap-3">
      {% for action in all_actions %}
        {% if action.action_type == "edit_transaction" %}
          <div class="card bg-base-100 card-border shadow">
            <div class="card-header">
              <div>
                {% if action.order != 0 %}<span class="badge badge-secondary">{{ action.order }}</span>{% endif %}
                <span class="badge badge-primary">{% trans 'Edit transaction' %}</span>
              </div>
            </div>
            <div class="card-body">
              <div>
                {% translate 'Set' %} <span
                  class="badge badge-secondary">{{ action.get_field_display }}</span> {% translate 'to' %}
              </div>
              <div class="bg-base-200 rounded-3xl mt-3 p-2">{{ action.value }}</div>
            </div>
            <div class="card-footer text-end">
              <a class="btn btn-ghost btn-xs"
                 role="button"
                 data-tippy-content="{% translate 'Edit' %}"
                 hx-get="{% url 'transaction_rule_action_edit' transaction_rule_action_id=action.id %}"
                 hx-target="#generic-offcanvas">
                <i class="fa-solid fa-pencil fa-fw"></i>
              </a>
              <a class="btn btn-ghost btn-xs text-error"
                 role="button"
                 data-tippy-content="{% translate 'Delete' %}"
                 hx-delete="{% url 'transaction_rule_action_delete' transaction_rule_action_id=action.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>
          </div>
        {% elif action.action_type == "update_or_create_transaction" %}
          <div class="card bg-base-100 card-border shadow">
            <div class="card-header">
              <div>
                {% if action.order != 0 %}<span class="badge badge-secondary">{{ action.order }}</span>{% endif %}
                <span class="badge badge-primary">{% trans 'Update or create transaction' %}</span>
              </div>
            </div>
            <div class="card-body">
              <div>{% trans 'Edit to view' %}</div>
            </div>
            <div class="card-footer text-end">
              <a class="btn btn-ghost btn-xs"
                 role="button"
                 data-tippy-content="{% translate 'Edit' %}"
                 hx-get="{% url 'update_or_create_transaction_rule_action_edit' pk=action.id %}"
                 hx-target="#generic-offcanvas">
                <i class="fa-solid fa-pencil fa-fw"></i>
              </a>
              <a class="btn btn-ghost btn-xs text-error"
                 role="button"
                 data-tippy-content="{% translate 'Delete' %}"
                 hx-delete="{% url 'update_or_create_transaction_rule_action_delete' pk=action.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>
          </div>
        {% endif %}
      {% empty %}
        <div class="bg-base-100 card-border shadow">
          <div class="card-body">
            {% translate 'This rule has no actions' %}
          </div>
        </div>
      {% endfor %}
      </div>
      <div class="grid grid-cols-1 lg:grid-cols-2 gap-2 mt-5">
        <div class="dropdown">
          <button class="btn btn-secondary w-full" type="button" data-bs-toggle="dropdown"
                  aria-expanded="false">
            <i class="fa-solid fa-flask-vial me-2"></i>{% translate 'Test' %}
          </button>
          <ul class="dropdown-menu menu">
            {% if transaction_rule.on_create %}
              <li><a role="link" href="#"
                     hx-get="{% url 'transaction_rule_dry_run_created' pk=transaction_rule.id %}"
                     hx-target="#generic-offcanvas">{% trans 'Create' %}</a></li>
            {% endif %}
            {% if transaction_rule.on_update %}
              <li><a role="link" href="#"
                     hx-get="{% url 'transaction_rule_dry_run_updated' pk=transaction_rule.id %}"
                     hx-target="#generic-offcanvas">{% trans 'Update' %}</a></li>
            {% endif %}
            {% if transaction_rule.on_delete %}
              <li><a role="link" href="#"
                     hx-get="{% url 'transaction_rule_dry_run_deleted' pk=transaction_rule.id %}"
                     hx-target="#generic-offcanvas">{% trans 'Delete' %}</a></li>
            {% endif %}
          </ul>
        </div>
        <div class="dropdown">
          <button class="btn btn-primary w-full" type="button" data-bs-toggle="dropdown"
                  aria-expanded="false">
            <i class="fa-solid fa-circle-plus me-2"></i>{% translate 'Add new action' %}
          </button>
          <ul class="dropdown-menu menu">
            <li><a role="link" href="#"
                   hx-get="{% url 'transaction_rule_action_add' transaction_rule_id=transaction_rule.id %}"
                   hx-target="#generic-offcanvas">{% trans 'Edit Transaction' %}</a></li>
            <li><a role="link" href="#"
                   hx-get="{% url 'update_or_create_transaction_rule_action_add' transaction_rule_id=transaction_rule.id %}"
                   hx-target="#generic-offcanvas">{% trans 'Update or Create Transaction' %}</a></li>
          </ul>
        </div>
      </div>
    </div>
  </div>
{% endblock %}
