<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>
<div class="page-header"><div><h1><i class="bi bi-card-checklist me-2"></i>Create Job Card</h1></div></div>
<?= form_open(base_url('job-cards/store')) ?>
<div class="row g-3">
  <div class="col-lg-8">
    <div class="fm-form-section">
      <h6><i class="bi bi-link"></i>Link to Work Order</h6>
      <div class="mb-3">
        <label class="form-label">Work Order <span class="text-danger">*</span></label>
        <select name="wo_id" class="form-select" required onchange="fillWoDetails(this)">
          <option value="">— Select Work Order —</option>
          <?php foreach($workOrders as $wo): ?>
          <option value="<?= $wo['id'] ?>" <?= ($preselectedWo && $preselectedWo['id']==$wo['id'])?'selected':'' ?> data-title="<?= esc($wo['title']) ?>" data-facility="<?= esc($wo['facility_name']??'') ?>"><?= esc($wo['wo_number']) ?> — <?= esc(substr($wo['title'],0,50)) ?></option>
          <?php endforeach; ?>
        </select>
      </div>
      <?php if($preselectedWo): ?>
      <div class="alert alert-info small py-2"><i class="bi bi-info-circle me-1"></i>Linked to: <strong><?= esc($preselectedWo['wo_number']) ?></strong> — <?= esc($preselectedWo['title']) ?> (<?= esc($preselectedWo['facility_name']??'') ?>)</div>
      <?php endif; ?>
    </div>
    <div class="fm-form-section">
      <h6><i class="bi bi-card-text"></i>Job Description</h6>
      <div class="mb-3">
        <label class="form-label">Work Description <span class="text-danger">*</span></label>
        <textarea name="description" class="form-control" rows="5" required placeholder="Describe the work to be performed in detail..."><?= old('description') ?></textarea>
      </div>
    </div>
  </div>
  <div class="col-lg-4">
    <div class="fm-form-section">
      <h6><i class="bi bi-person-gear"></i>Assignment</h6>
      <div class="mb-3">
        <label class="form-label">Assigned Technician <span class="text-danger">*</span></label>
        <select name="assigned_to" class="form-select" required>
          <option value="">— Select —</option>
          <?php foreach($technicians as $t): ?>
          <option value="<?= $t['id'] ?>"><?= esc($t['name']) ?></option>
          <?php endforeach; ?>
        </select>
      </div>
    </div>
    <div class="fm-form-section">
      <h6><i class="bi bi-clock"></i>Labor</h6>
      <div class="mb-0">
        <label class="form-label">Estimated Hours</label>
        <input type="number" name="labor_hours" class="form-control" step="0.5" min="0" value="0" placeholder="0">
      </div>
    </div>
  </div>
</div>
<div class="d-flex gap-2 mt-2">
  <button type="submit" class="btn btn-fm-primary"><i class="bi bi-check-lg me-2"></i>Create Job Card</button>
  <a href="<?= base_url('job-cards') ?>" class="btn btn-fm-outline">Cancel</a>
</div>
<?= form_close() ?>
<script>
function fillWoDetails(sel){
  const opt = sel.options[sel.selectedIndex];
}
</script>
<?= $this->endSection() ?>
