<?php
session_start();
require_once "../config/db.php";

if(!isset($_SESSION['user_id']) || $_SESSION['role'] != "admin"){
    header("Location: ../settings.php");
    exit();
}

$id = $_POST['user_id'];
$full_name = $_POST['full_name'];
$email = $_POST['email'];
$phone = $_POST['phone_number'];
$role = $_POST['role'];
$department = $_POST['department_id'] ?: NULL;

$stmt = mysqli_prepare($conn, 
"UPDATE users SET full_name=?, email=?, phone_number=?, role=?, department_id=? WHERE id=?");

mysqli_stmt_bind_param($stmt, "ssssii",
    $full_name,
    $email,
    $phone,
    $role,
    $department,
    $id
);

mysqli_stmt_execute($stmt);

header("Location: ../settings.php");
exit();