<?php
require '../config.php';
if (isset($_SESSION['admin_logged_in']) && $_SESSION['admin_logged_in'] === true) {
    header('Location: index.php');
    exit;
}
$msg = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username']);
    $password = $_POST['password'];

    $stmt = $mysqli->prepare("SELECT id, password FROM admins WHERE username = ?");
    $stmt->bind_param('s', $username);
    $stmt->execute();
    $stmt->bind_result($id, $hash);
    if ($stmt->fetch()) {
        if (password_verify($password, $hash)) {
            $_SESSION['admin_logged_in'] = true;
            $_SESSION['admin_id'] = $id;
            header('Location: index.php');
            exit;
        } else {
            $msg = 'Username atau password salah';
        }
    } else {
        $msg = 'Username atau password salah';
    }
    $stmt->close();
}
?>
<!doctype html>
<html>
<head><meta charset="utf-8"><title>Login Admin</title><link href="/dist/output.css" rel="stylesheet"></head>
<body class="bg-gray-100">
<div class="max-w-md mx-auto mt-20 bg-white p-6 rounded shadow">
  <h2 class="text-xl font-bold mb-4">Login Admin</h2>

  <?php if($msg): ?>
    <div class="text-red-600 mb-4"><?php echo htmlspecialchars($msg); ?></div>
  <?php endif; ?>

  <form method="post">
    <label class="block mb-2">Username</label>
    <input name="username" class="w-full p-2 border mb-4" required>

    <label class="block mb-2">Password</label>
    <input name="password" type="password" class="w-full p-2 border mb-4" required>

    <button class="bg-teal-600 text-white py-2 px-4 rounded">Login</button>
  </form>
</div>
</body>
</html>
