<?php
include 'includes/auth.php';
include 'db.php';

if($_SERVER['REQUEST_METHOD'] === 'POST'){
    $request_id = intval($_POST['request_id']);
    $staff_id = intval($_POST['staff_id']);

    // Fetch staff fullname
    $staff_result = $conn->query("SELECT fullname FROM staff WHERE id=$staff_id LIMIT 1");
    $staff = $staff_result->fetch_assoc();
    $staff_name = $staff['fullname'] ?? '';

    // Assign staff to request
    $stmt = $conn->prepare("UPDATE maintenance_requests SET staff_id=?, staff_name=? WHERE id=?");
    $stmt->bind_param("isi", $staff_id, $staff_name, $request_id);
    $stmt->execute();
    $stmt->close();

    header("Location: maintenance_requests.php");
    exit;
}
?>