<?php
// admin-login.php
session_start();

// If already logged in, redirect to dashboard
if (isset($_SESSION['is_admin']) && $_SESSION['is_admin'] === true) {
  header("Location: admin-dashboard.php");
  exit();
}

$error = '';

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

  $config = include('config.php');

  if ($email === $config['admin_email'] && $password === $config['admin_password']) {
    $_SESSION['is_admin'] = true;
    header("Location: admin-dashboard.php");
    exit();
  } else {
    $error = "Invalid credentials. Access denied.";
  }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Admin Login - Gobolcoin</title>
  <link rel="icon" type="image/png" href="/favicon.png">
  <style>
    body {
      font-family: Arial, sans-serif;
      background: linear-gradient(to top right, #ffffff 70%, #8B0000 130%);
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      margin: 0;
    }
    .login-box {
      background: #fff;
      padding: 2em;
      border-radius: 10px;
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
      width: 100%;
      max-width: 400px;
    }
    h2 {
      color: #8B0000;
      text-align: center;
    }
    input {
      width: 100%;
      padding: 10px;
      margin-top: 10px;
      border-radius: 8px;
      border: 1px solid #ccc;
    }
    button {
      margin-top: 1em;
      width: 100%;
      padding: 12px;
      background-color: #8B0000;
      color: white;
      border: none;
      border-radius: 8px;
      font-weight: bold;
      cursor: pointer;
    }
    .error {
      color: red;
      text-align: center;
      margin-top: 10px;
    }
  </style>
</head>
<body>
  <form class="login-box" method="POST">
    <h2>Admin Login</h2>
    <input type="email" name="email" placeholder="Email" required>
    <input type="password" name="password" placeholder="Password" required>
    <button type="submit">Login</button>
    <?php if (!empty($error)) echo "<div class='error'>$error</div>"; ?>
  </form>
</body>
</html>