<?php
include('koneksi.php');
session_start();

// Jika sudah login, langsung lempar ke halaman admin
if (isset($_SESSION['status']) && $_SESSION['status'] == "login") {
    header("location:admin_dashboard.php");
}

$pesan = "";
if (isset($_POST['login'])) {
    $username = mysqli_real_escape_string($koneksi, $_POST['username']);
    $password = $_POST['password'];

    $query = mysqli_query($koneksi, "SELECT * FROM users WHERE username='$username'");
    $data = mysqli_fetch_assoc($query);

    if (mysqli_num_rows($query) > 0) {
        // Verifikasi password (menggunakan hash)
        if (password_verify($password, $data['password'])) {
            $_SESSION['username'] = $data['username'];
            $_SESSION['role']     = $data['role'];
            $_SESSION['status']   = "login";
            header("location:admin_dashboard.php");
        } else {
            $pesan = "<div class='alert alert-danger'>Password salah!</div>";
        }
    } else {
        $pesan = "<div class='alert alert-danger'>Username tidak ditemukan!</div>";
    }
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login Admin - SI Plagiasi</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
        body { background: #e9ecef; height: 100vh; display: flex; align-items: center; }
        .login-card { width: 100%; max-width: 400px; margin: auto; border: none; border-radius: 15px; box-shadow: 0 10px 30px rgba(0,0,0,0.1); }
        .card-header { background: #2c3e50; color: white; text-align: center; border-radius: 15px 15px 0 0 !important; padding: 25px; }
        .btn-login { background: #1abc9c; color: white; border: none; font-weight: bold; }
        .btn-login:hover { background: #16a085; color: white; }
    </style>
</head>
<body>

<div class="container">
    <div class="card login-card">
        <div class="card-header">
            <h4 class="m-0">ADMIN LOGIN</h4>
            <small>Sistem Informasi Plagiasi PIAUD</small>
        </div>
        <div class="card-body p-4">
            <?= $pesan; ?>
            <form action="" method="POST">
                <div class="form-group">
                    <label><i class="fa fa-user mr-2"></i>Username</label>
                    <input type="text" name="username" class="form-control" placeholder="Masukkan username" required>
                </div>
                <div class="form-group">
                    <label><i class="fa fa-lock mr-2"></i>Password</label>
                    <input type="password" name="password" class="form-control" placeholder="Masukkan password" required>
                </div>
                <button type="submit" name="login" class="btn btn-login btn-block py-2 mt-4">MASUK</button>
                <a href="index.php" class="btn btn-link btn-block btn-sm text-muted mt-2">Kembali ke Beranda</a>
            </form>
        </div>
    </div>
</div>

</body>
</html>