<?php
session_start();
ob_start(); // Start output buffering to avoid "headers already sent" issues.

include 'koneksi.php'; // Include the database connection

// Check if login form is submitted
if (isset($_POST['login'])) {
    $email = trim($_POST['email']); // Sanitize email input
    $password = $_POST['password']; // Get the password

    // Prepare a statement to fetch user data based on email
    $stmt = $koneksi->prepare("SELECT * FROM pengguna WHERE email = ?");
    $stmt->bind_param("s", $email);
    $stmt->execute();
    $result = $stmt->get_result();

    // If user exists and password is correct
    if ($result && $result->num_rows > 0) {
        $row = $result->fetch_assoc();

        // Verify the password
        if (password_verify($password, $row['password'])) {
            // Set session variables
            $_SESSION['id_pengguna'] = $row['id_pengguna'];
            $_SESSION['email'] = $email;
            $_SESSION['role'] = $row['role'];

            // Redirect based on role
            if ($row['role'] == 'guru') {
                header("Location: home_guru.php"); // Ensure path is correct for 'guru' role
                exit();
            } elseif ($row['role'] == 'siswa') {
                header("Location: home_siswa.php"); // Ensure path is correct for 'siswa' role
                exit();
            } elseif ($row['role'] == 'admin') {
                header("Location: home_admin.php"); // Ensure path is correct for 'siswa' role
                exit();
            } else {
                echo "<div class='alert alert-danger'>Role tidak dikenali!</div>";
            }
        } else {
            echo "<div class='alert alert-danger'>Email atau password salah!</div>";
        }
    } else {
        echo "<div class='alert alert-danger'>Email tidak ditemukan!</div>";
    }

    // Close the statement and the connection
    $stmt->close();
    $koneksi->close();
}

ob_end_flush(); // End output buffering
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #007BFF;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 10px;
        }
        .container {
            max-width: 400px;
            width: 100%;
            padding: 20px;
            border-radius: 15px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            background-color: white;
        }
        h2 {
            text-align: center;
            margin-bottom: 20px;
            color: #333;
        }
        .btn-custom {
            background-color: #4A90E2;
            color: white;
            width: 100%;
            padding: 10px;
            border-radius: 5px;
        }
        .btn-custom:hover {
            background-color: #357ABD;
        }
        .alert {
            margin-top: 15px;
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>Login</h2>
        <form method="POST" action="login.php">
            <div class="form-group">
                <label for="email">Email:</label>
                <input type="email" class="form-control" name="email" value="<?php echo isset($email) ? htmlspecialchars($email) : ''; ?>" required>
            </div>
            
            <div class="form-group">
                <label for="password">Password:</label>
                <input type="password" class="form-control" name="password" required>
            </div>

            <button type="submit" name="login" class="btn btn-custom">Login</button>
        </form>
        <p>
            <a href="daftar.php" class="btn btn-secondary btn-block">Belum punya akun? Daftar</a>
        </p>
    </div>

    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>
