<?php include APP_PATH.'/views/partials/report_header.php'; ?>

<div class="rpt-btn-bar no-print">
  <div class="d-flex gap-2 flex-wrap">
    <?php $sc=[['Today',date('Y-m-d'),date('Y-m-d')],['This Week',date('Y-m-d',strtotime('monday this week')),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'))]];
    foreach($sc as[$l,$f,$t]):$a=$from===$f&&$to===$t;?>
    <a href="?from=<?=$f?>&to=<?=$t?>&group=<?=e($groupBy)?>&supplier=<?=$supplierId?>&payment=<?=e($paidF)?>" 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/purchases" class="d-flex gap-2 flex-wrap align-items-end mt-2 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">Supplier</label>
      <select name="supplier" class="form-select form-select-sm">
        <option value="">All Suppliers</option>
        <?php foreach($suppliers as$s):?><option value="<?=$s['id']?>" <?=$supplierId==$s['id']?'selected':''?>><?=e($s['supplier_name'])?></option><?php endforeach;?>
      </select>
    </div>
    <div><label class="form-label small mb-1">Payment</label>
      <select name="payment" class="form-select form-select-sm">
        <option value="all" <?=$paidF==='all'?'selected':''?>>All</option>
        <option value="paid" <?=$paidF==='paid'?'selected':''?>>Paid</option>
        <option value="partial" <?=$paidF==='partial'?'selected':''?>>Partial</option>
        <option value="due" <?=$paidF==='due'?'selected':''?>>Due</option>
      </select>
    </div>
    <button class="btn btn-secondary btn-sm align-self-end"><i class="bi bi-funnel me-1"></i>Filter</button>
    <a href="<?=APP_URL?>/reports/purchases" class="btn btn-outline-secondary btn-sm align-self-end">Clear</a>
  </form>
</div>

<!-- KPIs -->
<div class="row g-2 mb-3">
  <div class="col-6 col-md-3"><div class="card text-center py-3"><div class="fw-bold fs-5"><?=number_format($totals['count'])?></div><small class="text-muted">Purchase Orders</small></div></div>
  <div class="col-6 col-md-3"><div class="card text-center py-3 border-primary"><div class="fw-bold text-primary"><?=currency($totals['total'])?></div><small class="text-muted">Total Value</small></div></div>
  <div class="col-6 col-md-3"><div class="card text-center py-3 border-success"><div class="fw-bold text-success"><?=currency($totals['paid'])?></div><small class="text-muted">Paid</small></div></div>
  <div class="col-6 col-md-3"><div class="card text-center py-3 border-danger"><div class="fw-bold text-danger"><?=currency($totals['balance'])?></div><small class="text-muted">Balance Due</small></div></div>
</div>

<!-- Status badges -->
<div class="d-flex gap-2 mb-3 no-print">
  <span class="badge bg-success fs-6 px-3 py-2">✓ Paid: <?=$paidCount?></span>
  <span class="badge bg-warning text-dark fs-6 px-3 py-2">~ Partial: <?=$partialCount?></span>
  <span class="badge bg-danger fs-6 px-3 py-2">✗ Due: <?=$dueCount?></span>
</div>

<!-- Supplier Breakdown -->
<?php if(!empty($bySupplier)):?>
<div class="card mb-3">
  <div class="card-header fw-semibold small">By Supplier</div>
  <div class="card-body p-0">
    <table class="table table-sm mb-0">
      <thead class="table-secondary"><tr><th>#</th><th>Supplier</th><th class="text-end">Orders</th><th class="text-end">Total</th><th class="text-end">Paid</th><th class="text-end">Balance</th></tr></thead>
      <tbody>
        <?php foreach($bySupplier as$i=>$s):?>
        <tr>
          <td class="text-muted small"><?=$i+1?></td>
          <td class="fw-semibold"><?=e($s['supplier_name'])?></td>
          <td class="text-end"><?=number_format($s['count'])?></td>
          <td class="text-end"><?=currency($s['total'])?></td>
          <td class="text-end text-success"><?=currency($s['paid'])?></td>
          <td class="text-end <?=$s['balance']>0?'fw-bold text-danger':''?>"><?=$s['balance']>0?currency($s['balance']):'—'?></td>
        </tr>
        <?php endforeach;?>
      </tbody>
    </table>
  </div>
</div>
<?php endif;?>

<!-- Purchase List -->
<div class="card">
  <div class="card-header fw-semibold small d-flex justify-content-between">
    <span>Purchase Orders — <?=fDate($from)?> to <?=fDate($to)?></span>
    <small class="text-muted"><?=count($rows)?> records</small>
  </div>
  <div class="card-body p-0">
    <table class="table table-sm table-hover mb-0">
      <thead class="table-dark"><tr><th>#</th><th>Invoice</th><th>Supplier</th><th>Date</th><th class="text-end">Total</th><th class="text-end">Paid</th><th class="text-end">Balance</th><th>Status</th></tr></thead>
      <tbody>
        <?php foreach($rows as$i=>$r):
          $ps=$r['payment_status'];
          $rowCls=$ps==='due'?'table-danger bg-opacity-5':($ps==='partial'?'table-warning bg-opacity-5':'');
        ?>
        <tr class="<?=$rowCls?>">
          <td class="text-muted small"><?=$i+1?></td>
          <td><a href="<?=APP_URL?>/purchases/view/<?=$r['id']?>" class="fw-semibold text-decoration-none no-print"><?=e($r['invoice_no'])?></a><span class="print-only fw-semibold"><?=e($r['invoice_no'])?></span></td>
          <td class="small fw-semibold"><?=e($r['supplier_name'])?></td>
          <td class="small"><?=fDate($r['purchase_date'],'d M Y')?></td>
          <td class="text-end fw-semibold"><?=currency($r['total'])?></td>
          <td class="text-end text-success"><?=currency($r['paid'])?></td>
          <td class="text-end <?=$r['balance']>0?'fw-bold text-danger':''?>"><?=$r['balance']>0?currency($r['balance']):'—'?></td>
          <td><span class="badge <?=$ps==='paid'?'bg-success':($ps==='partial'?'bg-warning text-dark':'bg-danger')?>"><?=ucfirst($ps)?></span></td>
        </tr>
        <?php endforeach;if(empty($rows)):?><tr><td colspan="8" class="text-center text-muted py-4">No purchases found.</td></tr><?php endif;?>
      </tbody>
      <tfoot class="table-secondary fw-bold"><tr><td colspan="4">TOTAL</td><td class="text-end"><?=currency($totals['total'])?></td><td class="text-end text-success"><?=currency($totals['paid'])?></td><td class="text-end text-danger"><?=currency($totals['balance'])?></td><td></td></tr></tfoot>
    </table>
  </div>
</div>
<style>.print-only{display:none}@media print{.print-only{display:inline}}</style>