<?php $this->start('body'); ?>
<?php $f = $this->data['form']; $w = $this->data['w']; ?>

<div class="d-flex align-items-center justify-content-between mb-3">
  <h5 class="mb-0 fw-semibold"><i class="fas fa-pen me-2 text-muted"></i>Edit · <?= htmlspecialchars($w['wr_no']); ?></h5>
  <a href="<?= PROOT; ?>WorkRequests/view/<?= (int)$w['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back to WR</a>
</div>

<?= $this->raw('fb_msg'); ?>

<form method="POST" action="<?= PROOT; ?>WorkRequests/update/<?= (int)$w['c0d']; ?>" class="card border-0 shadow-sm" autocomplete="off">
  <?= csrf_field(); ?>
  <div class="card-body">
    <div class="row g-3">
      <div class="col-md-6"><label class="form-label">Truck</label>
        <select name="truck_id" class="form-select">
          <?php foreach ($this->data['trucks'] as $t): ?>
            <option value="<?= (int)$t['c0d']; ?>" <?= ($f['truck_id'] ?? '')==$t['c0d']?'selected':''; ?>>
              <?= htmlspecialchars($t['reg_no']); ?><?php if ($t['make']): ?> · <?= htmlspecialchars(trim($t['make'] . ' ' . $t['model'])); ?><?php endif; ?>
            </option>
          <?php endforeach; ?>
        </select></div>
      <div class="col-md-6"><label class="form-label">Reported by</label>
        <select name="driver_id" class="form-select">
          <option value="">— none —</option>
          <?php foreach ($this->data['drivers'] as $d): ?>
            <option value="<?= (int)$d['c0d']; ?>" <?= ($f['driver_id'] ?? '')==$d['c0d']?'selected':''; ?>><?= htmlspecialchars($d['full_name']); ?></option>
          <?php endforeach; ?>
        </select></div>

      <div class="col-md-3"><label class="form-label">Urgency</label>
        <select name="urgency" class="form-select">
          <?php foreach (['low','normal','high','breakdown'] as $u): ?>
            <option value="<?= $u; ?>" <?= ($f['urgency'] ?? '')===$u?'selected':''; ?>><?= ucfirst($u); ?></option>
          <?php endforeach; ?>
        </select></div>
      <div class="col-md-3"><label class="form-label">Odometer</label>
        <input type="text" name="truck_odometer" class="form-control js-int" value="<?= htmlspecialchars((string)($f['truck_odometer'] ?? '')); ?>"></div>
      <div class="col-md-6"><label class="form-label">Fault location</label>
        <input type="text" name="fault_location" class="form-control" value="<?= htmlspecialchars($f['fault_location'] ?? ''); ?>"></div>

      <div class="col-12"><label class="form-label">Symptom</label>
        <textarea name="symptom" rows="4" class="form-control"><?= htmlspecialchars($f['symptom'] ?? ''); ?></textarea></div>
      <div class="col-12"><label class="form-label">Notes</label>
        <textarea name="notes" rows="2" class="form-control"><?= htmlspecialchars($f['notes'] ?? ''); ?></textarea></div>
    </div>
  </div>
  <div class="card-footer bg-white d-flex justify-content-end gap-2 py-3">
    <a href="<?= PROOT; ?>WorkRequests/view/<?= (int)$w['c0d']; ?>" class="btn btn-outline-secondary">Cancel</a>
    <button class="btn btn-primary"><i class="fas fa-save me-1"></i>Save changes</button>
  </div>
</form>

<?php $this->end(); ?>
<?php $this->start('section_js'); ?>
<script>document.querySelectorAll('.js-int').forEach(el => new Cleave(el, { numeral: true, numeralThousandsGroupStyle: 'thousand', numeralDecimalScale: 0 }));</script>
<?php $this->end(); ?>
