<?php
$active = 'home';
$pageTitle = onb_full_name($hire);
require __DIR__ . '/../layout/workspace_header.php';
?>
<div class="topbar">
  <div><h1><?= htmlspecialchars(onb_full_name($hire)) ?></h1>
    <div class="crumbs"><a href="/onboarding" style="color:var(--blue-600);">Employee Onboarding</a> → <?= htmlspecialchars(onb_full_name($hire)) ?></div>
  </div>
</div>

<?php if (($_GET['err'] ?? '') === 'no_email'): ?><div class="alert alert-error">This hire has no email address on file — add one before sending.</div><?php endif; ?>
<?php if (($_GET['err'] ?? '') === 'mail_failed'): ?><div class="alert alert-error">Email failed to send — check mail server configuration.</div><?php endif; ?>

<div class="card">
  <p><?= htmlspecialchars($hire['position'] ?? '') ?> · <?= htmlspecialchars($hire['site'] ?? '') ?> · <?= htmlspecialchars($hire['email'] ?? '') ?></p>
  <p>Status: <span class="badge badge-manager"><?= htmlspecialchars(onb_status_label($hire['status'])) ?></span></p>

  <?php if ($hire['status'] === 'New'): ?>
    <form method="POST" action="/onboarding/send-phase" style="display:inline;">
      <input type="hidden" name="id" value="<?= (int)$hire['id'] ?>"><input type="hidden" name="phase" value="A">
      <button type="submit" class="btn-primary">Send Phase A →</button>
    </form>
  <?php elseif ($hire['status'] === 'A_sent'): ?>
    <form method="POST" action="/onboarding/send-phase" style="display:inline;">
      <input type="hidden" name="id" value="<?= (int)$hire['id'] ?>"><input type="hidden" name="phase" value="A">
      <button type="submit" class="btn-secondary">Resend Phase A</button>
    </form>
    <span style="color:var(--text-muted);">awaiting applicant…</span>
  <?php elseif ($hire['status'] === 'A_done'): ?>
    <a href="/onboarding/i9?id=<?= (int)$hire['id'] ?>" class="btn-primary" style="text-decoration:none;">Complete I-9 §2</a>
  <?php elseif ($hire['status'] === 'I9_done'): ?>
    <form method="POST" action="/onboarding/mark-training" style="display:inline;">
      <input type="hidden" name="id" value="<?= (int)$hire['id'] ?>">
      <button type="submit" class="btn-secondary">Mark training complete</button>
    </form>
  <?php elseif ($hire['status'] === 'Training'): ?>
    <form method="POST" action="/onboarding/send-phase" style="display:inline;">
      <input type="hidden" name="id" value="<?= (int)$hire['id'] ?>"><input type="hidden" name="phase" value="B">
      <button type="submit" class="btn-primary">Send Phase B →</button>
    </form>
  <?php elseif ($hire['status'] === 'B_sent'): ?>
    <form method="POST" action="/onboarding/send-phase" style="display:inline;">
      <input type="hidden" name="id" value="<?= (int)$hire['id'] ?>"><input type="hidden" name="phase" value="B">
      <button type="submit" class="btn-secondary">Resend Phase B</button>
    </form>
    <span style="color:var(--text-muted);">awaiting applicant…</span>
  <?php elseif ($hire['status'] === 'Final'): ?>
    <span style="color:#16a34a;font-weight:700;">✓ Onboarding complete</span>
  <?php endif; ?>
</div>

<div class="card">
  <h3>Government Forms</h3>
  <?php if (empty($forms)): ?>
    <p style="color:var(--text-muted);">Not generated yet — available once Phase B is submitted.</p>
  <?php else: ?>
    <table><thead><tr><th>Form</th><th></th></tr></thead><tbody>
      <?php foreach ($forms as $f): ?>
        <tr><td><?= htmlspecialchars($f['title']) ?></td>
            <td style="text-align:right;"><a href="/onboarding/download?type=gov&id=<?= (int)$f['id'] ?>" style="color:var(--blue-600);">Download</a></td></tr>
      <?php endforeach; ?>
    </tbody></table>
  <?php endif; ?>
</div>

<div class="card">
  <h3>All Documents</h3>
  <table><thead><tr><th>Document</th><th></th></tr></thead><tbody>
    <?php foreach ($documents as $d): ?>
      <tr><td><?= htmlspecialchars($d['title']) ?> <span style="color:var(--text-muted);">(<?= htmlspecialchars($d['category']) ?>)</span></td>
          <td style="text-align:right;"><a href="/onboarding/download?type=doc&id=<?= (int)$d['id'] ?>" style="color:var(--blue-600);">Download</a></td></tr>
    <?php endforeach; ?>
    <?php if (empty($documents)): ?><tr><td colspan="2" style="color:var(--text-muted);text-align:center;padding:16px;">No documents yet.</td></tr><?php endif; ?>
  </tbody></table>
</div>
<?php require __DIR__ . '/../layout/admin_footer.php'; ?>
