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

<?= flashHtml(); ?>

<form method="GET" action="<?= PROOT; ?>FuelEntries/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="Entry # · station · receipt · truck · driver" autocomplete="off">
        </div>
      </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="posted"    <?= $f['status']==='posted'?'selected':''; ?>>Posted</option>
          <option value="voided"    <?= $f['status']==='voided'?'selected':''; ?>>Voided</option>
          <option value="je_failed" <?= $f['status']==='je_failed'?'selected':''; ?>>JE failed</option>
        </select>
      </div>
      <div class="col-md-2">
        <label class="form-label small text-muted mb-1">Variance</label>
        <select name="variance_status" class="form-select form-select-sm">
          <option value="">All</option>
          <?php foreach (['pending','matched','investigating','adjusted','no_sensor'] as $v): ?>
            <option value="<?= $v; ?>" <?= $f['variance_status']===$v?'selected':''; ?>><?= ucfirst($v); ?></option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="col-md-2">
        <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-1">
        <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-1 d-flex gap-2">
        <button class="btn btn-sm btn-primary flex-grow-1" title="Apply"><i class="fas fa-filter"></i></button>
        <a href="<?= PROOT; ?>FuelEntries" 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:110px;">Entry #</th>
          <th>Date</th>
          <th>Truck / Driver</th>
          <th>Station</th>
          <th class="money">Litres</th>
          <th class="money">Amount</th>
          <th class="money">Odometer</th>
          <th>Payment</th>
          <th>Status</th>
          <th class="text-end"></th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($this->data['rows'] as $r): ?>
          <?php $bg = ['posted'=>'success','voided'=>'dark','je_failed'=>'danger','pending'=>'warning'][$r['status']] ?? 'secondary'; ?>
          <tr>
            <td class="font-monospace small"><a href="<?= PROOT; ?>FuelEntries/view/<?= (int)$r['c0d']; ?>" class="text-decoration-none fw-semibold"><?= htmlspecialchars($r['entry_no']); ?></a></td>
            <td class="small"><?= humanDate($r['entry_date']); ?><?php if ($r['entry_time']): ?> <span class="text-muted"><?= substr($r['entry_time'],0,5); ?></span><?php endif; ?></td>
            <td class="small">
              <?php if ($r['truck_reg']):   ?><a href="<?= PROOT; ?>Trucks/view/<?= (int)$r['truck_id']; ?>" class="text-decoration-none fw-semibold"><?= htmlspecialchars($r['truck_reg']); ?></a><?php endif; ?>
              <?php if ($r['driver_name']): ?><div class="text-muted"><i class="fas fa-user fa-xs me-1"></i><?= htmlspecialchars($r['driver_name']); ?></div><?php endif; ?>
            </td>
            <td class="small"><?= htmlspecialchars($r['station_name'] ?: '—'); ?></td>
            <td class="money"><?= number_format((float)$r['liters'], 2); ?> L</td>
            <td class="money"><?= htmlspecialchars($r['currency']); ?> <?= number_format((float)$r['gross_amount'], 0, '.', ','); ?></td>
            <td class="money"><?= $r['odometer_reading'] ? number_format((int)$r['odometer_reading']) : '—'; ?></td>
            <td class="small"><?= htmlspecialchars(ucwords(str_replace('_',' ',$r['payment_method']))); ?></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; ?>FuelEntries/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-outline-secondary" title="View"><i class="fas fa-eye"></i></a>
            </td>
          </tr>
        <?php endforeach; ?>
        <?php if (empty($this->data['rows'])): ?>
          <tr><td colspan="10" class="text-center text-muted py-5">
              No fuel entries yet — <a href="<?= PROOT; ?>FuelEntries/add">record the first refuel</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(); ?>
