<?php
$admin = $admin ?? \App\Core\Auth::currentUser();
$active = 'projects';
$pageTitle = 'Projects';
require __DIR__ . '/../layout/admin_header.php';
?>
<div class="topbar">
  <div>
    <h1>Projects</h1>
    <div class="crumbs">Workspace → Admin → Projects</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="openProjectModal()">+ New Project</button>
  </div>
  <table>
    <thead><tr><th>Project</th><th>Department</th><th>Code</th><th>KPI Form</th><th>Status</th><th>Action</th></tr></thead>
    <tbody>
      <?php foreach ($projects as $p): ?>
        <tr>
          <td style="font-weight:700;"><?= htmlspecialchars($p['name']) ?></td>
          <td><?= htmlspecialchars($p['department_name']) ?></td>
          <td><?= htmlspecialchars($p['code']) ?></td>
          <td>
            <?php
            $kftLabels = ['none'=>'—','scheduling'=>'Scheduling','ops_manager'=>'Ops Manager','accounts'=>'Accounts'];
            $kft = $p['kpi_form_type'] ?? 'none';
            echo $kft !== 'none' ? '<span style="background:#eff6ff;color:#1d4ed8;border-radius:8px;padding:2px 10px;font-size:12px;font-weight:600;">'.htmlspecialchars($kftLabels[$kft]??$kft).'</span>' : '<span style="color:var(--text-muted);font-size:13px;">—</span>';
            ?>
          </td>
          <td>
            <span class="badge <?= $p['status'] === 'active' ? 'badge-active' : ($p['status'] === 'archived' ? 'badge-disabled' : 'badge-manager') ?>">
              <?= htmlspecialchars($p['status']) ?>
            </span>
          </td>
          <td>
            <?php if (strtolower($p['department_name']) === 'scheduling'): ?>
              <a href="/projects/kpi/company" class="btn-secondary" style="text-decoration:none;padding:6px 12px;font-size:12px;">📊 KPI Tracker</a>
            <?php endif; ?>
            <button class="btn-secondary" style="padding:6px 12px;font-size:12px;"
              onclick='openProjectModal(<?= json_encode($p) ?>)'>Edit</button>
            <form method="POST" action="/admin/projects/archive" style="display:inline;">
              <input type="hidden" name="id" value="<?= (int)$p['id'] ?>">
              <input type="hidden" name="status" value="<?= $p['status'] === 'archived' ? 'active' : 'archived' ?>">
              <?php if ($p['status'] === 'archived'): ?>
                <button type="submit" class="btn-success">Restore</button>
              <?php else: ?>
                <button type="submit" class="btn-danger">Archive</button>
              <?php endif; ?>
            </form>
          </td>
        </tr>
      <?php endforeach; ?>
      <?php if (empty($projects)): ?>
        <tr><td colspan="5" style="color:var(--text-muted);text-align:center;padding:24px;">No projects yet — create the first one.</td></tr>
      <?php endif; ?>
    </tbody>
  </table>
</div>

<div class="modal-overlay" id="projectModal">
  <div class="modal">
    <h3 id="projectModalTitle">New Project</h3>
    <form method="POST" action="/admin/projects/save">
      <input type="hidden" name="id" id="p-id" value="">
      <div class="field">
        <label>Department</label>
        <select name="department_id" id="p-dept" required style="width:100%;padding:10px;border:1px solid var(--border);border-radius:10px;">
          <?php foreach ($departments as $d): ?>
            <option value="<?= $d['id'] ?>"><?= htmlspecialchars($d['name']) ?></option>
          <?php endforeach; ?>
        </select>
      </div>
      <div class="field"><label>Project Name</label><input type="text" name="name" id="p-name" required placeholder="e.g. Hiring & Termination"></div>
      <div class="field"><label>Code (unique, lowercase)</label><input type="text" name="code" id="p-code" required placeholder="e.g. hiring_termination"></div>
      <div class="field">
        <label>KPI Form Type <small style="font-weight:500;color:var(--text-muted);">(which KPI form users see on this project)</small></label>
        <select name="kpi_form_type" id="p-kpi-form-type" style="width:100%;padding:10px;border:1px solid var(--border);border-radius:10px;">
          <option value="none">None — no KPI form</option>
          <option value="scheduling">Scheduling KPI (Karan / Harpreet style)</option>
          <option value="ops_manager">Operations Manager KPI (Cesar style)</option>
          <option value="accounts">Accounts KPI (Jatinder style)</option>
        </select>
      </div>
      <div class="field">
        <label>Status</label>
        <select name="status" id="p-status" style="width:100%;padding:10px;border:1px solid var(--border);border-radius:10px;">
          <option value="planned">Planned</option>
          <option value="active" selected>Active</option>
          <option value="on_hold">On Hold</option>
          <option value="completed">Completed</option>
          <option value="archived">Archived</option>
        </select>
      </div>
      <div class="modal-actions">
        <button type="button" class="btn-secondary" onclick="closeModal('projectModal')">Cancel</button>
        <button type="submit" class="btn-primary">Save Project</button>
      </div>
    </form>
  </div>
</div>

<script>
function openModal(id){ document.getElementById(id).classList.add('open'); }
function closeModal(id){ document.getElementById(id).classList.remove('open'); }

function openProjectModal(p){
  document.getElementById('projectModalTitle').textContent = p ? 'Edit Project' : 'New Project';
  document.getElementById('p-id').value = p ? p.id : '';
  document.getElementById('p-name').value = p ? p.name : '';
  document.getElementById('p-code').value = p ? p.code : '';
  document.getElementById('p-dept').value = p ? p.department_id : document.getElementById('p-dept').value;
  document.getElementById('p-status').value = p ? p.status : 'active';
  document.getElementById('p-kpi-form-type').value = p ? (p.kpi_form_type || 'none') : 'none';
  openModal('projectModal');
}
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'; ?>
