<?php
$admin = $admin ?? \App\Core\Auth::currentUser();
$active = 'departments';
$pageTitle = 'Departments';
require __DIR__ . '/../layout/admin_header.php';
?>
<div class="topbar">
  <div>
    <h1>Departments</h1>
    <div class="crumbs">Workspace → Admin → Departments</div>
  </div>
  <div class="user-chip"><span class="dot"></span> <?= htmlspecialchars($admin['name']) ?></div>
</div>

<?php if ($flash): ?>
  <div class="alert alert-<?= $flash['type'] === 'error' ? 'error' : 'success' ?>"><?= htmlspecialchars($flash['msg']) ?></div>
<?php endif; ?>

<div class="card">
  <div class="toolbar">
    <div></div>
    <button class="btn-primary" onclick="openModal('addDeptModal')">+ Add Department</button>
  </div>
  <table>
    <thead><tr><th>Name</th><th>Code</th><th>Projects</th><th>Status</th><th>Action</th></tr></thead>
    <tbody>
      <?php foreach ($departments as $d): ?>
        <tr>
          <td style="font-weight:700;"><?= htmlspecialchars($d['name']) ?></td>
          <td><?= htmlspecialchars($d['code']) ?></td>
          <td><?= (int)$d['project_count'] ?> Project<?= (int)$d['project_count'] === 1 ? '' : 's' ?></td>
          <td><span class="badge <?= (int)$d['is_active'] === 1 ? 'badge-active' : 'badge-disabled' ?>"><?= (int)$d['is_active'] === 1 ? 'Active' : 'Disabled' ?></span></td>
          <td>
            <form method="POST" action="/admin/departments/toggle" style="display:inline;">
              <input type="hidden" name="id" value="<?= (int)$d['id'] ?>">
              <input type="hidden" name="active" value="<?= (int)$d['is_active'] === 1 ? '0' : '1' ?>">
              <?php if ((int)$d['is_active'] === 1): ?>
                <button type="submit" class="btn-danger">Disable</button>
              <?php else: ?>
                <button type="submit" class="btn-success">Enable</button>
              <?php endif; ?>
            </form>
          </td>
        </tr>
      <?php endforeach; ?>
    </tbody>
  </table>
</div>

<div class="modal-overlay" id="addDeptModal">
  <div class="modal">
    <h3>Add Department</h3>
    <form method="POST" action="/admin/departments/create">
      <div class="field"><label>Department Name</label><input type="text" name="name" required placeholder="e.g. Recruiting"></div>
      <div class="field"><label>Code (unique, lowercase, no spaces)</label><input type="text" name="code" required placeholder="e.g. recruiting"></div>
      <div class="modal-actions">
        <button type="button" class="btn-secondary" onclick="closeModal('addDeptModal')">Cancel</button>
        <button type="submit" class="btn-primary">Create Department</button>
      </div>
    </form>
  </div>
</div>

<script>
function openModal(id){ document.getElementById(id).classList.add('open'); }
function closeModal(id){ document.getElementById(id).classList.remove('open'); }
document.querySelectorAll('.modal-overlay').forEach(o => o.addEventListener('click', e => { if (e.target === o) o.classList.remove('open'); }));
</script>

<?php require __DIR__ . '/../layout/admin_footer.php'; ?>
