<?php
session_start();

// Database config
include("config2.php");

if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }

$userid = isset($_POST['userid']) ? trim($_POST['userid']) : '';
$password = isset($_POST['password']) ? trim($_POST['password']) : '';

if($userid == '' || $password == '') {
    $_SESSION['login_error'] = "Please fill in both fields.";
    header("Location: login.php");
    exit;
}

$pass_hash = md5($password);
$sql = "SELECT * FROM user_permissions WHERE userid='$userid' AND password='$pass_hash'";
$result = mysqli_query($conn, $sql);

if($result && mysqli_num_rows($result) > 0) {
    $_SESSION['userid'] = $userid;
    header("Location: edit_permissions_ui.php?userid=$userid"); // redirect to permissions page or dashboard
} else {
    $_SESSION['login_error'] = "Invalid User ID or Password.";
    header("Location: login.php");
}

mysqli_close($conn);
?>
