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

<?= flashHtml(); ?>

<!-- ── Status tabs ────────────────────────────────────────── -->
<ul class="nav nav-tabs mb-3 small">
  <?php
    $tabs = [
      ''           => ['label'=>'All',       'count'=>array_sum($c) - $c['cancelled'] - $c['rejected'], 'bg'=>''],
      'draft'      => ['label'=>'Draft',     'count'=>$c['draft'],      'bg'=>'secondary'],
      'submitted'  => ['label'=>'Submitted', 'count'=>$c['submitted'],  'bg'=>'warning'],
      'reviewed'   => ['label'=>'Reviewed',  'count'=>$c['reviewed'],   'bg'=>'info'],
      'approved'   => ['label'=>'Approved',  'count'=>$c['approved'],   'bg'=>'primary'],
      'paid'       => ['label'=>'Paid',      'count'=>$c['paid'],       'bg'=>'success'],
      'rejected'   => ['label'=>'Rejected',  'count'=>$c['rejected'],   'bg'=>'danger'],
      'cancelled'  => ['label'=>'Cancelled', 'count'=>$c['cancelled'],  'bg'=>'dark'],
    ];
    foreach ($tabs as $st => $meta): ?>
      <li class="nav-item">
        <a class="nav-link <?= $f['status']===$st ? 'active' : ''; ?>"
           href="<?= PROOT; ?>Expenses<?= $st !== '' ? '?status=' . $st : ''; ?>">
          <?= htmlspecialchars($meta['label']); ?>
          <?php if ($meta['count'] > 0): ?><span class="badge bg-<?= $meta['bg'] ?: 'light text-muted border'; ?> ms-1"><?= (int)$meta['count']; ?></span><?php endif; ?>
        </a>
      </li>
  <?php endforeach; ?>
</ul>

<!-- ── Search & filter bar ────────────────────────────────── -->
<form method="GET" action="<?= PROOT; ?>Expenses/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>
        <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="Expense # · description · truck · driver · vendor" autocomplete="off">
        </div>
      </div>
      <div class="col-md-3">
        <label class="form-label small text-muted mb-1">Expense type</label>
        <select name="expense_type_id" class="form-select form-select-sm">
          <option value="">All types</option>
          <?php foreach ($this->data['types'] as $et): ?>
            <option value="<?= (int)$et['c0d']; ?>" <?= $f['expense_type_id']==$et['c0d']?'selected':''; ?>>
              <?= htmlspecialchars($et['display_name']); ?><?php if ($et['gl_code']): ?> · <?= htmlspecialchars($et['gl_code']); ?><?php endif; ?>
            </option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="col-md-2">
        <label class="form-label small text-muted mb-1">From date</label>
        <input type="date" name="from_date" class="form-control form-control-sm" value="<?= htmlspecialchars($f['from_date']); ?>">
      </div>
      <div class="col-md-2">
        <label class="form-label small text-muted mb-1">To date</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; ?>Expenses<?= $f['status'] ? '?status=' . htmlspecialchars($f['status']) : ''; ?>" class="btn btn-sm btn-outline-secondary" title="Clear"><i class="fas fa-times"></i></a>
      </div>
    </div>
  </div>
</form>

<!-- ── Results table ──────────────────────────────────────── -->
<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;">Expense #</th>
          <th>Type · GL</th>
          <th>Description</th>
          <th>Tagged</th>
          <th class="money">Amount</th>
          <th>Date</th>
          <th>Status</th>
          <th class="text-end"></th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($this->data['rows'] as $r): ?>
          <?php $bg = ['draft'=>'secondary','submitted'=>'warning','reviewed'=>'info','approved'=>'primary','paid'=>'success','rejected'=>'danger','cancelled'=>'dark'][$r['status']] ?? 'secondary'; ?>
          <tr>
            <td class="font-monospace small"><a href="<?= PROOT; ?>Expenses/view/<?= (int)$r['c0d']; ?>" class="text-decoration-none fw-semibold"><?= htmlspecialchars($r['expense_no']); ?></a></td>
            <td class="small">
              <?= htmlspecialchars($r['type_name'] ?? '—'); ?>
              <?php if ($r['gl_code']): ?><div class="text-muted font-monospace small"><?= htmlspecialchars($r['gl_code']); ?> <?= htmlspecialchars($r['gl_name']); ?></div><?php endif; ?>
            </td>
            <td class="small text-truncate" style="max-width:260px;"><?= htmlspecialchars($r['description'] ?: '—'); ?></td>
            <td class="small">
              <?php if ($r['truck_reg']):   ?><span class="badge bg-light text-dark border me-1"><i class="fas fa-truck fa-xs me-1"></i><?= htmlspecialchars($r['truck_reg']); ?></span><?php endif; ?>
              <?php if ($r['driver_name']): ?><span class="badge bg-light text-dark border me-1"><i class="fas fa-user fa-xs me-1"></i><?= htmlspecialchars($r['driver_name']); ?></span><?php endif; ?>
              <?php if ($r['trip_no']):     ?><span class="badge bg-light text-dark border me-1"><i class="fas fa-route fa-xs me-1"></i><?= htmlspecialchars($r['trip_no']); ?></span><?php endif; ?>
              <?php if ($r['vendor_name']): ?><span class="badge bg-light text-dark border me-1"><i class="fas fa-store fa-xs me-1"></i><?= htmlspecialchars($r['vendor_name']); ?></span><?php endif; ?>
            </td>
            <td class="money"><?= htmlspecialchars($r['currency']); ?> <?= number_format((float)$r['gross_amount'], 2, '.', ','); ?></td>
            <td class="small"><?= humanDate($r['expense_date']); ?></td>
            <td><span class="badge bg-<?= $bg; ?>" style="text-transform:capitalize;"><?= htmlspecialchars($r['status']); ?></span></td>
            <td class="text-end">
              <a href="<?= PROOT; ?>Expenses/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="8" class="text-center text-muted py-5">
              No expenses match those filters — <a href="<?= PROOT; ?>Expenses/add">record the first expense</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(); ?>
