<?php
require __DIR__ . '/auth.php';

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $password = $_POST['password'] ?? '';

    if (portal_login($password)) {
        // setelah login, kembali ke halaman yang diminta
        $next = $_GET['next'] ?? '/';
        header('Location: ' . $next);
        exit;
    } else {
        $error = 'Password salah, coba lagi.';
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
  <meta charset="utf-8">
  <title>Login Portal Keperawatan</title>
  <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet">
  <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="min-h-screen bg-slate-100 flex items-center justify-center">
  <div class="bg-white rounded-2xl shadow-xl p-8 w-full max-w-sm">
    <h1 class="text-xl font-semibold text-center mb-4">Portal Keperawatan RS BaliMéd</h1>

    <?php if ($error): ?>
      <p class="text-sm text-red-600 mb-3 text-center"><?= htmlspecialchars($error) ?></p>
    <?php endif; ?>

    <form method="post" class="space-y-4">
      <div>
        <label class="block text-sm mb-1">Password Akses</label>
        <input type="password" name="password" required
               class="w-full border rounded-lg px-3 py-2 focus:ring-2 focus:ring-blue-500 outline-none">
      </div>
      <button type="submit"
              class="w-full bg-blue-600 hover:bg-blue-700 text-white font-semibold py-2.5 rounded-lg">
        Masuk
      </button>
    </form>
  </div>
</body>
</html>
