<?php
session_start();
include("../config/config.php");

$email = $_POST['email'];
$password = $_POST['password'];

$stmt = $con->prepare("SELECT * FROM users WHERE email=?");
$stmt->bind_param("s", $email);
$stmt->execute();
$res = $stmt->get_result();
$user = $res->fetch_assoc();

if ($user && $password == $user['password']) {
    $_SESSION['user_id'] = $user['user_id'];
    $_SESSION['role'] = $user['role'];

    if ($user['role'] == 'superadmin') {
        header("Location: ../public/superadmin/dashboard.php");
    } elseif ($user['role'] == 'admin') {
        header("Location: ../public/admin/dashboard.php");
    } else {
        header("Location: ../public/user/dashboard.php");
    }
} else {
    echo "Invalid Login!";
}
