<?php $this->start('body'); ?>

<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 Assignment</h5>
  <a href="<?= PROOT; ?>DriverAssignments" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i>Back</a>
</div>

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

<div class="alert alert-info small">
  <i class="fas fa-info-circle me-1"></i>
  If the selected driver already has an active assignment on another truck, it will be
  <strong>auto-closed</strong> as of the start date below (reason: <em>Reassignment</em>).
  A driver can only be on one truck at a time.
</div>

<form method="POST" action="<?= PROOT; ?>DriverAssignments/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">Driver <span class="text-danger">*</span></label>
        <select name="driver_id" class="form-select" required>
          <option value="">— select driver —</option>
          <?php foreach ($this->data['drivers'] as $d): ?>
            <option value="<?= (int)$d['c0d']; ?>" <?= $this->data['presel_driver']==$d['c0d']?'selected':''; ?>>
              <?= htmlspecialchars($d['full_name']); ?>
              <?php if ($d['employee_no']): ?> · <?= htmlspecialchars($d['employee_no']); ?><?php endif; ?>
              <?php if ($d['status']!=='available'): ?> · <?= htmlspecialchars(str_replace('_',' ',$d['status'])); ?><?php endif; ?>
            </option>
          <?php endforeach; ?>
        </select>
      </div>
      <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']; ?>" <?= $this->data['presel_truck']==$t['c0d']?'selected':''; ?>>
              <?= htmlspecialchars($t['reg_no']); ?>
              <?php if ($t['make'] || $t['model']): ?> · <?= 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-4">
        <label class="form-label">Effective from <span class="text-danger">*</span></label>
        <input type="date" name="assigned_from" class="form-control" value="<?= date('Y-m-d'); ?>" required>
      </div>
      <div class="col-md-4">
        <label class="form-label">Role</label>
        <select name="role_type" class="form-select">
          <?php foreach (['primary'=>'Primary','relief'=>'Relief / cover','trip'=>'Single trip'] as $k=>$v): ?>
            <option value="<?= $k; ?>"><?= $v; ?></option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="col-md-4">
        <label class="form-label">Reason</label>
        <input type="text" name="reason" class="form-control" placeholder="e.g. New hire · covering leave · route change">
      </div>
      <div class="col-12">
        <label class="form-label">Notes</label>
        <textarea name="notes" rows="2" class="form-control"></textarea>
      </div>
    </div>
  </div>
  <div class="card-footer bg-white d-flex justify-content-end gap-2 py-3">
    <a href="<?= PROOT; ?>DriverAssignments" class="btn btn-outline-secondary">Cancel</a>
    <button class="btn btn-primary"><i class="fas fa-save me-1"></i>Create assignment</button>
  </div>
</form>

<?php $this->end(); ?>
