<?php
session_start();
include '../data/dbconn.php'; // This should include your database connection code

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

    // Query to check user credentials
    $stmt = $conn->prepare('SELECT * FROM web_app_admin_users WHERE email = ? AND password = ?');
    $stmt->bind_param('ss', $email, $password);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows > 0) {
        $user = $result->fetch_assoc();
        // Store user data in session
        $_SESSION['user'] = $user;
        
        if ($user['role'] === 'admin') {
            header('Location: ../data');
        } elseif ($user['role'] === 'user') {
            header('Location: ../data/enrolleeview.php');
        } else {
            // Redirect to a default or error page if role is not recognized
            header('Location: index.php?error=invalid_role');
        }
    } else {
        // Redirect back to login page with error message
        header('Location: index.php?error=invalid_credentials');
    }
}
?>