<?php include APP_PATH.'/views/partials/report_header.php'; ?>
<div class="rpt-btn-bar no-print">
  <div class="d-flex gap-2 flex-wrap mb-2">
    <?php foreach([['Today',date('Y-m-d'),date('Y-m-d')],['This Month',date('Y-m-01'),date('Y-m-d')],['Last Month',date('Y-m-01',strtotime('first day of last month')),date('Y-m-t',strtotime('last month'))],['This Year',date('Y-01-01'),date('Y-m-d')]] as[$l,$f,$t]):$a=$from===$f&&$to===$t;?>
    <a href="?from=<?=$f?>&to=<?=$t?>&category=<?=$catId?>" class="btn btn-sm <?=$a?'btn-primary':'btn-outline-secondary'?> py-0 px-2"><?=$l?></a>
    <?php endforeach;?>
  </div>
  <form method="GET" action="<?=APP_URL?>/reports/expenses" class="d-flex gap-2 flex-wrap align-items-end filter-form">
    <div><label class="form-label small mb-1">From</label><input type="date" name="from" class="form-control form-control-sm" value="<?=e($from)?>"></div>
    <div><label class="form-label small mb-1">To</label><input type="date" name="to" class="form-control form-control-sm" value="<?=e($to)?>"></div>
    <div><label class="form-label small mb-1">Category</label><select name="category" class="form-select form-select-sm"><option value="">All</option><?php foreach($categories as$c):?><option value="<?=$c['id']?>" <?=$catId==$c['id']?'selected':''?>><?=e($c['category_name'])?></option><?php endforeach;?></select></div>
    <button class="btn btn-secondary btn-sm align-self-end">Filter</button>
    <a href="<?=APP_URL?>/reports/expenses" class="btn btn-outline-secondary btn-sm align-self-end">Clear</a>
  </form>
</div>

<div class="row g-2 mb-3 no-break">
  <div class="col-6 col-md-3"><div class="card text-center py-3 border-danger"><div class="fw-bold fs-5 text-danger"><?=currency($totalAmt)?></div><small class="text-muted">Total Expenses</small></div></div>
  <div class="col-6 col-md-3"><div class="card text-center py-3"><div class="fw-bold fs-5"><?=number_format($totalCount)?></div><small class="text-muted">Transactions</small></div></div>
  <div class="col-6 col-md-3"><div class="card text-center py-3"><div class="fw-bold"><?=currency($avgDaily)?></div><small class="text-muted">Daily Average</small></div></div>
  <div class="col-6 col-md-3"><div class="card text-center py-3"><div class="fw-bold"><?=currency($avgPerTxn)?></div><small class="text-muted">Per Transaction</small></div></div>
</div>

<div class="card mb-3 no-break">
  <div class="card-header fw-semibold small">Expense by Category &nbsp;|&nbsp; <?=fDate($from,'d M Y')?> – <?=fDate($to,'d M Y')?></div>
  <div class="table-responsive"><table class="table table-sm mb-0">
    <thead class="table-dark"><tr><th>#</th><th>Category</th><th class="text-end">Count</th><th class="text-end">Total</th><th class="text-end">% of Total</th><th class="text-end">Largest Single</th></tr></thead>
    <tbody>
      <?php foreach($byCategory as$i=>$c): $pct=$totalAmt>0?round($c['total']/$totalAmt*100,1):0;?>
      <tr><td class="text-muted"><?=$i+1?></td><td class="fw-semibold"><?=e($c['category_name'])?></td><td class="text-end"><?=number_format($c['count'])?></td><td class="text-end fw-bold text-danger"><?=currency($c['total'])?></td><td class="text-end"><?=$pct?>%</td><td class="text-end text-muted small"><?=currency($c['max_single'])?></td></tr>
      <?php endforeach; if(empty($byCategory)):?><tr><td colspan="6" class="text-center text-muted py-4">No expenses found.</td></tr><?php endif;?>
    </tbody>
    <tfoot class="table-secondary fw-bold"><tr><td colspan="2">TOTAL</td><td class="text-end"><?=number_format($totalCount)?></td><td class="text-end text-danger"><?=currency($totalAmt)?></td><td class="text-end">100%</td><td></td></tr></tfoot>
  </table></div>
</div>

<?php if(!empty($topExpenses)):?>
<div class="card no-break">
  <div class="card-header fw-semibold small">Top Individual Expenses</div>
  <div class="table-responsive"><table class="table table-sm mb-0">
    <thead class="table-dark"><tr><th>#</th><th>Title</th><th>Category</th><th>Date</th><th>Recorded By</th><th class="text-end">Amount</th></tr></thead>
    <tbody>
      <?php foreach($topExpenses as$i=>$e):?>
      <tr><td class="text-muted"><?=$i+1?></td><td class="fw-semibold small"><?=e($e['title'])?></td><td class="small text-muted"><?=e($e['category_name'])?></td><td class="small"><?=fDate($e['expense_date'])?></td><td class="small text-muted"><?=e($e['recorded_by']??'—')?></td><td class="text-end fw-bold text-danger"><?=currency($e['amount'])?></td></tr>
      <?php endforeach;?>
    </tbody>
  </table></div>
</div>
<?php endif;?>