<?php
require_once __DIR__ . '/includes/auth.php';
requireLogin();

$pdo = getPDO();
[$scopeSql, $scopeParams] = clrcScopeSql();

function countWhere(PDO $pdo, string $extra, array $params): int {
    $stmt = $pdo->prepare("SELECT COUNT(*) c FROM cwsn WHERE $extra");
    $stmt->execute($params);
    return (int)$stmt->fetch()['c'];
}

$total      = countWhere($pdo, "$scopeSql AND status = '0'", $scopeParams);
$incomplete = countWhere($pdo, "$scopeSql AND status = '2'", $scopeParams);
$deleted    = countWhere($pdo, "$scopeSql AND status = '1'", $scopeParams);
$boys       = countWhere($pdo, "$scopeSql AND status = '0' AND gender = 'BOY'", $scopeParams);
$girls      = countWhere($pdo, "$scopeSql AND status = '0' AND gender = 'GIRL'", $scopeParams);

$pageTitle = 'Dashboard';
require __DIR__ . '/includes/header.php';
?>
<div class="space-y-5">
  <div>
    <h1 class="text-lg font-bold text-slate-800">Welcome, <?= htmlspecialchars($_SESSION['display_name']) ?></h1>
    <p class="text-sm text-slate-500">
      <?= currentRole() === 'admin' ? 'District-wide overview of CWSN records.' : 'Overview of CWSN records in your CLRC.' ?>
    </p>
  </div>

  <div class="grid grid-cols-2 lg:grid-cols-5 gap-3">
    <div class="card p-4">
      <div class="text-2xl font-bold text-slate-800"><?= $total ?></div>
      <div class="text-xs text-slate-500 mt-1">Complete CWSN</div>
    </div>
    <div class="card p-4">
      <div class="text-2xl font-bold text-amber-600"><?= $incomplete ?></div>
      <div class="text-xs text-slate-500 mt-1">Incomplete Entries</div>
    </div>
    <div class="card p-4">
      <div class="text-2xl font-bold text-rose-600"><?= $deleted ?></div>
      <div class="text-xs text-slate-500 mt-1">Marked Deleted</div>
    </div>
    <div class="card p-4">
      <div class="text-2xl font-bold text-blue-600"><?= $boys ?></div>
      <div class="text-xs text-slate-500 mt-1">Boys</div>
    </div>
    <div class="card p-4">
      <div class="text-2xl font-bold text-pink-600"><?= $girls ?></div>
      <div class="text-xs text-slate-500 mt-1">Girls</div>
    </div>
  </div>

  <div class="card p-8 text-center text-slate-400 text-sm">
    This is the phase-1 PHP/MySQL foundation — login, roles, and basic counts are live.
    CWSN Records, Master Data, Reports, and the full analytical dashboard are built next.
  </div>
</div>
<?php require __DIR__ . '/includes/footer.php'; ?>
