<?php
$active = 'kpi';
$pageTitle = 'KPI Tracker';
require __DIR__ . '/../layout/workspace_header.php';
?>
<div class="topbar">
  <div>
    <h1>KPI Tracker</h1>
    <div class="crumbs">Workspace → KPI Tracker</div>
  </div>
  <div class="user-chip"><span class="dot"></span> <?= htmlspecialchars($user['name']) ?></div>
</div>

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

<?php if (empty($departments)): ?>
  <div class="card"><p style="color:var(--text-muted);font-size:13px;">You aren't assigned to any department yet, so there's no KPI form to fill out. Contact your administrator.</p></div>
<?php else: ?>

<div class="card">
  <form method="GET" action="/kpi" style="display:flex;gap:12px;align-items:end;flex-wrap:wrap;">
    <div class="field" style="margin-bottom:0;min-width:220px;">
      <label>Department</label>
      <select name="dept" onchange="this.form.submit()" style="width:100%;padding:10px;border:1px solid var(--border);border-radius:10px;">
        <?php foreach ($departments as $d): ?>
          <option value="<?= $d['id'] ?>" <?= $selectedDeptId === (int)$d['id'] ? 'selected' : '' ?>><?= htmlspecialchars($d['name']) ?></option>
        <?php endforeach; ?>
      </select>
    </div>
    <div class="field" style="margin-bottom:0;">
      <label>Week Starting</label>
      <input type="date" name="week" value="<?= htmlspecialchars($periodStart) ?>" onchange="this.form.submit()" style="padding:10px;border:1px solid var(--border);border-radius:10px;">
    </div>
  </form>
</div>

<?php if (empty($metrics)): ?>
  <div class="card"><p style="color:var(--text-muted);font-size:13px;">No KPI fields have been set up for this department yet.</p></div>
<?php else: ?>
  <div class="card" style="max-width:600px;">
    <h3><?= $myEntry ? 'Update This Week\'s Entry' : 'Submit This Week\'s KPIs' ?></h3>
    <form method="POST" action="/kpi/submit">
      <input type="hidden" name="department_id" value="<?= $selectedDeptId ?>">
      <input type="hidden" name="period_start" value="<?= htmlspecialchars($periodStart) ?>">
      <?php foreach ($metrics as $m): ?>
        <div class="field">
          <label><?= htmlspecialchars($m['metric_label']) ?><?= $m['unit'] ? ' (' . htmlspecialchars($m['unit']) . ')' : '' ?></label>
          <?php if ($m['input_type'] === 'text'): ?>
            <input type="text" name="metric[<?= htmlspecialchars($m['metric_key']) ?>]" value="<?= htmlspecialchars($myEntry['values'][$m['metric_key']] ?? '') ?>">
          <?php else: ?>
            <input type="number" step="0.01" name="metric[<?= htmlspecialchars($m['metric_key']) ?>]" value="<?= htmlspecialchars($myEntry['values'][$m['metric_key']] ?? '') ?>">
          <?php endif; ?>
        </div>
      <?php endforeach; ?>
      <button type="submit" class="btn-primary"><?= $myEntry ? 'Update Entry' : 'Submit Entry' ?></button>
    </form>
  </div>

  <div class="card">
    <h3>Your Recent Submissions</h3>
    <?php if (empty($history)): ?>
      <p style="color:var(--text-muted);font-size:13px;">No submissions yet.</p>
    <?php else: ?>
      <table>
        <thead>
          <tr><th>Week</th><?php foreach ($metrics as $m): ?><th><?= htmlspecialchars($m['metric_label']) ?></th><?php endforeach; ?></tr>
        </thead>
        <tbody>
          <?php foreach ($history as $h): ?>
            <tr>
              <td><?= htmlspecialchars($h['period_start']) ?></td>
              <?php foreach ($metrics as $m): ?>
                <td><?= htmlspecialchars($h['values'][$m['metric_key']] ?? '—') ?></td>
              <?php endforeach; ?>
            </tr>
          <?php endforeach; ?>
        </tbody>
      </table>
    <?php endif; ?>
  </div>
<?php endif; ?>
<?php endif; ?>

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