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

<?= flashHtml(); ?>

<!-- Status tabs -->
<ul class="nav nav-tabs mb-3 small">
  <?php
    $active = array_sum(array_intersect_key($c, array_flip(['draft','submitted','approved','sourcing','ready','wo_created'])));
    $tabs = [
      ''           => ['label'=>'Active',    'count'=>$active,          'bg'=>''],
      'submitted'  => ['label'=>'Awaiting Approval', 'count'=>$c['submitted'], 'bg'=>'warning'],
      'approved'   => ['label'=>'Approved · to source','count'=>$c['approved'], 'bg'=>'primary'],
      'sourcing'   => ['label'=>'Sourcing',  'count'=>$c['sourcing'],   'bg'=>'info'],
      'ready'      => ['label'=>'Ready · to convert', 'count'=>$c['ready'], 'bg'=>'success'],
      'wo_created' => ['label'=>'In WO',     'count'=>$c['wo_created'], 'bg'=>'primary'],
      'completed'  => ['label'=>'Completed', 'count'=>$c['completed'],  '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; ?>WorkRequests<?= $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>

<form method="GET" action="<?= PROOT; ?>WorkRequests/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-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="WR # · symptom · truck · driver" autocomplete="off"></div></div>
      <div class="col-md-3"><label class="form-label small text-muted mb-1">Urgency</label>
        <select name="urgency" class="form-select form-select-sm"><option value="">All</option>
          <?php foreach (['low'=>'Low','normal'=>'Normal','high'=>'High','breakdown'=>'Breakdown'] as $k=>$v): ?>
            <option value="<?= $k; ?>" <?= $f['urgency']===$k?'selected':''; ?>><?= $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-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; ?>WorkRequests<?= $f['status'] ? '?status=' . htmlspecialchars($f['status']) : ''; ?>" 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:100px;">WR #</th><th>Reported</th><th>Truck</th><th>Symptom</th><th>Urgency</th><th class="money">Est.</th><th>Status</th><th>Linked WO</th><th class="text-end"></th></tr>
      </thead>
      <tbody>
        <?php foreach ($this->data['rows'] as $r): ?>
          <?php
            $bg = ['draft'=>'secondary','submitted'=>'warning','approved'=>'primary','sourcing'=>'info','ready'=>'success','wo_created'=>'primary','completed'=>'success','rejected'=>'danger','cancelled'=>'dark'][$r['status']] ?? 'secondary';
            $urg = ['low'=>['light','text-muted'],'normal'=>['light','text-muted'],'high'=>['warning','text-dark'],'breakdown'=>['danger','text-white']][$r['urgency']] ?? ['light','text-muted'];
          ?>
          <tr>
            <td class="font-monospace small"><a href="<?= PROOT; ?>WorkRequests/view/<?= (int)$r['c0d']; ?>" class="text-decoration-none fw-semibold"><?= htmlspecialchars($r['wr_no']); ?></a></td>
            <td class="small"><?= $r['reported_at'] ? humanDateTime($r['reported_at']) : '—'; ?><?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 fw-semibold"><?= htmlspecialchars($r['truck_reg'] ?? '?'); ?><?php if ($r['make']): ?><div class="text-muted fw-normal"><?= htmlspecialchars(trim($r['make'] . ' ' . $r['model'])); ?></div><?php endif; ?></td>
            <td class="small text-truncate" style="max-width:280px;"><?= htmlspecialchars($r['symptom'] ?: '—'); ?></td>
            <td><span class="badge bg-<?= $urg[0]; ?> <?= $urg[1]; ?><?= $urg[0]==='light'?' border':''; ?>" style="text-transform:capitalize;"><?= htmlspecialchars($r['urgency']); ?></span></td>
            <td class="money"><?= (float)$r['estimated_cost'] > 0 ? htmlspecialchars($r['currency']) . ' ' . number_format((float)$r['estimated_cost'], 0, '.', ',') : '—'; ?></td>
            <td><span class="badge bg-<?= $bg; ?>" style="text-transform:capitalize;"><?= htmlspecialchars(str_replace('_',' ',$r['status'])); ?></span></td>
            <td class="small"><?php if ($r['wo_no']): ?><a href="<?= PROOT; ?>WorkOrders/view/<?= (int)$r['wo_id']; ?>" class="font-monospace text-decoration-none"><?= htmlspecialchars($r['wo_no']); ?></a><?php else: ?><span class="text-muted">—</span><?php endif; ?></td>
            <td class="text-end"><a href="<?= PROOT; ?>WorkRequests/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="9" class="text-center text-muted py-5">No work requests match — <a href="<?= PROOT; ?>WorkRequests/add">report the first fault</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(); ?>
