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

<?= flashHtml(); ?>

<form method="GET" action="<?= PROOT; ?>Geofences/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-5"><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 · notes" autocomplete="off"></div></div>
      <div class="col-md-3"><label class="form-label small text-muted mb-1">Kind</label>
        <select name="kind" class="form-select form-select-sm"><option value="">All</option>
          <?php foreach (['depot'=>'Depot','customer_site'=>'Customer site','project_site'=>'Project site','no_go'=>'No-go','route_corridor'=>'Route corridor','fuel_station'=>'Fuel station','weighbridge'=>'Weighbridge'] as $k=>$v): ?>
            <option value="<?= $k; ?>" <?= $f['kind']===$k?'selected':''; ?>><?= $v; ?></option>
          <?php endforeach; ?>
        </select></div>
      <div class="col-md-2"><label class="form-label small text-muted mb-1">Status</label>
        <select name="active" class="form-select form-select-sm">
          <option value="">All</option>
          <option value="1" <?= $f['active']===1?'selected':''; ?>>Active</option>
          <option value="0" <?= $f['active']===0?'selected':''; ?>>Inactive</option>
        </select></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; ?>Geofences" 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>Name</th><th>Kind</th><th>Center</th><th class="money">Radius</th><th>Alert on exit?</th><th>Status</th><th class="text-end"></th></tr>
      </thead>
      <tbody>
        <?php foreach ($this->data['rows'] as $r): ?>
          <tr>
            <td class="small"><a href="<?= PROOT; ?>Geofences/view/<?= (int)$r['c0d']; ?>" class="text-decoration-none fw-semibold" style="color: <?= htmlspecialchars($r['color'] ?: '#2626F5'); ?>"><i class="fas fa-map-marker-alt me-1"></i><?= htmlspecialchars($r['name']); ?></a></td>
            <td class="small text-muted" style="text-transform:capitalize;"><?= htmlspecialchars(str_replace('_',' ',$r['kind'])); ?></td>
            <td class="small font-monospace"><?= $r['center_lat'] !== null ? number_format((float)$r['center_lat'],4) . ', ' . number_format((float)$r['center_lng'],4) : '—'; ?></td>
            <td class="money small"><?= $r['radius_m'] ? number_format((int)$r['radius_m']) . ' m' : '—'; ?></td>
            <td><?php if ($r['trigger_alert']): ?><span class="badge bg-warning text-dark">Alert</span><?php else: ?><span class="text-muted small">—</span><?php endif; ?></td>
            <td><?php if ($r['active']): ?><span class="badge bg-success">Active</span><?php else: ?><span class="badge bg-secondary">Inactive</span><?php endif; ?></td>
            <td class="text-end"><a href="<?= PROOT; ?>Geofences/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="7" class="text-center text-muted py-5">No geofences — <a href="<?= PROOT; ?>Geofences/add">create the first one</a> (depot, customer site, or no-go zone).</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(); ?>
