<?php
require_once __DIR__ . '/config/config.php';
if (session_status() !== PHP_SESSION_ACTIVE) session_start();

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    require_once __DIR__ . '/includes/auth.php';
    $result = attemptLogin($_POST['login_id'] ?? '', $_POST['password'] ?? '');
    if ($result['ok']) {
        header('Location: dashboard.php');
        exit;
    }
    $error = $result['error'];
}

// Public read: just codes/names for the login dropdown, no auth needed for this list.
$pdo = getPDO();
$clrcs = $pdo->query('SELECT clrccode, clrcname FROM clrc ORDER BY clrcname')->fetchAll();
?>
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Sign in — <?= htmlspecialchars(SITE_NAME) ?></title>
  <script src="https://cdn.tailwindcss.com"></script>
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
  <style>body{font-family:'Inter',system-ui,sans-serif}</style>
</head>
<body class="min-h-screen flex items-center justify-center bg-gradient-to-b from-[#0d1938] via-[#0d1938] to-slate-950 px-4">
  <div class="w-full max-w-md">
    <div class="flex flex-col items-center mb-8">
      <div class="flex h-14 w-14 items-center justify-center rounded-2xl bg-blue-600 text-white shadow-lg text-2xl">♿</div>
      <h1 class="mt-4 text-xl font-bold text-white">CWSN Data Portal</h1>
      <p class="text-sm text-slate-400 mt-1">Samagra Shiksha Mission · Purba Bardhaman</p>
    </div>

    <form method="post" class="bg-white rounded-xl border border-slate-200 shadow-lg p-6 sm:p-8 space-y-5">
      <div>
        <label class="block text-xs font-semibold uppercase tracking-wide text-slate-500 mb-1.5">Login as</label>
        <select name="login_id" required class="w-full rounded-lg border border-slate-300 px-3 py-2.5 text-sm">
          <option value="admin">Admin (District)</option>
          <?php foreach ($clrcs as $c): ?>
            <option value="<?= htmlspecialchars($c['clrccode']) ?>">
              <?= htmlspecialchars($c['clrcname']) ?> — <?= htmlspecialchars($c['clrccode']) ?>
            </option>
          <?php endforeach; ?>
        </select>
      </div>
      <div>
        <label class="block text-xs font-semibold uppercase tracking-wide text-slate-500 mb-1.5">Password</label>
        <input type="password" name="password" required autofocus
               class="w-full rounded-lg border border-slate-300 px-3 py-2.5 text-sm">
      </div>

      <?php if ($error): ?>
        <div class="rounded-lg bg-rose-50 border border-rose-200 px-3 py-2 text-sm text-rose-700">
          <?= htmlspecialchars($error) ?>
        </div>
      <?php endif; ?>

      <button type="submit" class="w-full rounded-lg bg-blue-600 hover:bg-blue-700 text-white font-semibold py-2.5 text-sm">
        Sign in
      </button>
      <p class="text-center text-xs text-slate-400">
        First time signing in? Use the default password given to you by the District Admin —
        you'll be asked to set your own right after.
      </p>
    </form>
  </div>
</body>
</html>
