<?php $this->start('body'); ?>
<?php $rows = $this->data['rows']; $suppliers = $this->data['suppliers']; $filters = $this->data['filters']; $counts = $this->data['counts']; ?>

<div class="d-flex align-items-center justify-content-between mb-3 flex-wrap gap-2">
  <div>
    <h5 class="mb-0 fw-semibold"><i class="fas fa-file-invoice me-2 text-muted"></i>Supplier Bills</h5>
    <div class="text-muted small">
      <?= (int)$counts['draft']; ?> draft · <?= (int)$counts['posted']; ?> posted · <?= (int)$counts['paid']; ?> paid ·
      <strong>TZS <?= number_format($counts['total_due'], 0); ?></strong> outstanding
    </div>
  </div>
  <a href="<?= PROOT; ?>SupplierBills/add" class="btn btn-sm btn-primary"><i class="fas fa-plus me-1"></i>New bill</a>
</div>

<?= flashHtml(); ?>

<form method="GET" class="card border-0 shadow-sm mb-3">
  <div class="card-body py-2 d-flex flex-wrap gap-2 align-items-end">
    <div>
      <label class="form-label small mb-1 text-uppercase text-muted">Search</label>
      <input type="text" name="q" class="form-control form-control-sm" value="<?= htmlspecialchars($filters['q']); ?>" placeholder="bill no · vendor · supplier ref">
    </div>
    <div>
      <label class="form-label small mb-1 text-uppercase text-muted">Supplier</label>
      <select name="vendor_id" class="form-select form-select-sm">
        <option value="0">All</option>
        <?php foreach ($suppliers as $s): ?>
          <option value="<?= (int)$s['c0d']; ?>" <?= (int)$filters['vendor_id'] === (int)$s['c0d'] ? 'selected' : ''; ?>><?= htmlspecialchars($s['vendor_name']); ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <div>
      <label class="form-label small mb-1 text-uppercase text-muted">Status</label>
      <select name="status" class="form-select form-select-sm">
        <?php foreach (['' => 'All', 'draft' => 'Draft', 'posted' => 'Posted', 'paid' => 'Paid', 'void' => 'Void'] as $k => $v): ?>
          <option value="<?= $k; ?>" <?= $filters['status'] === $k ? 'selected' : ''; ?>><?= $v; ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <div><label class="form-label small mb-1 text-uppercase text-muted">From</label><input type="date" name="from_date" value="<?= htmlspecialchars($filters['from_date']); ?>" class="form-control form-control-sm"></div>
    <div><label class="form-label small mb-1 text-uppercase text-muted">To</label>  <input type="date" name="to_date"   value="<?= htmlspecialchars($filters['to_date']);   ?>" class="form-control form-control-sm"></div>
    <button type="submit" class="btn btn-sm btn-primary"><i class="fas fa-search"></i></button>
  </div>
</form>

<div class="card border-0 shadow-sm">
  <div class="table-responsive">
    <table class="table table-sm mb-0 align-middle">
      <thead class="table-light">
        <tr>
          <th>Bill</th><th>Supplier</th><th>Bill date</th><th>Due</th>
          <th class="money">Total</th><th class="money">Paid</th><th class="money">Balance</th>
          <th>Status</th><th class="text-end"></th>
        </tr>
      </thead>
      <tbody>
        <?php if (empty($rows)): ?>
          <tr><td colspan="9" class="text-center text-muted py-5">No bills. <a href="<?= PROOT; ?>SupplierBills/add">Add the first one</a>.</td></tr>
        <?php else: ?>
          <?php foreach ($rows as $r):
            $bg = ['draft'=>'secondary','posted'=>'warning text-dark','paid'=>'success','void'=>'dark'][$r['status']] ?? 'light';
            $overdue = $r['status'] === 'posted' && (int)$r['days_overdue'] > 0;
          ?>
            <tr>
              <td><span class="font-monospace small"><?= htmlspecialchars($r['bill_no']); ?></span>
                <?php if ($r['supplier_ref']): ?><div class="text-muted small"><?= htmlspecialchars($r['supplier_ref']); ?></div><?php endif; ?>
              </td>
              <td class="small"><?= htmlspecialchars($r['vendor_name'] ?? '—'); ?></td>
              <td class="small"><?= humanDate($r['bill_date']); ?></td>
              <td class="small">
                <?= $r['due_date'] ? humanDate($r['due_date']) : '—'; ?>
                <?php if ($overdue): ?><span class="badge bg-danger ms-1"><?= (int)$r['days_overdue']; ?>d</span><?php endif; ?>
              </td>
              <td class="money font-monospace small"><?= number_format((float)$r['total_amount'], 2); ?></td>
              <td class="money font-monospace small text-success"><?= number_format((float)$r['amount_paid'], 2); ?></td>
              <td class="money font-monospace fw-semibold"><?= number_format((float)$r['balance_due'], 2); ?></td>
              <td><span class="badge bg-<?= $bg; ?>"><?= htmlspecialchars($r['status']); ?></span></td>
              <td class="text-end"><a href="<?= PROOT; ?>SupplierBills/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-outline-primary"><i class="fas fa-arrow-right"></i></a></td>
            </tr>
          <?php endforeach; ?>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>
<?= $this->raw('pg_html'); ?>
<?php $this->end(); ?>
