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

$notif = ""; // untuk sweetalert

if(isset($_POST['login'])){
    $user = $_POST['username'];
    $pass = $_POST['password'];

    $data = $koneksi->query("SELECT * FROM login WHERE username='$user'")
            or die("Query error: " . $koneksi->error);

    $d = $data->fetch_assoc();

    if($d && password_verify($pass, $d['password'])){
        $_SESSION['admin'] = $d['username'];

        $notif = "
        Swal.fire({
            icon: 'success',
            title: 'Login Berhasil',
            text: 'Selamat datang $user',
            showConfirmButton: false,
            timer: 1500
        }).then(() => {
            window.location='dashboard_admin.php';
        });
        ";
    } else {
        $notif = "
        Swal.fire({
            icon: 'error',
            title: 'Login Gagal',
            text: 'Username atau password salah!'
        });
        ";
    }
}
?>

<!DOCTYPE html>
<html>
<head>
<title>Login Admin</title>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700&display=swap" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<style>
body {
    font-family: 'Poppins', sans-serif;
    background: linear-gradient(135deg,#0059ff,#f4f6f9);
    height:100vh;
    display:flex;
    justify-content:center;
    align-items:center;
}

.card-modern {
    border-radius: 20px;
    box-shadow: 0 15px 40px rgba(0,0,0,0.15);
    border:none;
}

.title {
    font-weight:700;
    color:#0059ff;
}
</style>

</head>

<body>

<div class="col-md-4">
<div class="card card-modern p-4">

<h3 class="text-center title mb-3">🔐 Login Admin</h3>

<form method="POST">

<div class="form-group">
<label>Username</label>
<input type="text" name="username" class="form-control" required>
</div>

<div class="form-group">
<label>Password</label>
<input type="password" name="password" class="form-control" required>
</div>

<button type="submit" name="login" class="btn btn-primary btn-block mt-3">
Login
</button>

<a href="hal_utama.php" class="btn btn-dark btn-block mt-3">
⬅ Kembali
</a>

</form>

</div>
</div>

<?php if($notif): ?>
<script>
<?= $notif ?>
</script>
<?php endif; ?>

</body>
</html>