<?php
// public/partials/header.php
if (session_status() === PHP_SESSION_NONE) session_start();
$u = $_SESSION['user'] ?? null;
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title><?= htmlspecialchars($page_title ?? 'School CRM') ?></title>

  <!-- Bootstrap 5 -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">

  <style>
    body { background:#f5f6fa; }
    .card-soft { border: 1px solid rgba(0,0,0,.08); border-radius: 1rem; }
    .shadow-soft { box-shadow: 0 10px 30px rgba(0,0,0,.06); }
    .table-responsive { border-radius: 1rem; }
    .badge-soft { background: #eef2ff; color:#3730a3; }
  </style>
</head>
<body>

<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
  <div class="container-fluid">
    <a class="navbar-brand fw-semibold" href="/school-crm/public/dashboard.php">School CRM</a>

    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navMain">
      <span class="navbar-toggler-icon"></span>
    </button>

    <div class="collapse navbar-collapse" id="navMain">
      <ul class="navbar-nav me-auto mb-2 mb-lg-0">

        <?php if ($u): ?>
          <li class="nav-item"><a class="nav-link" href="/school-crm/public/dashboard.php">Dashboard</a></li>

          <?php if (($u['role'] ?? '') === 'ADMIN'): ?>
            <li class="nav-item"><a class="nav-link" href="/school-crm/public/admin/users.php">Users</a></li>
            <li class="nav-item"><a class="nav-link" href="/school-crm/public/admin/assignments.php">Assignments</a></li>
            <li class="nav-item"><a class="nav-link" href="/school-crm/public/admin/schools.php">Schools</a></li>
            <li class="nav-item"><a class="nav-link" href="/school-crm/public/admin/districts.php">Districts</a></li>
            <li class="nav-item"><a class="nav-link" href="/school-crm/public/admin/import_master.php">Master Import</a></li>
            <li class="nav-item"><a class="nav-link" href="/school-crm/public/admin/import_schools.php">Import Schools</a></li>
            <li class="nav-item"><a class="nav-link" href="/school-crm/public/participant_add.php">Add Participant</a></li>
            <li class="nav-item"><a class="nav-link" href="/school-crm/public/participants.php">Participants</a></li>
            <li class="nav-item dropdown">
              <a class="nav-link dropdown-toggle" href="#" id="reportsDropdown" role="button" data-bs-toggle="dropdown">Reports</a>
              <ul class="dropdown-menu">
                <li><a class="dropdown-item" href="/school-crm/reports/daily.php">Daily Report</a></li>
                <li><a class="dropdown-item" href="/school-crm/reports/rep_performance.php">Rep Performance</a></li>
                <li><a class="dropdown-item" href="/school-crm/reports/block_coverage.php">Block Coverage</a></li>
                <li><a class="dropdown-item" href="/school-crm/reports/top_gp.php">Top GPs</a></li>
                <li><a class="dropdown-item" href="/school-crm/reports/gp_coverage.php">GP Coverage</a></li>
              </ul>
            </li>

          <?php else: ?>
            <li class="nav-item"><a class="nav-link" href="/school-crm/public/visit_add.php">Add Visit</a></li>
            <li class="nav-item"><a class="nav-link" href="/school-crm/public/visits.php">My Visits</a></li>
            <li class="nav-item"><a class="nav-link" href="/school-crm/public/followups.php">Follow-ups</a></li>
            <li class="nav-item"><a class="nav-link" href="/school-crm/public/rep/my_schools.php">My Schools</a></li>

          <?php endif; ?>

        <?php endif; ?>

      </ul>

      <div class="d-flex align-items-center gap-2">
        <?php if ($u): ?>
          <span class="text-white-50 small d-none d-lg-inline">
            <?= htmlspecialchars($u['name'] ?? '') ?> • <?= htmlspecialchars($u['role'] ?? '') ?>
          </span>
          <a class="btn btn-outline-light btn-sm" href="/school-crm/logout.php">Logout</a>
        <?php endif; ?>
      </div>
    </div>
  </div>
</nav>      


<main class="container py-4">
<!-- Auto-uppercase helper: use data-uppercase or class="uppercase" on inputs/textareas -->
<style>
  input[data-uppercase], textarea[data-uppercase], input.uppercase, textarea.uppercase { text-transform: uppercase; }
</style>
<script>
  (function(){
    function handleInput(ev){
      const el = ev.target;
      if (!(el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement)) return;
      if (!el.matches('[data-uppercase], .uppercase')) return;
      try{
        const s = el.selectionStart, e = el.selectionEnd;
        el.value = el.value.toUpperCase();
        if (typeof s === 'number' && typeof e === 'number') el.setSelectionRange(s, e);
      } catch (err) { /* ignore */ }
    }
    function handlePaste(ev){
      const el = ev.target;
      if (!(el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement)) return;
      if (!el.matches('[data-uppercase], .uppercase')) return;
      setTimeout(()=>{ try{ el.value = el.value.toUpperCase(); }catch(e){} }, 0);
    }
    document.addEventListener('input', handleInput, true);
    document.addEventListener('paste', handlePaste, true);
  })();
</script>
