<?php include __DIR__ . '/../includes/header.php'; ?>
<div class="d-flex justify-content-between align-items-center mb-3 flex-wrap gap-2">
  <div><h4 class="mb-0">Reclutamiento</h4><div class="text-muted small">Control de vacantes, candidatos y postulaciones.</div></div>
  <?php if (can_manage_master_data()): ?><div class="d-flex gap-2"><a class="btn btn-outline-primary" href="<?= e(base_url('recruitment/candidate_create.php')) ?>">Nuevo candidato</a><a class="btn btn-primary" href="<?= e(base_url('recruitment/vacancy_create.php')) ?>">Nueva vacante</a></div><?php endif; ?>
</div>
<div class="row g-3 mb-3">
  <div class="col-lg-4"><div class="card card-stat"><div class="card-body"><div class="metric-label">Vacantes abiertas</div><div class="metric-value"><?= (int)db()->query("SELECT COUNT(*) c FROM recruitment_vacancies WHERE status='Abierta'")->fetch()['c'] ?></div></div></div></div>
  <div class="col-lg-4"><div class="card card-stat"><div class="card-body"><div class="metric-label">Candidatos</div><div class="metric-value"><?= count_table('recruitment_candidates') ?></div></div></div></div>
  <div class="col-lg-4"><div class="card card-stat"><div class="card-body"><div class="metric-label">Postulaciones activas</div><div class="metric-value"><?= (int)db()->query("SELECT COUNT(*) c FROM recruitment_applications WHERE stage IN ('Postulado','Entrevista')")->fetch()['c'] ?></div></div></div></div>
</div>
<div class="table-wrap mb-3">
  <div class="d-flex justify-content-between align-items-center mb-3"><h5 class="mb-0">Vacantes</h5><a class="btn btn-sm btn-outline-primary" href="<?= e(base_url('reports/export_recruitment_xlsx.php')) ?>">Exportar XLSX</a></div>
  <div class="table-responsive"><table class="table table-bordered table-hover align-middle"><thead><tr><th>Título</th><th>Departamento</th><th>Ubicación</th><th>Estado</th><th width="220">Acciones</th></tr></thead><tbody><?php foreach (db()->query("SELECT * FROM recruitment_vacancies ORDER BY id DESC") as $row): ?><tr><td><?= e($row['title']) ?></td><td><?= e($row['department']) ?></td><td><?= e($row['location']) ?></td><td><span class="badge text-bg-<?= e(status_badge_class($row['status'])) ?>"><?= e($row['status']) ?></span></td><td><a class="btn btn-sm btn-outline-success" href="<?= e(base_url('recruitment/application_create.php?vacancy_id=' . $row['id'])) ?>">Postular</a><?php if (can_manage_master_data()): ?> <a class="btn btn-sm btn-warning" href="<?= e(base_url('recruitment/vacancy_edit.php?id=' . $row['id'])) ?>">Editar</a> <a class="btn btn-sm btn-danger" href="<?= e(base_url('recruitment/vacancy_delete.php?id=' . $row['id'])) ?>" onclick="return confirm('¿Eliminar vacante?')">Eliminar</a><?php endif; ?></td></tr><?php endforeach; ?></tbody></table></div>
</div>
<div class="table-wrap mb-3">
  <div class="d-flex justify-content-between align-items-center mb-3"><h5 class="mb-0">Candidatos</h5><a class="btn btn-sm btn-outline-primary" href="<?= e(base_url('recruitment/candidate_create.php')) ?>">Nuevo candidato</a></div>
  <div class="table-responsive"><table class="table table-bordered table-hover align-middle"><thead><tr><th>Nombre</th><th>Cédula</th><th>Teléfono</th><th>Correo</th><th width="160">Acciones</th></tr></thead><tbody><?php foreach (db()->query("SELECT * FROM recruitment_candidates ORDER BY id DESC") as $row): ?><tr><td><?= e($row['full_name']) ?></td><td><?= e($row['document_number']) ?></td><td><?= e($row['phone']) ?></td><td><?= e($row['email']) ?></td><td><?php if (can_manage_master_data()): ?><a class="btn btn-sm btn-warning" href="<?= e(base_url('recruitment/candidate_edit.php?id=' . $row['id'])) ?>">Editar</a> <a class="btn btn-sm btn-danger" href="<?= e(base_url('recruitment/candidate_delete.php?id=' . $row['id'])) ?>" onclick="return confirm('¿Eliminar candidato?')">Eliminar</a><?php endif; ?></td></tr><?php endforeach; ?></tbody></table></div>
</div>
<div class="table-wrap">
  <h5 class="mb-3">Postulaciones</h5>
  <div class="table-responsive"><table class="table table-bordered table-hover align-middle"><thead><tr><th>Candidato</th><th>Vacante</th><th>Etapa</th><th>Puntaje</th><th>Fecha</th><th width="150">Acciones</th></tr></thead><tbody><?php foreach (db()->query("SELECT a.*, c.full_name candidate, v.title vacancy FROM recruitment_applications a JOIN recruitment_candidates c ON c.id=a.candidate_id JOIN recruitment_vacancies v ON v.id=a.vacancy_id ORDER BY a.id DESC") as $row): ?><tr><td><?= e($row['candidate']) ?></td><td><?= e($row['vacancy']) ?></td><td><?= e($row['stage']) ?></td><td><?= e($row['score']) ?></td><td><?= e($row['applied_at']) ?></td><td><?php if (can_manage_master_data()): ?><a class="btn btn-sm btn-warning" href="<?= e(base_url('recruitment/application_edit.php?id=' . $row['id'])) ?>">Editar</a> <a class="btn btn-sm btn-danger" href="<?= e(base_url('recruitment/application_delete.php?id=' . $row['id'])) ?>" onclick="return confirm('¿Eliminar postulación?')">Eliminar</a><?php endif; ?></td></tr><?php endforeach; ?></tbody></table></div>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>