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

<?= flashHtml(); ?>

<form method="GET" action="<?= PROOT; ?>WorkOrders/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="WO # · fault · truck · vendor" 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>
          <?php foreach (['open','in_progress','completed','closed','cancelled'] as $s): ?>
            <option value="<?= $s; ?>" <?= $f['status']===$s?'selected':''; ?>><?= ucfirst(str_replace('_',' ',$s)); ?></option>
          <?php endforeach; ?>
        </select></div>
      <div class="col-md-2"><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 (['planned','breakdown','callout','inspection','recall'] as $k): ?>
            <option value="<?= $k; ?>" <?= $f['kind']===$k?'selected':''; ?>><?= ucfirst($k); ?></option>
          <?php endforeach; ?>
        </select></div>
      <div class="col-md-2"><label class="form-label small text-muted mb-1">Priority</label>
        <select name="priority" class="form-select form-select-sm"><option value="">All</option>
          <?php foreach (['low','normal','high','urgent'] as $p): ?>
            <option value="<?= $p; ?>" <?= $f['priority']===$p?'selected':''; ?>><?= ucfirst($p); ?></option>
          <?php endforeach; ?>
        </select></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; ?>WorkOrders" 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;">WO #</th>
          <th>Truck</th>
          <th>Vendor</th>
          <th>Kind / Fault</th>
          <th>Opened</th>
          <th class="money">Cost</th>
          <th>Priority</th>
          <th>Status</th>
          <th class="text-end"></th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($this->data['rows'] as $r): ?>
          <?php $bg = ['open'=>'warning','in_progress'=>'info','completed'=>'primary','closed'=>'success','cancelled'=>'dark'][$r['status']] ?? 'secondary';
            $pbg = ['low'=>'light','normal'=>'light','high'=>'warning','urgent'=>'danger'][$r['priority']] ?? 'light'; ?>
          <tr>
            <td class="font-monospace small"><a href="<?= PROOT; ?>WorkOrders/view/<?= (int)$r['c0d']; ?>" class="text-decoration-none fw-semibold"><?= htmlspecialchars($r['wo_no']); ?></a></td>
            <td class="small"><?= htmlspecialchars($r['truck_reg'] ?? '?'); ?><?php if ($r['make']): ?><div class="text-muted"><?= htmlspecialchars(trim($r['make'] . ' ' . $r['model'])); ?></div><?php endif; ?></td>
            <td class="small"><?= htmlspecialchars($r['vendor_name'] ?: '—'); ?></td>
            <td class="small"><?= htmlspecialchars(ucfirst($r['kind'])); ?><?php if ($r['fault_description']): ?><div class="text-muted text-truncate" style="max-width:280px;"><?= htmlspecialchars($r['fault_description']); ?></div><?php endif; ?></td>
            <td class="small"><?= humanDate($r['opened_on']); ?></td>
            <td class="money"><?= $r['total_cost'] > 0 ? htmlspecialchars($r['currency']) . ' ' . number_format((float)$r['total_cost'], 0, '.', ',') : '—'; ?></td>
            <td><span class="badge bg-<?= $pbg; ?> <?= $r['priority']==='urgent'?'text-white':'text-dark'; ?>" style="text-transform:capitalize;"><?= htmlspecialchars($r['priority']); ?></span></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; ?>WorkOrders/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 orders yet — <a href="<?= PROOT; ?>WorkOrders/add">open the first one</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(); ?>
