<?php
session_start();
require_once __DIR__ . '/../config/db.php';

if (isset($_SESSION['admin_id'])) { header('Location: index.php'); exit; }

$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $email = trim($_POST['email'] ?? '');
    $pass  = $_POST['password'] ?? '';
    $stmt  = $pdo->prepare("SELECT * FROM surveyors WHERE email = ? AND role IN ('admin','supervisor') AND is_active = 1");
    $stmt->execute([$email]);
    $user = $stmt->fetch();
    if ($user && password_verify($pass, $user['password_hash'])) {
        $_SESSION['admin_id']   = $user['id'];
        $_SESSION['admin_role'] = $user['role'];
        $_SESSION['admin_name'] = $user['name'];
        header('Location: index.php');
        exit;
    }
    $error = 'Invalid email or password.';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Bancharee Admin — Login</title>
  <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="min-h-screen bg-gradient-to-br from-slate-900 via-blue-950 to-slate-900 flex items-center justify-center p-4">

  <div class="w-full max-w-sm">

    <!-- Logo / brand -->
    <div class="text-center mb-8">
      <div class="text-4xl mb-3">🗺</div>
      <h1 class="text-white text-2xl font-bold tracking-tight">Bancharee Admin</h1>
      <p class="text-blue-300 text-sm mt-1">Bancharedanda Compensation Survey System</p>
    </div>

    <!-- Card -->
    <div class="bg-white rounded-2xl shadow-2xl p-8">

      <?php if ($error): ?>
        <div class="mb-4 px-4 py-3 bg-red-50 border border-red-200 rounded-xl text-red-700 text-sm flex items-center gap-2">
          <span>⚠️</span><?= htmlspecialchars($error) ?>
        </div>
      <?php endif ?>

      <form method="post" class="space-y-5">
        <div>
          <label class="block text-sm font-medium text-slate-700 mb-1.5">Email Address</label>
          <input type="email" name="email" required autofocus
                 class="w-full px-4 py-2.5 border border-gray-300 rounded-xl text-sm
                        focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent
                        transition-all placeholder-gray-400"
                 placeholder="admin@bancharee.np">
        </div>
        <div>
          <label class="block text-sm font-medium text-slate-700 mb-1.5">Password</label>
          <input type="password" name="password" required
                 class="w-full px-4 py-2.5 border border-gray-300 rounded-xl text-sm
                        focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent
                        transition-all placeholder-gray-400"
                 placeholder="••••••••">
        </div>
        <button type="submit"
                class="w-full py-3 bg-blue-600 hover:bg-blue-700 active:bg-blue-800
                       text-white font-semibold rounded-xl text-sm transition-colors shadow-sm">
          Sign In
        </button>
      </form>

    </div>

    <p class="text-center text-slate-500 text-xs mt-6">
      Kathmandu Metropolitan City · Field Survey Platform
    </p>
  </div>

</body>
</html>
