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

<div class="d-flex align-items-center justify-content-between mb-3 flex-wrap gap-2">
  <h5 class="mb-0 fw-semibold"><i class="fas fa-file-invoice-dollar me-2 text-muted"></i>Customer Invoices
    <span class="text-muted fw-normal fs-6">· <?= (int)$this->data['total']; ?></span>
  </h5>
  <a href="<?= PROOT; ?>CustomerInvoices/billTrips" class="btn btn-sm btn-primary"><i class="fas fa-plus me-1"></i> Bill trips</a>
</div>

<?= flashHtml(); ?>

<!-- KPI strip -->
<div class="row g-2 mb-3">
  <div class="col-6 col-md-3"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2"><div class="text-uppercase text-muted small">Outstanding</div><div class="fs-5 fw-bold text-danger">TZS <?= number_format((float)$c['outstanding'], 0); ?></div></div></div></div>
  <div class="col-6 col-md-3"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2"><div class="text-uppercase text-muted small">Draft</div><div class="fs-5 fw-bold text-secondary"><?= (int)$c['draft']; ?></div></div></div></div>
  <div class="col-6 col-md-3"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2"><div class="text-uppercase text-muted small">Open</div><div class="fs-5 fw-bold text-warning"><?= (int)$c['issued'] + (int)$c['partially_paid']; ?></div></div></div></div>
  <div class="col-6 col-md-3"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2"><div class="text-uppercase text-muted small">Paid</div><div class="fs-5 fw-bold text-success"><?= (int)$c['paid']; ?></div></div></div></div>
</div>

<ul class="nav nav-tabs mb-3 small">
  <?php $tabs = ['' => 'All', 'draft'=>'Draft', 'issued'=>'Issued', 'partially_paid'=>'Partially paid', 'paid'=>'Paid', 'void'=>'Void'];
    foreach ($tabs as $st => $lab): ?>
    <li class="nav-item">
      <a class="nav-link <?= $f['status']===$st ? 'active' : ''; ?>"
         href="<?= PROOT; ?>CustomerInvoices<?= $st !== '' ? '?status=' . $st : ''; ?>"><?= htmlspecialchars($lab); ?></a>
    </li>
  <?php endforeach; ?>
  <li class="nav-item ms-auto">
    <a class="nav-link <?= $f['overdue'] ? 'active bg-danger text-white' : ''; ?>" href="<?= PROOT; ?>CustomerInvoices?overdue=1">
      <i class="fas fa-exclamation-triangle"></i> Overdue only
    </a>
  </li>
</ul>

<form method="GET" action="<?= PROOT; ?>CustomerInvoices/index/1" class="card border-0 shadow-sm mb-3">
  <input type="hidden" name="status" value="<?= htmlspecialchars($f['status']); ?>">
  <div class="card-body py-2">
    <div class="row g-2 align-items-end">
      <div class="col-md-4"><label class="form-label small text-muted mb-1">Search</label>
        <input type="text" class="form-control form-control-sm" name="q" value="<?= htmlspecialchars($f['q']); ?>" placeholder="Invoice # · customer"></div>
      <div class="col-md-3"><label class="form-label small text-muted mb-1">From</label>
        <input type="date" name="from_date" class="form-control form-control-sm" value="<?= htmlspecialchars($f['from_date']); ?>"></div>
      <div class="col-md-3"><label class="form-label small text-muted mb-1">To</label>
        <input type="date" name="to_date" class="form-control form-control-sm" value="<?= htmlspecialchars($f['to_date']); ?>"></div>
      <div class="col-md-2 d-flex gap-1">
        <button class="btn btn-sm btn-primary flex-grow-1"><i class="fas fa-filter"></i></button>
        <a href="<?= PROOT; ?>CustomerInvoices" class="btn btn-sm btn-outline-secondary"><i class="fas fa-times"></i></a>
      </div>
    </div>
  </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>Invoice #</th><th>Date</th><th>Due</th><th>Customer</th><th class="money">Total</th><th class="money">Paid</th><th class="money">Outstanding</th><th>Status</th><th class="text-end"></th></tr>
      </thead>
      <tbody>
      <?php foreach ($this->data['rows'] as $r):
        $out = (float)$r['total_amount'] - (float)$r['amount_paid'];
        $bg = ['draft'=>'secondary','issued'=>'primary','partially_paid'=>'warning','paid'=>'success','void'=>'dark'][$r['status']] ?? 'secondary';
        $overdue = in_array($r['status'], ['issued','partially_paid'], true) && (int)$r['days_overdue'] > 0;
      ?>
        <tr>
          <td class="small"><a href="<?= PROOT; ?>CustomerInvoices/view/<?= (int)$r['c0d']; ?>" class="fw-semibold text-decoration-none font-monospace"><?= htmlspecialchars($r['invoice_no']); ?></a></td>
          <td class="small text-muted"><?= humanDate($r['invoice_date']); ?></td>
          <td class="small <?= $overdue ? 'text-danger fw-semibold' : 'text-muted'; ?>">
            <?= $r['due_date'] ? humanDate($r['due_date']) : '—'; ?>
            <?php if ($overdue): ?><div class="small"><?= (int)$r['days_overdue']; ?> days late</div><?php endif; ?>
          </td>
          <td class="small"><a href="<?= PROOT; ?>Customers/view/<?= (int)$r['customer_id']; ?>" class="text-decoration-none"><?= htmlspecialchars($r['customer_name'] ?: '—'); ?></a></td>
          <td class="money"><?= htmlspecialchars($r['currency']); ?> <?= number_format((float)$r['total_amount'], 2, '.', ','); ?></td>
          <td class="money small text-success"><?= number_format((float)$r['amount_paid'], 2, '.', ','); ?></td>
          <td class="money fw-semibold"><?= number_format($out, 2, '.', ','); ?></td>
          <td><span class="badge bg-<?= $bg; ?>" style="text-transform:capitalize;"><?= htmlspecialchars(str_replace('_', ' ', $r['status'])); ?></span></td>
          <td class="text-end"><a href="<?= PROOT; ?>CustomerInvoices/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-eye"></i></a></td>
        </tr>
      <?php endforeach; ?>
      <?php if (empty($this->data['rows'])): ?>
        <tr><td colspan="9" class="text-center text-muted py-5">No invoices match — <a href="<?= PROOT; ?>CustomerInvoices/billTrips">bill some closed trips</a>.</td></tr>
      <?php endif; ?>
      </tbody>
    </table>
  </div>
  <?php if (!empty($this->data['pg_html'])): ?>
    <div class="card-footer bg-white d-flex justify-content-between py-2"><span class="small text-muted">Page <?= (int)$this->data['page']; ?> · <?= (int)$this->data['total']; ?> total</span><?= $this->data['pg_html']; ?></div>
  <?php endif; ?>
</div>

<?php $this->end(); ?>
