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

<?= flashHtml(); ?>

<form method="GET" action="<?= PROOT; ?>Vendors/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-6">
        <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="Name · vendor # · TIN · city · category" autocomplete="off">
        </div>
      </div>
      <div class="col-md-4">
        <label class="form-label small text-muted mb-1">Category</label>
        <select name="category" class="form-select form-select-sm">
          <option value="">All</option>
          <?php foreach (['fuel_supplier'=>'Fuel supplier','workshop'=>'External workshop','spare_parts'=>'Spare parts','tyres'=>'Tyres','lubricants'=>'Lubricants','insurance'=>'Insurance','landlord'=>'Landlord','utilities'=>'Utilities','telecoms'=>'Telecoms','other'=>'Other'] as $k=>$v): ?>
            <option value="<?= $k; ?>" <?= $f['category']===$k?'selected':''; ?>><?= $v; ?></option>
          <?php endforeach; ?>
        </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"></i></button>
        <a href="<?= PROOT; ?>Vendors" 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 style="width:90px;">Vendor #</th>
          <th>Name</th>
          <th>Category</th>
          <th>TIN / VRN</th>
          <th>Phone</th>
          <th>City</th>
          <th class="text-end"></th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($this->data['rows'] as $r): ?>
          <tr>
            <td class="font-monospace small"><?= htmlspecialchars($r['vendor_no']); ?></td>
            <td><a href="<?= PROOT; ?>Vendors/view/<?= (int)$r['c0d']; ?>" class="text-decoration-none fw-semibold"><?= htmlspecialchars($r['vendor_name']); ?></a></td>
            <td><?php if ($r['vendor_category']): ?><span class="badge bg-light text-dark border"><?= htmlspecialchars(ucwords(str_replace('_',' ',$r['vendor_category']))); ?></span><?php endif; ?></td>
            <td class="font-monospace small"><?= htmlspecialchars($r['vendor_tin'] ?: '—'); ?><?php if ($r['vendor_vrn']): ?><div class="text-muted"><?= htmlspecialchars($r['vendor_vrn']); ?></div><?php endif; ?></td>
            <td class="small"><?= htmlspecialchars($r['vendor_phone_1'] ?: '—'); ?></td>
            <td class="small"><?= htmlspecialchars($r['vendor_city'] ?: '—'); ?></td>
            <td class="text-end">
              <a href="<?= PROOT; ?>Vendors/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-eye"></i></a>
              <a href="<?= PROOT; ?>Vendors/edit/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-pen"></i></a>
            </td>
          </tr>
        <?php endforeach; ?>
        <?php if (empty($this->data['rows'])): ?>
          <tr><td colspan="7" class="text-center text-muted py-5">No vendors yet — <a href="<?= PROOT; ?>Vendors/add">add the first vendor</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(); ?>
