<?php
session_start();
include('../includes/config.php'); 

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

    $result = mysqli_query($conn, "SELECT * FROM users WHERE username='$username' AND role='admin'");

    if(mysqli_num_rows($result) === 1){
        $row = mysqli_fetch_assoc($result);
        if(password_verify($password, $row['password'])){
            $_SESSION['user_id'] = $row['id'];
            $_SESSION['username'] = $row['username'];
            $_SESSION['role'] = $row['role']; 
            header("Location: index_admin.php"); 
            exit;
        } else {
            $error = "Password salah!";
        }
    } else {
        $error = "Username tidak ditemukan!";
    }
}
?>
<!DOCTYPE html>
<html lang="id">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login Admin - Alfateeh Sepatuku</title>
<style>
body { font-family: 'Poppins', sans-serif; background:#f5f7fa; display:flex; justify-content:center; align-items:center; height:100vh; }
.login-container { background:#1e88e5; padding:40px 30px; border-radius:15px; width:380px; text-align:center; color:#fff; }
.login-container h1 { font-size:2rem; margin-bottom:30px; }
.login-container input { width:100%; padding:12px; margin:10px 0; border-radius:8px; border:none; font-size:1rem; }
.login-container button { width:100%; padding:12px; margin-top:15px; border:none; border-radius:8px; background:#00b4d8; font-weight:700; cursor:pointer; }
.login-container button:hover { background:#1565c0; }
.error-message { color:#ffeb3b; margin-bottom:15px; font-weight:600; }
</style>
</head>
<body>
<div class="login-container">
    <h1>Login Admin</h1>
    <?php if(isset($error)) echo "<div class='error-message'>$error</div>"; ?>
    <form method="POST">
        <input type="text" name="username" placeholder="Username" required>
        <input type="password" name="password" placeholder="Password" required>
        <button type="submit" name="login">Login</button>
    </form>
</div>
</body>
</html>
