<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>
<div class="page-header">
  <div><h1><i class="bi bi-card-checklist me-2"></i>Job Cards</h1></div>
  <a href="<?= base_url('job-cards/create') ?>" class="btn btn-fm-primary btn-sm"><i class="bi bi-plus me-1"></i>New Job Card</a>
</div>
<div class="row g-3 mb-3">
  <?php foreach(['draft'=>['kpi-secondary','bi-file-earmark'],'in_progress'=>['kpi-blue','bi-gear'],'completed'=>['kpi-teal','bi-check-circle'],'approved'=>['kpi-green','bi-shield-check']] as $s=>[$c,$i]): ?>
  <div class="col-6 col-md-3"><div class="kpi-card <?= $c ?>"><div class="d-flex align-items-center gap-3"><div class="kpi-icon"><i class="bi <?= $i ?>"></i></div><div><div class="kpi-label"><?= ucfirst(str_replace('_',' ',$s)) ?></div><div class="kpi-value"><?= $kpi[$s]??0 ?></div></div></div></div></div>
  <?php endforeach; ?>
</div>
<div class="fm-card">
  <div class="fm-card-body p-0">
    <?php if(empty($jobCards)): ?>
    <p class="text-center text-muted py-5">No job cards yet. <a href="<?= base_url('job-cards/create') ?>">Create one</a>.</p>
    <?php else: ?>
    <table class="fm-table">
      <thead><tr><th>JC #</th><th>Work Order</th><th>Facility</th><th>Assigned To</th><th>Hours</th><th>Status</th><th>Created</th><th></th></tr></thead>
      <tbody>
      <?php foreach($jobCards as $jc): ?>
      <tr>
        <td><a href="<?= base_url('job-cards/view/'.$jc['id']) ?>" class="fw-bold text-primary"><?= esc($jc['jc_number']) ?></a></td>
        <td class="small"><a href="<?= base_url('workorders/view/'.$jc['wo_id']) ?>" class="text-muted"><?= esc($jc['wo_number']??'—') ?></a><br><span class="x-small text-muted"><?= esc(substr($jc['wo_title']??'',0,30)) ?></span></td>
        <td class="small"><?= esc($jc['facility_name']??'—') ?></td>
        <td class="small"><?= esc($jc['assigned_to_name']??'—') ?></td>
        <td class="small text-center"><?= number_format($jc['labor_hours'],1) ?>h</td>
        <td><span class="fm-badge badge-status-<?= esc($jc['status']) ?>"><?= ucfirst($jc['status']) ?></span></td>
        <td class="small text-muted"><?= date('d M Y',strtotime($jc['created_at'])) ?></td>
        <td>
          <a href="<?= base_url('job-cards/view/'.$jc['id']) ?>" class="btn-action bg-primary text-white me-1"><i class="bi bi-eye"></i></a>
          <a href="<?= base_url('job-cards/print/'.$jc['id']) ?>" class="btn-action bg-secondary text-white" target="_blank"><i class="bi bi-printer"></i></a>
        </td>
      </tr>
      <?php endforeach; ?>
      </tbody>
    </table>
    <?php endif; ?>
  </div>
</div>
<?= $this->endSection() ?>
