<?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['wo_no']); ?></h5>
  <a href="<?= PROOT; ?>WorkOrders/view/<?= (int)$w['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back to WO</a>
</div>

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

<form method="POST" action="<?= PROOT; ?>WorkOrders/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">Vendor / Workshop</label>
        <select name="vendor_id" class="form-select">
          <option value="">— internal workshop —</option>
          <?php foreach ($this->data['vendors'] as $v): ?>
            <option value="<?= (int)$v['c0d']; ?>" <?= ($f['vendor_id'] ?? '')==$v['c0d']?'selected':''; ?>><?= htmlspecialchars($v['vendor_name']); ?></option>
          <?php endforeach; ?>
        </select></div>

      <div class="col-md-3"><label class="form-label">Kind</label>
        <select name="kind" class="form-select">
          <?php foreach (['planned'=>'Planned','breakdown'=>'Breakdown','callout'=>'Callout','inspection'=>'Inspection','recall'=>'Recall'] as $k=>$vv): ?>
            <option value="<?= $k; ?>" <?= ($f['kind'] ?? '')===$k?'selected':''; ?>><?= $vv; ?></option>
          <?php endforeach; ?>
        </select></div>
      <div class="col-md-3"><label class="form-label">Priority</label>
        <select name="priority" class="form-select">
          <?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-3"><label class="form-label">Opened on</label><input type="date" name="opened_on" class="form-control" value="<?= htmlspecialchars($f['opened_on'] ?? ''); ?>"></div>
      <div class="col-md-3"><label class="form-label">Odometer at open</label><input type="text" name="opened_km" class="form-control js-int" value="<?= htmlspecialchars((string)($f['opened_km'] ?? '')); ?>"></div>

      <div class="col-12"><label class="form-label">Fault description</label><textarea name="fault_description" rows="3" class="form-control"><?= htmlspecialchars($f['fault_description'] ?? ''); ?></textarea></div>
      <div class="col-12"><label class="form-label">Work performed</label><textarea name="work_performed" rows="3" class="form-control"><?= htmlspecialchars($f['work_performed'] ?? ''); ?></textarea></div>

      <hr class="my-2">
      <div class="col-md-3"><label class="form-label">Labour hours</label><input type="text" name="labour_hours" class="form-control js-num" value="<?= htmlspecialchars((string)($f['labour_hours'] ?? '')); ?>"></div>
      <div class="col-md-3"><label class="form-label">Labour rate</label><input type="text" name="labour_rate" class="form-control js-money" value="<?= htmlspecialchars((string)($f['labour_rate'] ?? '')); ?>"></div>
      <div class="col-md-3"><label class="form-label">Labour cost</label><input type="text" name="labour_cost" class="form-control js-money" value="<?= htmlspecialchars((string)($f['labour_cost'] ?? '')); ?>"></div>
      <div class="col-md-3"><label class="form-label">External invoice cost</label><input type="text" name="external_cost" class="form-control js-money" value="<?= htmlspecialchars((string)($f['external_cost'] ?? '')); ?>"></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; ?>WorkOrders/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 }));
document.querySelectorAll('.js-num').forEach(el => new Cleave(el, { numeral: true, numeralThousandsGroupStyle: 'thousand', numeralDecimalScale: 2 }));
document.querySelectorAll('.js-money').forEach(el => new Cleave(el, { numeral: true, numeralThousandsGroupStyle: 'thousand', numeralDecimalScale: 2 }));

// Auto: labour_cost = hours × rate when both filled and labour_cost is empty
const lh = document.querySelector('input[name="labour_hours"]');
const lr = document.querySelector('input[name="labour_rate"]');
const lc = document.querySelector('input[name="labour_cost"]');
function autoLabour() {
    const h = parseFloat((lh?.value||'').replace(/,/g,'')) || 0;
    const r = parseFloat((lr?.value||'').replace(/,/g,'')) || 0;
    if (h > 0 && r > 0 && lc && !lc.value.trim()) {
        lc.value = (h * r).toFixed(2);
        lc.dispatchEvent(new Event('input', { bubbles: true }));
    }
}
lh?.addEventListener('blur', autoLabour); lr?.addEventListener('blur', autoLabour);
</script>
<?php $this->end(); ?>
