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

<div class="d-flex align-items-center justify-content-between mb-3">
  <h5 class="mb-0 fw-semibold"><i class="fas fa-plus me-2 text-muted"></i>New Work Order</h5>
  <a href="<?= PROOT; ?>WorkOrders" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

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

<div class="alert alert-info small">
  <i class="fas fa-info-circle me-1"></i>
  Save opens the WO. Add parts and record labour cost on the next screen, then <strong>Start → Complete → Close</strong>. Close posts the maintenance JE and releases the truck.
</div>

<form method="POST" action="<?= PROOT; ?>WorkOrders/save" 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 <span class="text-danger">*</span></label>
        <select name="truck_id" class="form-select" required>
          <option value="">— select truck —</option>
          <?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; ?>
              <?php if ($t['status']!=='active'): ?> · <?= htmlspecialchars($t['status']); ?><?php endif; ?>
            </option>
          <?php endforeach; ?>
        </select></div>
      <div class="col-md-6"><label class="form-label">Vendor / Workshop (optional)</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']); ?>
              <?php if ($v['vendor_category']): ?> · <?= htmlspecialchars($v['vendor_category']); ?><?php endif; ?>
            </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 service','breakdown'=>'Breakdown','callout'=>'Roadside callout','inspection'=>'Inspection','recall'=>'Manufacturer recall'] as $k=>$vv): ?>
            <option value="<?= $k; ?>" <?= ($f['kind'] ?? 'planned')===$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'=>'Low','normal'=>'Normal','high'=>'High','urgent'=>'Urgent'] as $k=>$vv): ?>
            <option value="<?= $k; ?>" <?= ($f['priority'] ?? 'normal')===$k?'selected':''; ?>><?= $vv; ?></option>
          <?php endforeach; ?>
        </select></div>
      <div class="col-md-3"><label class="form-label">Opened on <span class="text-danger">*</span></label>
        <input type="date" name="opened_on" class="form-control" required value="<?= htmlspecialchars($f['opened_on'] ?? date('Y-m-d')); ?>"></div>
      <div class="col-md-3"><label class="form-label">Odometer at open (km)</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" placeholder="What's wrong? What did the driver report? What did the workshop find on inspection?"><?= htmlspecialchars($f['fault_description'] ?? ''); ?></textarea></div>
    </div>
  </div>
  <div class="card-footer bg-white d-flex justify-content-end gap-2 py-3">
    <a href="<?= PROOT; ?>WorkOrders" class="btn btn-outline-secondary">Cancel</a>
    <button class="btn btn-primary"><i class="fas fa-save me-1"></i>Open work order</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(); ?>
