<%# Export Controls %>
<%
# Build params hash for links
base_params = {
period_type: period_type,
start_date: start_date,
end_date: end_date,
sort_by: params[:sort_by],
sort_direction: params[:sort_direction]
}.compact
%>
<%# Export Options %>
<%= link_to export_transactions_reports_path(base_params.merge(format: :csv)),
class: "font-medium whitespace-nowrap inline-flex items-center gap-1 rounded-lg px-3 py-2 text-sm text-primary border border-secondary bg-transparent hover:bg-surface-hover" do %>
<%= icon("download", class: "w-3 h-3") %>
<%= t("reports.transactions_breakdown.export.csv") %>
<% end %>
<%= link_to google_sheets_instructions_reports_path(base_params),
class: "font-medium whitespace-nowrap inline-flex items-center gap-1 rounded-lg px-3 py-2 text-sm text-primary border border-secondary bg-transparent hover:bg-surface-hover",
data: { turbo_frame: "modal" } do %>
<%= icon("external-link", class: "w-3 h-3") %>
<%= t("reports.transactions_breakdown.export.google_sheets") %>
<% end %>
<%# Transactions Tables - Split by Income and Expenses %>
<% if transactions.any? %>
<%
# Separate income and expenses
income_groups = transactions.select { |g| g[:type] == "income" }
expense_groups = transactions.select { |g| g[:type] == "expense" }
# Calculate totals
income_total = income_groups.sum { |g| g[:total] }
expense_total = expense_groups.sum { |g| g[:total] }
# Determine sort direction for Amount column
current_sort_by = params[:sort_by]
current_sort_direction = params[:sort_direction]
# Toggle sort direction: if currently sorting by amount desc, switch to asc; otherwise default to desc
next_sort_direction = (current_sort_by == "amount" && current_sort_direction == "desc") ? "asc" : "desc"
# Build params for amount sort link
amount_sort_params = base_params.merge(sort_by: "amount", sort_direction: next_sort_direction)
%>
<%# Income Section %>
<% if income_groups.any? %>
<%= render "reports/breakdown_table",
groups: income_groups,
total: income_total,
type: :income,
amount_sort_params: amount_sort_params,
current_sort_by: current_sort_by,
current_sort_direction: current_sort_direction %>
<% end %>
<%# Expenses Section %>
<% if expense_groups.any? %>
<%= render "reports/breakdown_table",
groups: expense_groups,
total: expense_total,
type: :expense,
amount_sort_params: amount_sort_params,
current_sort_by: current_sort_by,
current_sort_direction: current_sort_direction %>
<% end %>
<%# Summary Stats %>
<%= t("reports.transactions_breakdown.pagination.showing", count: transactions.sum { |g| g[:count] }) %>
<% else %>
<%= t("reports.transactions_breakdown.no_transactions") %>
<% end %>