<?php $this->start('body'); ?>
<?php $b = $this->data['buckets']; $filter = $this->data['filter']; ?>

<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-file-signature me-2 text-muted"></i>Document Expiries
    <span class="text-muted fw-normal fs-6">· <?= count($this->data['rows']); ?></span>
  </h5>
  <form method="POST" action="<?= PROOT; ?>DocExpiries/sweepNow" class="d-inline"><?= csrf_field(); ?>
    <button class="btn btn-sm btn-outline-primary" title="Run the doc-expiry sweep now without waiting for cron"><i class="fas fa-sync me-1"></i>Sweep now</button>
  </form>
</div>

<?= flashHtml(); ?>

<div class="row g-2 mb-3">
  <div class="col-6 col-md"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2"><div class="text-uppercase text-muted small">Expired</div><div class="fs-5 fw-bold text-danger"><?= (int)$b['expired']; ?></div></div></div></div>
  <div class="col-6 col-md"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2"><div class="text-uppercase text-muted small">≤ 7 days</div><div class="fs-5 fw-bold text-danger"><?= (int)$b['0_7']; ?></div></div></div></div>
  <div class="col-6 col-md"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2"><div class="text-uppercase text-muted small">8–14 days</div><div class="fs-5 fw-bold text-warning"><?= (int)$b['8_14']; ?></div></div></div></div>
  <div class="col-6 col-md"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2"><div class="text-uppercase text-muted small">15–30 days</div><div class="fs-5 fw-bold text-info"><?= (int)$b['15_30']; ?></div></div></div></div>
  <div class="col-6 col-md"><div class="card border-0 shadow-sm h-100"><div class="card-body py-2"><div class="text-uppercase text-muted small">30+ days</div><div class="fs-5 fw-bold text-success"><?= (int)$b['30_plus']; ?></div></div></div></div>
</div>

<ul class="nav nav-tabs mb-3 small">
  <?php $tabs = ['' => 'All', 'expired' => 'Expired', '0_7' => '≤ 7 days', '8_14' => '8-14 days', '15_30' => '15-30 days', '30_plus' => '30+ days'];
    foreach ($tabs as $k => $lab): ?>
    <li class="nav-item">
      <a class="nav-link <?= $filter === $k ? 'active' : ''; ?>" href="<?= PROOT; ?>DocExpiries<?= $k !== '' ? '?bucket=' . $k : ''; ?>"><?= htmlspecialchars($lab); ?></a>
    </li>
  <?php endforeach; ?>
</ul>

<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>Subject</th><th>Document</th><th>Doc #</th><th>Issuer</th><th>Expires</th><th class="money">Days</th><th>Status</th><th class="text-end"></th></tr>
      </thead>
      <tbody>
        <?php foreach ($this->data['rows'] as $r):
          $d = (int)$r['days_remaining'];
          if     ($d < 0)   { $bg = 'danger';  $lab = abs($d) . ' days ago'; }
          elseif ($d <= 7)  { $bg = 'danger';  $lab = $d . ' d'; }
          elseif ($d <= 14) { $bg = 'warning'; $lab = $d . ' d'; }
          elseif ($d <= 30) { $bg = 'info';    $lab = $d . ' d'; }
          else              { $bg = 'success'; $lab = $d . ' d'; }
          $subjLink = $r['source'] === 'truck'
              ? PROOT . 'Trucks/view/'  . (int)$r['subject_id']
              : PROOT . 'Drivers/view/' . (int)$r['subject_id'];
        ?>
          <tr>
            <td class="small">
              <span class="badge bg-<?= $r['source'] === 'truck' ? 'primary' : 'info'; ?>" style="font-size:.6em;text-transform:uppercase;"><?= htmlspecialchars($r['source']); ?></span>
              <a href="<?= htmlspecialchars($subjLink); ?>" class="fw-semibold text-decoration-none ms-1"><?= htmlspecialchars($r['subject_name']); ?></a>
              <?php if ($r['subject_detail']): ?><div class="text-muted small ms-4"><?= htmlspecialchars($r['subject_detail']); ?></div><?php endif; ?>
            </td>
            <td class="small"><?= htmlspecialchars($r['doc_type_name']); ?></td>
            <td class="small font-monospace text-muted"><?= htmlspecialchars($r['doc_no'] ?: '—'); ?></td>
            <td class="small text-muted"><?= htmlspecialchars($r['issuer'] ?: '—'); ?></td>
            <td class="small"><?= humanDate($r['expires_on']); ?></td>
            <td class="money small"><?= $d >= 0 ? number_format($d) : '<span class="text-danger">' . number_format(abs($d)) . ' over</span>'; ?></td>
            <td><span class="badge bg-<?= $bg; ?>"><?= htmlspecialchars($lab); ?></span></td>
            <td class="text-end">
              <?php if ($r['file_url']): ?>
                <a href="<?= htmlspecialchars($r['file_url']); ?>" target="_blank" rel="noopener" class="btn btn-sm btn-outline-secondary" title="Open document"><i class="fas fa-file"></i></a>
              <?php endif; ?>
              <a href="<?= htmlspecialchars($subjLink); ?>" class="btn btn-sm btn-outline-primary"><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">
            <?php if ($filter): ?>Nothing in this bucket.<?php else: ?>No documents with expiry dates on file. Upload one via <a href="<?= PROOT; ?>Trucks">Trucks</a> or <a href="<?= PROOT; ?>Drivers">Drivers</a>.<?php endif; ?>
          </td></tr>
        <?php endif; ?>
      </tbody>
    </table>
  </div>
</div>

<?php $this->end(); ?>
