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

<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-building me-2 text-muted"></i>Customers
      <span class="text-muted fw-normal fs-6">· <?= (int)$this->data['total']; ?></span>
  </h5>
  <a href="<?= PROOT; ?>Customers/add" class="btn btn-sm btn-primary"><i class="fas fa-plus me-1"></i> Add Customer</a>
</div>

<?= flashHtml(); ?>

<form method="GET" action="<?= PROOT; ?>Customers/index/1" class="card border-0 shadow-sm mb-3">
  <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>
        <div class="input-group input-group-sm">
          <span class="input-group-text bg-white"><i class="fas fa-search text-muted"></i></span>
          <input type="text" class="form-control" name="q" value="<?= htmlspecialchars($f['q']); ?>"
                 placeholder="Company · TIN · VRN · city · contact person" autocomplete="off">
        </div>
      </div>
      <div class="col-md-2">
        <label class="form-label small text-muted mb-1">Tier</label>
        <select name="tier" class="form-select form-select-sm">
          <option value="">All</option>
          <?php foreach (['RETAIL','CORPORATE','GOVERNMENT','NGO','SME'] as $t): ?>
            <option value="<?= $t; ?>" <?= $f['tier']===$t?'selected':''; ?>><?= ucwords(strtolower($t)); ?></option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="col-md-2">
        <label class="form-label small text-muted mb-1">Sector</label>
        <select name="sector" class="form-select form-select-sm">
          <option value="">All</option>
          <?php foreach (['CONSTRUCTION','MANUFACTURING','MINING','RETAIL','GOVERNMENT','LOGISTICS','FMCG','AGRICULTURE','OTHER'] as $s): ?>
            <option value="<?= $s; ?>" <?= $f['sector']===$s?'selected':''; ?>><?= ucwords(strtolower($s)); ?></option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="col-md-2">
        <label class="form-label small text-muted mb-1">Status</label>
        <select name="status" class="form-select form-select-sm">
          <option value="">All</option>
          <option value="ACTIVE"   <?= $f['status']==='ACTIVE'?'selected':''; ?>>Active</option>
          <option value="INACTIVE" <?= $f['status']==='INACTIVE'?'selected':''; ?>>Inactive</option>
          <option value="ON_HOLD"  <?= $f['status']==='ON_HOLD'?'selected':''; ?>>On hold</option>
        </select>
      </div>
      <div class="col-md-2 d-flex gap-2">
        <button class="btn btn-sm btn-primary flex-grow-1"><i class="fas fa-filter me-1"></i>Apply</button>
        <a href="<?= PROOT; ?>Customers" class="btn btn-sm btn-outline-secondary" title="Clear"><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 style="width:90px;">Customer #</th>
          <th>Company</th>
          <th>Contact person</th>
          <th>Phone</th>
          <th>City</th>
          <th>Tier</th>
          <th class="money">Credit limit</th>
          <th>Terms</th>
          <th>Status</th>
          <th class="text-end"></th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($this->data['rows'] as $r): ?>
          <tr>
            <td class="text-muted small font-monospace"><?= htmlspecialchars($r['customer_no']); ?></td>
            <td>
              <a href="<?= PROOT; ?>Customers/view/<?= (int)$r['c0d']; ?>" class="text-decoration-none fw-semibold"><?= htmlspecialchars($r['fname']); ?></a>
              <?php if ($r['lname']): ?><div class="text-muted small fst-italic"><?= htmlspecialchars($r['lname']); ?></div><?php endif; ?>
            </td>
            <td>
              <?= htmlspecialchars($r['customer_poc'] ?: '—'); ?>
            </td>
            <td class="small"><?= htmlspecialchars($r['phone_1'] ?: '—'); ?></td>
            <td class="small"><?= htmlspecialchars($r['city'] ?: '—'); ?></td>
            <td><span class="badge bg-light text-dark border"><?= htmlspecialchars(ucwords(strtolower($r['customer_tier']))); ?></span></td>
            <td class="money"><?= $r['credit_limit'] > 0 ? htmlspecialchars($r['currency']) . ' ' . number_format((float)$r['credit_limit'], 0, '.', ',') : '—'; ?></td>
            <td class="small"><?= (int)$r['payment_terms_days'] > 0 ? 'Net ' . (int)$r['payment_terms_days'] : 'Cash'; ?></td>
            <td>
              <?php $bg = ['ACTIVE'=>'success','INACTIVE'=>'secondary','ON_HOLD'=>'warning'][$r['status']] ?? 'secondary'; ?>
              <span class="badge bg-<?= $bg; ?>" style="text-transform:capitalize;"><?= htmlspecialchars(strtolower(str_replace('_',' ',$r['status']))); ?></span>
            </td>
            <td class="text-end">
              <a href="<?= PROOT; ?>Customers/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-outline-secondary" title="View"><i class="fas fa-eye"></i></a>
              <a href="<?= PROOT; ?>Customers/edit/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-outline-secondary" title="Edit"><i class="fas fa-pen"></i></a>
            </td>
          </tr>
        <?php endforeach; ?>
        <?php if (empty($this->data['rows'])): ?>
          <tr><td colspan="10" class="text-center text-muted py-5">
              No customers match those filters — <a href="<?= PROOT; ?>Customers/add">add the first customer</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 align-items-center 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(); ?>
