{% load i18n %}
<div class="sticky bottom-4 left-0 right-0 z-1000 hidden mx-auto w-fit" id="actions-bar" _="on change from #transactions-list or htmx:afterSettle from window
        if #actions-bar then
            if no <input[type='checkbox']:checked/> in #transactions-list
              if #actions-bar
                add .slide-in-bottom-short-reverse then settle
                then add .hidden to #actions-bar
                then remove .slide-in-bottom-short-reverse
              end
            else
              if #actions-bar
                set #selected-count's innerHTML to length of <input[type='checkbox']:checked/> in #transactions-list
                then remove .hidden from #actions-bar
                then trigger selected_transactions_updated
              end
            end
          end
        end">
  <div class="card bg-base-300 shadow slide-in-bottom-short max-w-[90vw] card-border mt-5">
    <div class="card-body flex-row p-2 flex justify-between items-center gap-3 overflow-x-auto">
      {% spaceless %}
      <div class="font-bold text-md ms-2" id="selected-count">0</div>
      <div class="divider divider-horizontal m-0"></div>
      <div>
        <button role="button" class="btn btn-secondary btn-sm" type="button" data-bs-toggle="dropdown"
          aria-expanded="false">
          <i class="fa-regular fa-square-check fa-fw"></i>
          <i class="fa-solid fa-chevron-down fa-xs"></i>
        </button>
        <ul class="dropdown-menu menu">
          <li>
            <a class="cursor-pointer"
              _="on click set <#transactions-list .transaction:not([style*='display: none']) input[type='checkbox']/>'s checked to true then call me.blur() then trigger change">
              <i class="fa-regular fa-square-check text-success me-3"></i>{% translate 'Select All' %}
            </a>
          </li>
          <li>
            <a class="cursor-pointer"
              _="on click set <#transactions-list input[type='checkbox']/>'s checked to false then call me.blur() then trigger change">
              <i class="fa-regular fa-square text-error me-3"></i>{% translate 'Unselect All' %}
            </a>
          </li>
          <li>
            <a class="cursor-pointer"
              _="on click for checkbox in <#transactions-list input[type='checkbox']/> set checkbox.checked to (not checkbox.checked) end then call me.blur() then trigger change">
              <i class="fa-solid fa-arrow-right-arrow-left text-info me-3"></i>{% translate 'Invert election' %}
            </a>
          </li>
        </ul>
      </div>
      <div class="divider divider-horizontal m-0"></div>
      <button class="btn btn-secondary btn-sm" hx-get="{% url 'transactions_bulk_undelete' %}" hx-include=".transaction"
        data-tippy-content="{% translate 'Restore' %}">
        <i class="fa-solid fa-trash-arrow-up fa-fw"></i>
      </button>
      <button class="btn btn-secondary btn-sm" hx-get="{% url 'transactions_bulk_delete' %}" hx-include=".transaction"
        hx-trigger="confirmed" data-tippy-content="{% translate 'Delete' %}" 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 them!" %}" _="install prompt_swal">
        <i class="fa-solid fa-trash text-error"></i>
      </button>
      <div class="divider divider-horizontal m-0"></div>
      <div class="join" _="on selected_transactions_updated from #actions-bar
                  set realTotal to math.bignumber(0)
                  set flatTotal to math.bignumber(0)
                  set transactions to <.transaction:has(input[name='transactions']:checked)/>
                  set flatAmountValues to []
                  set realAmountValues to []

                  for transaction in transactions
                    set amt to first <.main-amount .amount/> in transaction
                    set amountValue to parseFloat(amt.getAttribute('data-amount'))
                    append amountValue to flatAmountValues

                    if not isNaN(amountValue)
                      set flatTotal to math.chain(flatTotal).add(amountValue)

                      if transaction match .income
                        append amountValue to realAmountValues
                        set realTotal to math.chain(realTotal).add(amountValue)
                      else
                        append -amountValue to realAmountValues
                        set realTotal to math.chain(realTotal).subtract(amountValue)
                      end
                    end
                  end
                  
                  set mean to flatTotal.divide(flatAmountValues.length).done().toNumber()
                  set realTotal to realTotal.done().toNumber()
                  set flatTotal to flatTotal.done().toNumber()

                  put realTotal.toLocaleString(undefined, {minimumFractionDigits: 0, maximumFractionDigits: 40}) into #real-total-front's innerText
                  put realTotal.toLocaleString(undefined, {minimumFractionDigits: 0, maximumFractionDigits: 40}) into #calc-menu-real-total's innerText
                  put flatTotal.toLocaleString(undefined, {minimumFractionDigits: 0, maximumFractionDigits: 40}) into #calc-menu-flat-total's innerText
                  put Math.max.apply(Math, realAmountValues).toLocaleString(undefined, {minimumFractionDigits: 0, maximumFractionDigits: 40}) into #calc-menu-max's innerText
                  put Math.min.apply(Math, realAmountValues).toLocaleString(undefined, {minimumFractionDigits: 0, maximumFractionDigits: 40}) into #calc-menu-min's innerText
                  put mean.toLocaleString(undefined, {minimumFractionDigits: 0, maximumFractionDigits: 40}) into #calc-menu-mean's innerText
                  put flatAmountValues.length.toLocaleString(undefined, {minimumFractionDigits: 0, maximumFractionDigits: 40}) into #calc-menu-count's innerText
                end">
        <button class="btn btn-secondary btn-sm join-item" _="on click
                      set original_value to #real-total-front's innerText
                      writeText(original_value) on navigator.clipboard
                      put '{% translate "copied!" %}' into #real-total-front's innerText wait 1s put original_value
          into #real-total-front's innerText end">
          <i class="fa-solid fa-plus fa-fw me-md-2"></i>
          <span class="hidden md:inline-block" id="real-total-front">0</span>
        </button>
        <div>
          <button class="join-item btn btn-sm btn-secondary" type="button" data-bs-toggle="dropdown"
            data-bs-auto-close="outside" aria-expanded="false">
            <i class="fa-solid fa-chevron-down fa-xs"></i>
          </button>

          <ul class="dropdown-menu dropdown-menu-end menu">
            <li class="cursor-pointer" _="on click
                        set original_value to #calc-menu-flat-total's innerText
                        writeText(original_value) on navigator.clipboard
                        put '{% translate "copied!" %}' into #calc-menu-flat-total wait 1s put original_value into
              #calc-menu-flat-total end">
              <div class="py-1 px-3">
                <div>
                  <div class="text-base-content/60 text-xs font-medium">
                    {% trans "Flat Total" %}
                  </div>
                  <div id="calc-menu-flat-total">
                    0
                  </div>
                </div>
              </div>
            </li>
            <li class="cursor-pointer" _="on click
                        set original_value to #calc-menu-real-total's innerText
                        writeText(original_value) on navigator.clipboard
                        put '{% translate "copied!" %}' into #calc-menu-real-total wait 1s put original_value into
              #calc-menu-real-total end">
              <div class="py-1 px-3">
                <div>
                  <div class="text-base-content/60 text-xs font-medium">
                    {% trans "Real Total" %}
                  </div>
                  <div id="calc-menu-real-total">
                    0
                  </div>
                </div>
              </div>
            </li>
            <li class="cursor-pointer" _="on click
                        set original_value to #calc-menu-mean's innerText
                        writeText(original_value) on navigator.clipboard
                        put '{% translate "copied!" %}' into #calc-menu-mean wait 1s put original_value into
              #calc-menu-mean end">
              <div class="p-1 px-3">
                <div>
                  <div class="text-base-content/60 text-xs font-medium">
                    {% trans "Mean" %}
                  </div>
                  <div id="calc-menu-mean">
                    0
                  </div>
                </div>
              </div>
            </li>
            <li class="cursor-pointer" _="on click
                        set original_value to #calc-menu-max's innerText
                        writeText(original_value) on navigator.clipboard
                        put '{% translate "copied!" %}' into #calc-menu-max wait 1s put original_value into
              #calc-menu-max end">
              <div class="py-1 px-3">
                <div>
                  <div class="text-base-content/60 text-xs font-medium">
                    {% trans "Max" %}
                  </div>
                  <div id="calc-menu-max">
                    0
                  </div>
                </div>
              </div>
            </li>
            <li class="cursor-pointer" _="on click
                        set original_value to #calc-menu-min's innerText
                        writeText(original_value) on navigator.clipboard
                        put '{% translate "copied!" %}' into #calc-menu-min wait 1s put original_value into
              #calc-menu-min end">
              <div class="py-1 px-3">
                <div>
                  <div class="text-base-content/60 text-xs font-medium">
                    {% trans "Min" %}
                  </div>
                  <div id="calc-menu-min">
                    0
                  </div>
                </div>
              </div>
            </li>
            <li class="cursor-pointer" _="on click
                        set original_value to #calc-menu-count's innerText
                        writeText(original_value) on navigator.clipboard
                        put '{% translate "copied!" %}' into #calc-menu-count wait 1s put original_value into
              #calc-menu-count end">
              <div class="py-1 px-3">
                <div>
                  <div class="text-base-content/60 text-xs font-medium">
                    {% trans "Count" %}
                  </div>
                  <div id="calc-menu-count">
                    0
                  </div>
                </div>
              </div>
            </li>
          </ul>
        </div>
      </div>
      {% endspaceless %}
    </div>
  </div>
</div>