<?php
$pageTitle = 'Financial Reports';
$revenue = 0;
$expenseTotal = 0;
foreach ($profitLoss as $row) {
    if ($row['account_type'] === 'revenue') $revenue += (float) $row['amount'];
    else $expenseTotal += abs((float) $row['amount']);
}
?>
<div class="page-heading">
    <div><p class="eyebrow">Accounting</p><h2>Financial Reports</h2><p>Review operating results, account balances, aging, and profitability.</p></div>
    <form class="inline-form" method="get"><input type="date" name="from" value="<?= e($from) ?>"><input type="date" name="to" value="<?= e($to) ?>"><button class="btn btn-light">Apply</button></form>
</div>
<div class="stats-grid">
    <div class="stat-card highlight"><span>Revenue</span><strong><?= money($revenue) ?></strong></div>
    <div class="stat-card"><span>Expenses</span><strong><?= money($expenseTotal) ?></strong></div>
    <div class="stat-card"><span>Net Income</span><strong><?= money($revenue - $expenseTotal) ?></strong></div>
    <div class="stat-card"><span>Open Receivables</span><strong><?= money(array_sum(array_map(fn($r)=>(float)$r['balance'], $receivableAging))) ?></strong></div>
</div>
<div class="content-grid two-col">
    <section class="card table-card"><div class="card-header"><h3>Profit & Loss</h3></div><div class="table-wrap"><table><thead><tr><th>Account</th><th>Type</th><th>Amount</th></tr></thead><tbody><?php foreach ($profitLoss as $row): ?><tr><td><?= e($row['code'].' '.$row['name']) ?></td><td><?= e(readable_status($row['account_type'])) ?></td><td><?= money(abs((float)$row['amount'])) ?></td></tr><?php endforeach; ?><?php if (!$profitLoss): ?><tr><td colspan="3" class="empty-state">No posted activity in this period.</td></tr><?php endif; ?></tbody></table></div></section>
    <section class="card table-card"><div class="card-header"><h3>Load Profitability</h3></div><div class="table-wrap"><table><thead><tr><th>Load</th><th>Revenue</th><th>Total Cost</th><th>Net Profit</th></tr></thead><tbody><?php foreach ($profitability as $row): ?><tr><td><?= e($row['load_number']) ?></td><td><?= money($row['revenue']) ?></td><td><?= money((float)$row['expenses'] + (float)$row['driver_pay']) ?></td><td><strong><?= money($row['net_profit']) ?></strong></td></tr><?php endforeach; ?></tbody></table></div></section>
</div>
<section class="card table-card"><div class="card-header"><h3>Accounts Receivable Aging</h3></div><div class="table-wrap"><table><thead><tr><th>Invoice</th><th>Customer</th><th>Due Date</th><th>Age</th><th>Balance</th></tr></thead><tbody><?php foreach ($receivableAging as $row): ?><tr><td><?= e($row['invoice_number']) ?></td><td><?= e($row['broker_name'] ?: '-') ?></td><td><?= e(date_display($row['due_date'])) ?></td><td><span class="badge badge-<?= $row['aging_bucket']==='Current'?'success':'warning' ?>"><?= e($row['aging_bucket']) ?></span></td><td><?= money($row['balance']) ?></td></tr><?php endforeach; ?><?php if (!$receivableAging): ?><tr><td colspan="5" class="empty-state">No open receivables.</td></tr><?php endif; ?></tbody></table></div></section>
<section class="card table-card"><div class="card-header"><h3>Trial Balance as of <?= e(date_display($to)) ?></h3></div><div class="table-wrap"><table><thead><tr><th>Code</th><th>Account</th><th>Type</th><th>Debit</th><th>Credit</th><th>Balance</th></tr></thead><tbody><?php foreach ($trialBalance as $row): ?><tr><td><?= e($row['code']) ?></td><td><?= e($row['name']) ?></td><td><?= e(readable_status($row['account_type'])) ?></td><td><?= money($row['debit']) ?></td><td><?= money($row['credit']) ?></td><td><?= money($row['balance']) ?></td></tr><?php endforeach; ?></tbody></table></div></section>
<div class="content-grid two-col">
<section class="card table-card"><div class="card-header"><h3>Accounts Payable Aging</h3></div><div class="table-wrap"><table><thead><tr><th>Bill</th><th>Vendor</th><th>Due Date</th><th>Age</th><th>Balance</th></tr></thead><tbody><?php foreach ($payableAging as $row): ?><tr><td><?= e($row['bill_number']) ?></td><td><?= e($row['vendor_name']) ?></td><td><?= e(date_display($row['due_date'])) ?></td><td><?= e($row['aging_bucket']) ?></td><td><?= money($row['balance']) ?></td></tr><?php endforeach; ?><?php if (!$payableAging): ?><tr><td colspan="5" class="empty-state">No open payables.</td></tr><?php endif; ?></tbody></table></div></section>
<section class="card table-card"><div class="card-header"><h3>Balance Sheet Accounts</h3></div><div class="table-wrap"><table><thead><tr><th>Account</th><th>Type</th><th>Balance</th></tr></thead><tbody><?php foreach ($balanceSheet as $row): ?><tr><td><?= e($row['code'].' '.$row['name']) ?></td><td><?= e(readable_status($row['account_type'])) ?></td><td><?= money($row['balance']) ?></td></tr><?php endforeach; ?></tbody></table></div></section>
</div>
<div class="content-grid two-col">
<section class="card table-card"><div class="card-header"><h3>Cash Activity</h3></div><div class="table-wrap"><table><thead><tr><th>Type</th><th>Amount</th></tr></thead><tbody><?php foreach ($cashFlow as $row): ?><tr><td><?= e(readable_status($row['transaction_type'])) ?></td><td><?= money($row['amount']) ?></td></tr><?php endforeach; ?><?php if (!$cashFlow): ?><tr><td colspan="2" class="empty-state">No bank activity in this period.</td></tr><?php endif; ?></tbody></table></div></section>
<section class="card table-card"><div class="card-header"><h3>Expenses by Category</h3></div><div class="table-wrap"><table><thead><tr><th>Category</th><th>Total</th></tr></thead><tbody><?php foreach ($expenseSummary as $row): ?><tr><td><?= e($row['category']) ?></td><td><?= money($row['total']) ?></td></tr><?php endforeach; ?><?php if (!$expenseSummary): ?><tr><td colspan="2" class="empty-state">No approved expenses in this period.</td></tr><?php endif; ?></tbody></table></div></section>
</div>
