<?php
require_once __DIR__ . '/../config/db.php';
require_once __DIR__ . '/../includes/functions.php';

if (isset($_SESSION['admin_auth'])) {
    redirect(baseUrl('admin/index.php'));
}

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

    $stmt = $pdo->prepare("SELECT * FROM administrator WHERE username = ?");
    $stmt->execute(array($username));
    $admin = $stmt->fetch();

    if ($admin && password_verify($password, $admin['password'])) {
        $_SESSION['admin_auth'] = array(
            'id' => $admin['id'],
            'username' => $admin['username'],
            'nama' => $admin['nama']
        );
        // Clear any previous "please login" messages
        if (isset($_SESSION['flash']) && $_SESSION['flash']['message'] === 'Silahkan login terlebih dahulu.') {
            unset($_SESSION['flash']);
        }
        redirect(baseUrl('admin/index.php'));
    } else {
        $error = "Username atau password salah.";
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login Admin - SMAN 1 Barumun</title>
    <link rel="stylesheet" href="<?= baseUrl('dist/style.css') ?>">
</head>
<body class="bg-primary flex items-center justify-center min-h-screen p-6">
    <div class="max-w-md w-full">
        <div class="text-center mb-10 flex flex-col items-center">
            <div class="flex items-center gap-4 mb-2">
                <img src="<?= baseUrl('logo.png') ?>" alt="Logo" class="w-16 h-16 object-contain">
                <div>
                    <h1 class="text-3xl font-extrabold text-white tracking-tight">SMA Neg. 1<span class="text-accent"> Barumun</span></h1>
                    <p class="text-primary-light font-medium uppercase tracking-widest text-sm">Admin Dashboard Panel</p>
                </div>
            </div>
        </div>

        <div class="bg-white rounded-3xl p-10 shadow-2xl">
            <h2 class="text-2xl font-bold text-slate-800 mb-2">Selamat Datang</h2>
            <p class="text-slate-500 mb-8">Silahkan masuk ke akun administrator Anda.</p>

            <?php if (isset($error)): ?>
                <div class="mb-6 p-4 rounded-xl bg-red-100 text-red-700 border border-red-200 text-sm">
                    <?= $error ?>
                </div>
            <?php endif; ?>

            <form action="" method="POST" class="space-y-6">
                <div>
                    <label class="block text-sm font-bold text-slate-700 mb-2">Username</label>
                    <input type="text" name="username" required class="w-full px-5 py-4 rounded-xl border border-slate-200 focus:border-primary focus:ring-4 focus:ring-primary/10 outline-none transition-all" placeholder="Masukkan username">
                </div>
                <div>
                    <label class="block text-sm font-bold text-slate-700 mb-2">Password</label>
                    <input type="password" name="password" required class="w-full px-5 py-4 rounded-xl border border-slate-200 focus:border-primary focus:ring-4 focus:ring-primary/10 outline-none transition-all" placeholder="Masukkan password">
                </div>
                <button type="submit" class="w-full btn-primary py-4">Masuk Sekarang</button>
            </form>
        </div>
        
        <p class="text-center text-primary-light mt-10 text-sm">
            &copy; <?= date('Y') ?> SMA Negeri 1 Barumun. All rights reserved.
        </p>
    </div>
</body>
</html>
