<?php
include 'inc/db.php';
if($_SERVER['REQUEST_METHOD'] == 'POST'){

    $email = trim($_POST['email']);
    $pass  = trim($_POST['password']);
    $password = md5($pass);

    if(empty($email) || empty($pass)){
        echo json_encode(['status'=>0,'msg'=>"All fields are required"]);
        exit;
    }

    $query = mysqli_query($conn, "SELECT * FROM tbl_register WHERE email='$email' AND password='$password' LIMIT 1");

    if(mysqli_num_rows($query) == 1){
        $user = mysqli_fetch_assoc($query);
        $_SESSION['user_id'] = $user['id'];
        $_SESSION['user_name'] = $user['name'];
        $_SESSION['user_role'] = $user['role'];

        // Return role for redirection
        echo json_encode(['status'=>1,'role'=>$user['role']]);
    } else {
        echo json_encode(['status'=>0,'msg'=>"Invalid email or password"]);
    }
}
?>
