<?php
require_once 'auth.php';
include 'db.php';
requireLogin($conn);

header('Content-Type: application/json');

$staff_id = (int) ($_GET['id'] ?? 0);

if ($staff_id <= 0) {
    echo json_encode(['success' => false, 'message' => 'Invalid staff ID']);
    exit();
}

// Fetch staff data
$stmt = mysqli_prepare($conn, "SELECT id, agency_type, name, position, department, agency, address FROM staff_distribution_list WHERE id = ? AND deleted_at IS NULL");
if (!$stmt) {
    echo json_encode(['success' => false, 'message' => 'Database error']);
    exit();
}

mysqli_stmt_bind_param($stmt, "i", $staff_id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$staff = mysqli_fetch_assoc($result);

if (!$staff) {
    echo json_encode(['success' => false, 'message' => 'Staff record not found']);
    exit();
}

echo json_encode(['success' => true, 'staff' => $staff]);
exit();
