{% load i18n %}
<div class="tw-sticky tw-bottom-4 tw-left-0 tw-right-0 tw-z-50 tw-hidden mx-auto tw-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-reverse then settle
                then add .tw-hidden to #actions-bar
                then remove .slide-in-bottom-reverse
              end
            else
              if #actions-bar
                remove .tw-hidden from #actions-bar
                then trigger selected_transactions_updated
              end
            end
          end
        end">
  <div class="card slide-in-bottom">
    <div class="card-body p-2 d-flex justify-content-between align-items-center gap-3">
      {% spaceless %}
        <div class="dropdown">
          <button class="btn btn-secondary btn-sm dropdown-toggle" type="button" data-bs-toggle="dropdown"
                  aria-expanded="false">
            <i class="fa-regular fa-square-check fa-fw"></i>
          </button>
          <ul class="dropdown-menu">
            <li>
              <div class="dropdown-item px-3 tw-cursor-pointer"
                   _="on click set <#transactions-list input[type='checkbox']/>'s checked to true then call me.blur() then trigger change">
                <i class="fa-regular fa-square-check tw-text-green-400 me-3"></i>{% translate 'Select All' %}
              </div>
            </li>
            <li>
              <div class="dropdown-item px-3 tw-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 tw-text-red-400 me-3"></i>{% translate 'Unselect All' %}
              </div>
            </li>
          </ul>
        </div>
        <div class="vr tw-align-middle"></div>
        <button class="btn btn-secondary btn-sm"
                hx-get="{% url 'transactions_bulk_undelete' %}"
                hx-include=".transaction"
                data-bs-toggle="tooltip"
                data-bs-title="{% 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-bs-toggle="tooltip"
                data-bs-title="{% 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-danger"></i>
        </button>
        <div class="vr tw-align-middle"></div>
        <div class="btn-group"
             _="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" _="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 text-primary"></i>
            <span class="d-none d-md-inline-block" id="real-total-front">0</span>
          </button>
          <button type="button" class="btn btn-sm btn-secondary dropdown-toggle dropdown-toggle-split"
                  data-bs-toggle="dropdown" aria-expanded="false" data-bs-auto-close="outside">
            <span class="visually-hidden">{% trans "Toggle Dropdown" %}</span>
          </button>

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