<?php
if (!function_exists('fmtDate')) {
    function fmtDate($d, $t = false) {
        if (!$d || $d === '0000-00-00' || $d === '0000-00-00 00:00:00') return '—';
        try {
            $dt = new DateTime($d);
            return $t ? $dt->format('m/d/Y g:i A') : $dt->format('m/d/Y');
        } catch (Exception $e) {
            return htmlspecialchars((string)$d);
        }
    }
}
$active = 'kpi';
$pageTitle = 'KPI Tracker';
require __DIR__ . '/../layout/admin_header.php';
?>
<div class="topbar">
  <div>
    <h1>KPI Tracker</h1>
    <div class="crumbs">Workspace → Admin → KPI Tracker</div>
  </div>
  <div class="user-chip"><span class="dot"></span> <?= htmlspecialchars($admin['name']) ?></div>
</div>

<div class="card">
  <form method="GET" action="/admin/kpi" style="display:flex;gap:12px;align-items:end;flex-wrap:wrap;margin-bottom:6px;">
    <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>
    <a href="/admin/kpi/setup?dept=<?= $selectedDeptId ?>" class="btn-secondary" style="text-decoration:none;">⚙️ Configure KPI Fields</a>
  </form>
</div>

<div class="card">
  <h3>Submissions for this week</h3>
  <?php if (empty($metrics)): ?>
    <p style="color:var(--text-muted);font-size:13px;">No KPI fields configured for this department yet. Use "Configure KPI Fields" above.</p>
  <?php elseif (empty($entries)): ?>
    <p style="color:var(--text-muted);font-size:13px;">No submissions yet for this week.</p>
  <?php else: ?>
    <table>
      <thead>
        <tr>
          <th>Submitted By</th>
          <?php foreach ($metrics as $m): ?>
            <th><?= htmlspecialchars($m['metric_label']) ?><?= $m['unit'] ? ' (' . htmlspecialchars($m['unit']) . ')' : '' ?></th>
          <?php endforeach; ?>
          <th>Submitted At</th>
        </tr>
      </thead>
      <tbody>
        <?php foreach ($entries as $e): ?>
          <tr>
            <td style="font-weight:700;"><?= htmlspecialchars($e['user_name']) ?></td>
            <?php foreach ($metrics as $m): ?>
              <td><?= htmlspecialchars($e['values'][$m['metric_key']] ?? '—') ?></td>
            <?php endforeach; ?>
            <td style="color:var(--text-muted);font-size:12px;"><?= fmtDate($e['submitted_at']) ?></td>
          </tr>
        <?php endforeach; ?>
      </tbody>
    </table>
  <?php endif; ?>
</div>

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