<?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    = 'home';
$pageTitle = 'KPI Record — ' . ($entry['submitter_name'] ?? '') . ' — ' . ($entry['week_ending'] ?? '');
$isModalMode = !empty($_GET['modal']);
if (!$isModalMode) {
    require __DIR__ . '/../layout/workspace_header.php';
} else { ?>
<!DOCTYPE html><html lang="en"><head>
<meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="stylesheet" href="/assets/css/app.css?v=<?= defined('ASSET_VERSION') ? ASSET_VERSION : time() ?>">
<style>body,.admin-shell{background:#fff!important;}.main-inner{padding:20px 24px 32px!important;}.sidebar{display:none!important;}.main{margin:0!important;}</style>
</head><body><div class="admin-shell"><main class="main"><div class="main-inner">
<?php } ?>
<style>
.vcard{background:#fff;border:1px solid var(--border);border-radius:14px;padding:22px 26px;margin-bottom:16px;}
.vcard h3{font-size:11px;font-weight:700;letter-spacing:.07em;text-transform:uppercase;color:var(--text-muted);margin:0 0 14px;}
.vfld{display:flex;flex-direction:column;gap:3px;}
.vfld label{font-size:11px;font-weight:600;color:var(--text-muted);text-transform:uppercase;letter-spacing:.05em;}
.vfld .val{font-size:14px;font-weight:600;color:var(--text);}
.vgrid{display:grid;grid-template-columns:repeat(auto-fill,minmax(160px,1fr));gap:14px;}
.vgrid3{display:grid;grid-template-columns:1fr 1fr 1fr;gap:14px;}
</style>

<?php
$sc = ['draft'=>'#6b7280','submitted'=>'#2563eb','reviewed'=>'#7c3aed','approved'=>'#059669','locked'=>'#059669','returned'=>'#dc2626'];
$statusColor = $sc[$entry['status']] ?? '#6b7280';
$statusLabel = ['draft'=>'Draft','submitted'=>'Submitted — Awaiting Review','reviewed'=>'Reviewed','approved'=>'Approved','locked'=>'Approved & Locked','returned'=>'Returned'][$entry['status']] ?? ucfirst($entry['status']);
$sites_visited = json_decode($entry['sites_visited'] ?? '[]', true) ?: [];
?>

<?php if (!$isModalMode): ?>
<div class="topbar">
  <div>
    <h1>Weekly KPI — <?= htmlspecialchars($entry['submitter_name']) ?> — <?= fmtDate($entry['week_ending']) ?></h1>
    <div class="crumbs"><a href="/workspace" style="color:var(--blue-600);">Workspace</a> → Projects → KPI Tracker → Record #<?= $entry['id'] ?></div>
  </div>
  <span style="display:inline-block;padding:4px 14px;border-radius:20px;font-size:13px;font-weight:700;background:<?= $statusColor ?>22;color:<?= $statusColor ?>;"><?= $statusLabel ?></span>
</div>
<?php else: ?>
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:18px;">
  <h2 style="margin:0;font-size:17px;font-weight:800;">KPI — <?= htmlspecialchars($entry['submitter_name']) ?> — <?= fmtDate($entry['week_ending']) ?></h2>
  <span style="padding:4px 14px;border-radius:20px;font-size:12px;font-weight:700;background:<?= $statusColor ?>22;color:<?= $statusColor ?>;"><?= $statusLabel ?></span>
</div>
<?php endif; ?>

<!-- Review outcome card -->
<?php if (in_array($entry['status'],['locked','reviewed','approved']) && ($entry['review_score'] || $entry['review_comment'])): ?>
<div style="background:linear-gradient(120deg,#ecfdf5,#f0fdf4);border:1.5px solid #6ee7b7;border-radius:14px;padding:18px 22px;margin-bottom:16px;">
  <div style="font-weight:700;color:#065f46;margin-bottom:8px;">KPI review completed
    <span style="font-size:12px;font-weight:500;color:#047857;margin-left:8px;">Reviewed by <?= htmlspecialchars($entry['reviewer_name'] ?? '—') ?> · <?= $entry['reviewed_at'] ? fmtDate($entry['reviewed_at']) : '—' ?></span>
  </div>
  <div style="display:flex;gap:28px;align-items:flex-start;">
    <?php if ($entry['review_score'] ?? 0): ?>
    <div>
      <div style="font-size:11px;font-weight:700;color:var(--text-muted);text-transform:uppercase;letter-spacing:.06em;margin-bottom:4px;">Score</div>
      <span style="background:#FAEEDA;color:#633806;border-radius:8px;padding:4px 14px;font-size:20px;font-weight:800;"><?= (int)$entry['review_score'] ?>/10</span>
    </div>
    <?php endif; ?>
    <?php if ($entry['review_comment']): ?>
    <div>
      <div style="font-size:11px;font-weight:700;color:var(--text-muted);text-transform:uppercase;letter-spacing:.06em;margin-bottom:4px;">Reviewer Comments</div>
      <div style="font-size:14px;"><?= htmlspecialchars($entry['review_comment']) ?></div>
    </div>
    <?php endif; ?>
  </div>
  <?php if ($isReviewer && $entry['status']==='locked'): ?>
  <!-- Revise Score -->
  <details style="margin-top:14px;">
    <summary style="font-size:12px;color:var(--blue-600);cursor:pointer;font-weight:600;">✎ Revise Rating</summary>
    <form method="POST" action="/projects/jatinder-kpi/review-action<?= $isModalMode?'?modal=1':'' ?>" style="margin-top:10px;display:flex;flex-wrap:wrap;gap:10px;align-items:flex-end;">
      <input type="hidden" name="id" value="<?= $entry['id'] ?>">
      <input type="hidden" name="action" value="revise_score">
      <div class="fld"><label style="font-size:11px;font-weight:600;color:var(--text-muted);">New Score (1–10)</label><input type="number" name="new_score" min="1" max="10" required style="width:90px;padding:8px;border:1px solid var(--border);border-radius:8px;font-size:14px;"></div>
      <div class="fld" style="flex:1;min-width:160px;"><label style="font-size:11px;font-weight:600;color:var(--text-muted);">New Comment</label><input type="text" name="new_comment" placeholder="Updated comment..." style="width:100%;padding:8px;border:1px solid var(--border);border-radius:8px;font-size:13px;"></div>
      <div class="fld" style="flex:2;min-width:200px;"><label style="font-size:11px;font-weight:600;color:var(--text-muted);">Reason for Revision *</label><input type="text" name="revise_reason" placeholder="Why are you revising?" required style="width:100%;padding:8px;border:1px solid var(--border);border-radius:8px;font-size:13px;"></div>
      <button type="submit" class="btn-primary" style="padding:8px 18px;font-size:13px;">Save Revision</button>
    </form>
  </details>
  <?php endif; ?>
</div>
<?php elseif ($entry['status']==='returned' && $entry['return_reason']): ?>
<div style="background:#fef2f2;border:1.5px solid #fca5a5;border-radius:14px;padding:18px 22px;margin-bottom:16px;">
  <div style="font-weight:700;color:#991b1b;margin-bottom:6px;">Returned for edits</div>
  <div style="font-size:14px;color:#7f1d1d;"><?= htmlspecialchars($entry['return_reason']) ?></div>
</div>
<?php endif; ?>

<?php
// ── Review discussion thread (Accounts KPI) ───────────────────────────────
// Same mechanism as the Scheduling KPI: the reviewer's comment stays on the
// KPI row itself and is rendered as the root of the chain; replies live in
// `kpi_discussion` under kpi_type='accounts'. Posting never alters the KPI
// record's status, score or lock state.
$__accRoot   = trim((string)($entry['review_comment'] ?? ''));
$__accMsgs   = \App\Models\KpiDiscussion::forEntry((int)$entry['id'], 'accounts');
$__accShow   = ($__accRoot !== '') || !empty($__accMsgs);
// Only the record owner may post; reviewers are view-only.
$__accCanPost = !empty($isSubmitter);
$__accWhen    = $entry['approved_at'] ?? ($entry['reviewed_at'] ?? ($entry['locked_at'] ?? null));
?>
<?php if ($__accShow): ?>
<div class="card" id="kpi-discussion" style="background:#fff;border:1px solid var(--border);border-radius:14px;padding:20px 22px;margin-bottom:16px;">
  <h3 style="margin:0 0 4px;font-size:16px;font-weight:800;">&#128172; Review Discussion</h3>
  <p style="font-size:12.5px;color:var(--text-muted);margin:0 0 18px;">
    The full conversation on this KPI, oldest first. Everything stays attached to this record.
  </p>

  <?php if ($__accRoot !== ''): ?>
    <div style="display:flex;gap:12px;margin-bottom:16px;">
      <div style="flex:0 0 4px;background:#3B6D11;border-radius:2px;"></div>
      <div style="flex:1;background:#F6FAF1;border:1px solid #DCEAC9;border-radius:10px;padding:12px 14px;">
        <div style="display:flex;justify-content:space-between;align-items:baseline;gap:10px;flex-wrap:wrap;">
          <div style="font-size:13px;font-weight:700;color:#3B6D11;">
            <?= htmlspecialchars($entry['reviewer_name'] ?? 'Reviewer') ?>
            <span style="font-size:11px;font-weight:600;background:#EAF3DE;color:#3B6D11;padding:2px 8px;border-radius:20px;margin-left:6px;">Reviewer</span>
          </div>
          <div style="font-size:11.5px;color:var(--text-muted);"><?= $__accWhen ? fmtDate($__accWhen) : '' ?></div>
        </div>
        <div style="font-size:14px;margin-top:8px;line-height:1.55;"><?= nl2br(htmlspecialchars($__accRoot)) ?></div>
      </div>
    </div>
  <?php endif; ?>

  <?php foreach ($__accMsgs as $__m): ?>
    <?php
      $__isRev = $__m['author_role'] === 'reviewer';
      $__ac = $__isRev ? '#3B6D11' : '#4F6EF7';
      $__bg = $__isRev ? '#F6FAF1' : '#F5F7FF';
      $__bd = $__isRev ? '#DCEAC9' : '#D8E0FD';
      $__pb = $__isRev ? '#EAF3DE' : '#E5EAFE';
      $__in = $__isRev ? '0' : '28px';
    ?>
    <div style="display:flex;gap:12px;margin-bottom:16px;margin-left:<?= $__in ?>;">
      <div style="flex:0 0 4px;background:<?= $__ac ?>;border-radius:2px;"></div>
      <div style="flex:1;background:<?= $__bg ?>;border:1px solid <?= $__bd ?>;border-radius:10px;padding:12px 14px;">
        <div style="display:flex;justify-content:space-between;align-items:baseline;gap:10px;flex-wrap:wrap;">
          <div style="font-size:13px;font-weight:700;color:<?= $__ac ?>;">
            <?= htmlspecialchars($__m['author_name']) ?>
            <span style="font-size:11px;font-weight:600;background:<?= $__pb ?>;color:<?= $__ac ?>;padding:2px 8px;border-radius:20px;margin-left:6px;">
              <?= $__isRev ? 'Reviewer' : 'Response' ?>
            </span>
          </div>
          <div style="font-size:11.5px;color:var(--text-muted);"><?= fmtDate($__m['created_at'], true) ?></div>
        </div>
        <div style="font-size:14px;margin-top:8px;line-height:1.55;"><?= nl2br(htmlspecialchars($__m['body'])) ?></div>
      </div>
    </div>
  <?php endforeach; ?>

  <?php if ($__accCanPost): ?>
    <form method="POST" action="/projects/jatinder-kpi/reply" style="margin-top:18px;border-top:1px solid var(--border);padding-top:16px;"
          onsubmit="var b=document.getElementById('accReply_<?= $entry['id'] ?>');if(!b.value.trim()){alert('Please write a response before saving.');b.focus();return false;}return true;">
      <input type="hidden" name="id" value="<?= $entry['id'] ?>">
      <?php if ($isModalMode): ?><input type="hidden" name="modal" value="1"><?php endif; ?>
      <label style="font-size:11px;font-weight:700;text-transform:uppercase;letter-spacing:.05em;color:var(--text-muted);display:block;margin-bottom:6px;">
        Your response / explanation
      </label>
      <textarea name="reply_body" id="accReply_<?= $entry['id'] ?>" rows="4"
                placeholder="Explain the numbers, respond to the reviewer's points, or add context..."
                style="width:100%;padding:11px 14px;border:1px solid var(--border);border-radius:10px;font-size:14px;font-family:inherit;line-height:1.5;resize:vertical;"></textarea>
      <div style="display:flex;justify-content:space-between;align-items:center;gap:12px;margin-top:10px;flex-wrap:wrap;">
        <span style="font-size:12px;color:var(--text-muted);">
          Saved against this KPI record and visible to your reviewer. Your KPI figures and its approved status are not affected.
        </span>
        <button type="submit" class="btn-primary">Save Response</button>
      </div>
    </form>
  <?php endif; ?>
</div>
<?php endif; ?>

<!-- Review actions for Cesar/Sara — AT THE TOP for submitted entries -->
<?php if ($isReviewer && $entry['status']==='submitted'): ?>
<div class="vcard" style="border:2px solid #2563eb;background:linear-gradient(120deg,#eff6ff,#fff);">
  <h3 style="color:#1d4ed8;">📋 Review This KPI</h3>
  <form method="POST" action="/projects/jatinder-kpi/review-action<?= $isModalMode?'?modal=1':'' ?>"
        onsubmit="var s=this.querySelector('[name=review_score]').value,c=this.querySelector('[name=review_comment_approve]').value.trim();if(!s||s<1||s>10){alert('Please enter a score between 1 and 10.');return false;}if(!c){alert('Please enter review comments.');return false;}return confirm('Approve and lock this KPI record?');">
    <input type="hidden" name="id" value="<?= $entry['id'] ?>">
    <input type="hidden" name="action" value="approve">
    <?php if ($isModalMode): ?><input type="hidden" name="modal" value="1"><?php endif; ?>
    <div style="display:grid;grid-template-columns:140px 1fr;gap:14px;align-items:end;margin-bottom:14px;">
      <div class="fld">
        <label>Score (1–10) <span style="color:#dc2626;">*</span></label>
        <input type="number" name="review_score" min="1" max="10" step="1" required placeholder="1–10"
               style="width:100%;padding:10px;border:1px solid var(--border);border-radius:10px;font-size:16px;font-weight:700;">
      </div>
      <div class="fld">
        <label>Review Comment <span style="color:#dc2626;">*</span></label>
        <input type="text" name="review_comment_approve" required placeholder="What went well, areas to improve..."
               style="width:100%;padding:10px;border:1px solid var(--border);border-radius:10px;font-size:14px;">
      </div>
    </div>
    <button type="submit" class="btn-primary" style="padding:11px 28px;">✅ Approve &amp; Lock</button>
  </form>

  <form method="POST" action="/projects/jatinder-kpi/review-action<?= $isModalMode?'?modal=1':'' ?>"
        style="margin-top:14px;padding-top:14px;border-top:1px solid var(--border);"
        onsubmit="var r=this.querySelector('[name=reason]').value.trim();if(!r){alert('A return reason is required.');return false;}return true;">
    <input type="hidden" name="id" value="<?= $entry['id'] ?>">
    <input type="hidden" name="action" value="return">
    <?php if ($isModalMode): ?><input type="hidden" name="modal" value="1"><?php endif; ?>
    <div class="fld" style="margin-bottom:10px;">
      <label>Return Reason <span style="color:#dc2626;">*</span></label>
      <input type="text" name="reason" required placeholder="Explain what needs to be corrected..."
             style="width:100%;padding:10px;border:1px solid #fca5a5;border-radius:10px;font-size:14px;">
    </div>
    <button type="submit" style="padding:11px 28px;background:none;border:2px solid #dc2626;color:#dc2626;border-radius:10px;cursor:pointer;font-size:13px;font-weight:700;">
      ↩ Return for Edits
    </button>
  </form>
</div>
<?php endif; ?>

<!-- KPI Data -->
<div class="vcard"><h3>Week</h3>
  <div class="vgrid">
    <div class="vfld"><label>Week Starting</label><div class="val"><?= fmtDate($entry['week_starting'] ?? '') ?></div></div>
    <div class="vfld"><label>Week Ending</label><div class="val"><?= fmtDate($entry['week_ending']) ?></div></div>
  </div>
</div>

<div class="vcard"><h3>Staffing &amp; Training</h3>
  <div class="vgrid">
    <div class="vfld"><label>New Hires</label><div class="val"><?= (int)($entry['new_hires']??0) ?></div></div>
    <div class="vfld"><label>New Hire Name(s)</label><div class="val"><?= htmlspecialchars($entry['new_hire_names']??'—') ?></div></div>
    <div class="vfld"><label>Training Hours</label><div class="val"><?= htmlspecialchars($entry['new_hire_training_hrs']??'0') ?></div></div>
    <div class="vfld"><label>New Contract Hours</label><div class="val"><?= htmlspecialchars($entry['new_contract_hrs']??'0') ?></div></div>
    <div class="vfld"><label>New Contract Site</label><div class="val"><?= htmlspecialchars($entry['new_contract_site']??'—') ?></div></div>
    <div class="vfld"><label>Lost Contract Hours</label><div class="val"><?= htmlspecialchars($entry['lost_contract_hrs']??'0') ?></div></div>
    <div class="vfld"><label>Lost Contract Site</label><div class="val"><?= htmlspecialchars($entry['lost_contract_site']??'—') ?></div></div>
  </div>
</div>

<div class="vcard"><h3>Violations</h3>
  <div class="vgrid">
    <div class="vfld"><label>Guards Sleeping</label><div class="val"><?= (int)($entry['guards_sleeping']??0) ?></div></div>
    <div class="vfld"><label>Serious Violations</label><div class="val"><?= (int)($entry['serious_violations']??0) ?></div></div>
    <div class="vfld"><label>Failed Re-Inspections</label><div class="val"><?= (int)($entry['failed_reinspections']??0) ?></div></div>
    <div class="vfld"><label>Avg Score / Site</label><div class="val"><?= htmlspecialchars($entry['avg_score_per_site']??'0') ?></div></div>
    <div class="vfld"><label>Uniform Compliance %</label><div class="val"><?= (int)($entry['uniform_compliance_pct']??0) ?>%</div></div>
  </div>
  <div class="vgrid3" style="margin-top:14px;">
    <div class="vfld"><label>Violation Description</label><div class="val"><?= nl2br(htmlspecialchars($entry['violation_desc']??'—')) ?></div></div>
    <div class="vfld"><label>How Handled</label><div class="val"><?= nl2br(htmlspecialchars($entry['violation_handled']??'—')) ?></div></div>
    <div class="vfld"><label>Improvements</label><div class="val"><?= nl2br(htmlspecialchars($entry['improvements']??'—')) ?></div></div>
  </div>
</div>

<div class="vcard"><h3>Client Activity</h3>
  <div class="vgrid">
    <div class="vfld"><label>Client Meetings</label><div class="val"><?= (int)($entry['client_meetings']??0) ?></div></div>
    <div class="vfld"><label>Client Names Met</label><div class="val"><?= htmlspecialchars($entry['client_names_met']??'—') ?></div></div>
    <div class="vfld"><label>Complaints</label><div class="val"><?= (int)($entry['client_complaints']??0) ?></div></div>
    <div class="vfld"><label>Compliments</label><div class="val"><?= (int)($entry['client_compliments']??0) ?></div></div>
    <div class="vfld"><label>Avg Response Time (hrs)</label><div class="val"><?= htmlspecialchars($entry['avg_response_time_hrs']??'0') ?></div></div>
  </div>
</div>

<div class="vcard"><h3>Sites Visited This Week</h3>
  <div class="val"><?= !empty($sites_visited) ? htmlspecialchars(implode(', ',$sites_visited)) : '—' ?></div>
</div>

<div class="vcard"><h3>Guard Recognition</h3>
  <div class="vgrid">
    <div class="vfld"><label>Guards Recommended</label><div class="val"><?= (int)($entry['recognition_count']??0) ?></div></div>
    <div class="vfld"><label>Name(s) &amp; Reason</label><div class="val"><?= htmlspecialchars($entry['recognition_names']??'—') ?></div></div>
  </div>
</div>

<div class="vcard"><h3>🔧 Site Improvement Work with Employees</h3>
<?php
$improvements = json_decode($entry['site_improvements'] ?? '[]', true) ?: [];
if (empty($improvements)): ?>
  <div style="color:var(--text-muted);font-size:13px;">No site improvement entries this week.</div>
<?php else: foreach ($improvements as $i => $imp): ?>
  <div style="background:var(--bg);border:1px solid var(--border);border-radius:10px;padding:14px;margin-bottom:10px;">
    <div style="display:grid;grid-template-columns:repeat(auto-fill,minmax(150px,1fr));gap:12px;margin-bottom:10px;">
      <div class="vfld"><label>Employee</label><div class="val"><?= htmlspecialchars($imp['employee']??'—') ?></div></div>
      <div class="vfld"><label>Site</label><div class="val"><?= htmlspecialchars($imp['site']??'—') ?></div></div>
      <div class="vfld"><label>Area</label><div class="val"><?= htmlspecialchars($imp['area']??'—') ?></div></div>
      <div class="vfld"><label>Score Before</label><div class="val"><?= htmlspecialchars($imp['score_before']??'—') ?></div></div>
      <div class="vfld"><label>Score This Week</label><div class="val"><?= htmlspecialchars($imp['score_this_week']??'—') ?></div></div>
      <div class="vfld"><label>ROT Recertified</label><div class="val"><?= htmlspecialchars($imp['recertified_rot']??'—') ?></div></div>
      <div class="vfld"><label>Status</label><div class="val"><?= htmlspecialchars($imp['improvement_status']??'—') ?></div></div>
    </div>
    <?php if (!empty($imp['rot_signed_desc'])): ?>
    <div class="vfld" style="margin-bottom:8px;"><label>ROT Signed Description</label><div class="val"><?= nl2br(htmlspecialchars($imp['rot_signed_desc'])) ?></div></div>
    <?php endif; ?>
    <?php if (!empty($imp['improvement_notes'])): ?>
    <div class="vfld"><label>Improvement Notes</label><div class="val"><?= nl2br(htmlspecialchars($imp['improvement_notes'])) ?></div></div>
    <?php endif; ?>
  </div>
<?php endforeach; endif; ?>
</div>
  <div class="vgrid">
    <div class="vfld"><label>Day Total</label><div class="val"><?= (int)($entry['insp_day_total']??0) ?></div></div>
    <div class="vfld"><label>Day Failed</label><div class="val"><?= (int)($entry['insp_day_failed']??0) ?></div></div>
    <div class="vfld"><label>Swing Total</label><div class="val"><?= (int)($entry['insp_swing_total']??0) ?></div></div>
    <div class="vfld"><label>Swing Failed</label><div class="val"><?= (int)($entry['insp_swing_failed']??0) ?></div></div>
    <div class="vfld"><label>Grave Total</label><div class="val"><?= (int)($entry['insp_grave_total']??0) ?></div></div>
    <div class="vfld"><label>Grave Failed</label><div class="val"><?= (int)($entry['insp_grave_failed']??0) ?></div></div>
    <div class="vfld"><label>Total Inspections</label><div class="val"><?= (int)($entry['insp_total']??0) ?></div></div>
    <div class="vfld"><label>Total Failed</label><div class="val"><?= (int)($entry['insp_failed_total']??0) ?></div></div>
  </div>
</div>

<div class="vcard"><h3>Site Inspections</h3>
  <div class="vgrid">
    <div class="vfld"><label>Units Picked Up</label><div class="val"><?= (int)($entry['msu_pickup']??0) ?></div></div>
    <div class="vfld"><label>Units Dropped Off</label><div class="val"><?= (int)($entry['msu_dropoff']??0) ?></div></div>
    <div class="vfld"><label>Troubleshooting</label><div class="val"><?= htmlspecialchars($entry['msu_troubleshoot']??'—') ?></div></div>
    <div class="vfld"><label>After-Hours Response</label><div class="val"><?= htmlspecialchars($entry['afterhours_response']??'—') ?></div></div>
  </div>
  <?php if ($entry['msu_notes']): ?><div class="vfld" style="margin-top:10px;"><label>MSU Notes</label><div class="val"><?= nl2br(htmlspecialchars($entry['msu_notes'])) ?></div></div><?php endif; ?>
</div>

<div class="vcard"><h3>Phone Audit</h3>
  <div class="vgrid">
    <div class="vfld"><label>Audit Done</label><div class="val"><?= htmlspecialchars($entry['phone_audit_done']??'—') ?></div></div>
    <div class="vfld"><label>Items Saved</label><div class="val"><?= htmlspecialchars($entry['phone_items_saved']??'—') ?></div></div>
    <div class="vfld"><label>Unauthorized Apps</label><div class="val"><?= htmlspecialchars($entry['phone_unauth_apps']??'—') ?></div></div>
    <div class="vfld"><label>Phones Audited</label><div class="val"><?= (int)($entry['phone_count']??0) ?></div></div>
  </div>
  <?php if ($entry['phone_notes']): ?><div class="vfld" style="margin-top:10px;"><label>Phone Notes</label><div class="val"><?= nl2br(htmlspecialchars($entry['phone_notes'])) ?></div></div><?php endif; ?>
</div>

<div class="vcard"><h3>General Notes</h3>
  <div class="vgrid3">
    <div class="vfld"><label>Highlights</label><div class="val"><?= nl2br(htmlspecialchars($entry['highlights']??'—')) ?></div></div>
    <div class="vfld"><label>Concerns</label><div class="val"><?= nl2br(htmlspecialchars($entry['concerns']??'—')) ?></div></div>
    <div class="vfld"><label>Remarks</label><div class="val"><?= nl2br(htmlspecialchars($entry['remarks']??'—')) ?></div></div>
  </div>
</div>

<div style="padding-bottom:32px;">
  <?php if ($isReviewer): ?>
    <a href="/projects/jatinder-kpi/review" class="btn-secondary" style="text-decoration:none;">← Back to Review Queue</a>
  <?php else: ?>
    <a href="/projects/jatinder-kpi/my-records" class="btn-secondary" style="text-decoration:none;">← Back to My Records</a>
  <?php endif; ?>
</div>

<?php if ($isModalMode): ?>
<script>
(function(){
  function autoResize(){try{window.parent.postMessage({type:'kpi-resize',height:document.body.scrollHeight},'*');}catch(e){}}
  window.addEventListener('load',autoResize);
  new MutationObserver(autoResize).observe(document.body,{childList:true,subtree:true});
})();
</script>
</div></main></div></body></html>
<?php else: ?>
<?php require __DIR__ . '/../layout/admin_footer.php'; ?>
<?php endif; ?>
