<%# locals: (budget:) %>

<div>
  <div class="p-4 border-b border-gray-100">
    <h3 class="text-sm text-gray-500 mb-2">Income</h3>

    <% income_totals = budget.income_categories_with_totals %>
    <% income_categories = income_totals.category_totals.reject { |ct| ct.amount_money.zero? }.sort_by { |ct| ct.percentage }.reverse %>
    <span class="inline-block mb-2 text-xl font-medium text-gray-900">
      <%= format_money(income_totals.total_money) %>
    </span>

    <% if income_categories.any? %>
      <div>
        <div class="flex h-1.5 mb-3 gap-1">
          <% income_categories.each do |item| %>
            <div class="h-full rounded-xs" style="background-color: <%= item.category.color %>; width: <%= item.percentage %>%"></div>
          <% end %>
        </div>

        <div class="flex flex-wrap gap-x-2.5 gap-y-1 text-xs">
          <% income_categories.each do |item| %>
            <div class="flex items-center gap-1.5">
              <div class="w-2.5 h-2.5 rounded-full flex-shrink-0" style="background-color: <%= item.category.color %>"></div>
              <span class="text-gray-500"><%= item.category.name %></span>
              <span class="text-gray-900"><%= number_to_percentage(item.percentage, precision: 0) %></span>
            </div>
          <% end %>
        </div>
      </div>
    <% end %>
  </div>

  <div class="p-4">
    <h3 class="text-sm text-gray-500 mb-2">Expenses</h3>

    <% expense_totals = budget.expense_categories_with_totals %>
    <% expense_categories = expense_totals.category_totals.reject { |ct| ct.amount_money.zero? || ct.category.subcategory? }.sort_by { |ct| ct.percentage }.reverse %>

    <span class="inline-block mb-2 text-xl font-medium text-gray-900"><%= format_money(expense_totals.total_money) %></span>

    <% if expense_categories.any? %>
      <div>
        <div class="flex h-1.5 mb-3 gap-1">
          <% expense_categories.each do |item| %>
            <div class="h-full rounded-xs" style="background-color: <%= item.category.color %>; width: <%= item.percentage %>%"></div>
          <% end %>
        </div>

        <div class="flex flex-wrap gap-x-2.5 gap-y-1 text-xs">
          <% expense_categories.each do |item| %>
            <div class="flex items-center gap-1.5">
              <div class="w-2.5 h-2.5 rounded-full flex-shrink-0" style="background-color: <%= item.category.color %>"></div>
              <span class="text-gray-500"><%= item.category.name %></span>
              <span class="text-gray-900"><%= number_to_percentage(item.percentage, precision: 0) %></span>
            </div>
          <% end %>
        </div>
      </div>
    <% end %>
  </div>
</div>
