Mini Shell

Direktori : /home/mhcadmin/.trash/
Upload File :
Current File : /home/mhcadmin/.trash/get_users_by_department.php

<?php
session_start();
$rootPath = dirname(__FILE__);
require_once $rootPath . '/config/config.php';
require_once $rootPath . '/include/auth_validate.php';

// Enable error reporting for debugging (remove in production)
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Set header to return JSON
header('Content-Type: application/json');

// Create a response array
$response = [
    'success' => false, 
    'message' => '', 
    'users' => [],  // Changed from 'admins' to 'users'
    'debug' => []
];

try {
    // Check if user is logged in - more flexible check
    if (!isset($_SESSION['user_id']) && !isset($_SESSION['admin_id']) && !isset($_SESSION['id'])) {
        $response['message'] = 'Unauthorized access - No user session';
        $response['debug']['session'] = $_SESSION;
        echo json_encode($response);
        exit();
    }
    
    // Check if department is provided
    if (!isset($_POST['department']) || empty($_POST['department'])) {
        $response['message'] = 'No department specified';
        echo json_encode($response);
        exit();
    }

    $department = trim($_POST['department']);
    
    // Check database connection
    if (!isset($conn) || !$conn) {
        $response['message'] = 'Database connection failed';
        $response['debug']['conn_isset'] = isset($conn) ? 'true' : 'false';
        echo json_encode($response);
        exit();
    }

    // Escape the department
    $department = mysqli_real_escape_string($conn, $department);

    // IMPORTANT FIX: Check what departments actually exist in the database
    $check_query = "SELECT DISTINCT UserDepartment FROM admin_accounts WHERE UserDepartment IS NOT NULL AND UserDepartment != '' ORDER BY UserDepartment";
    $check_result = mysqli_query($conn, $check_query);
    
    $available_departments = [];
    if ($check_result) {
        while ($dept_row = mysqli_fetch_assoc($check_result)) {
            $available_departments[] = $dept_row['UserDepartment'];
        }
    }
    $response['debug']['available_departments'] = $available_departments;
    $response['debug']['requested_department'] = $department;

    // UPDATED QUERY: Only select id and Full_Name - no admin_type
    // Also using 'users' terminology in comments but table name remains admin_accounts
    $query = "SELECT id, Full_Name 
              FROM admin_accounts 
              WHERE UserDepartment = '$department' 
              AND (Status = 'Active' OR Status IS NULL OR Status = '')
              ORDER BY Full_Name ASC";
    
    $response['debug']['query'] = $query;
    
    $result = mysqli_query($conn, $query);

    if (!$result) {
        $response['message'] = 'Database query error';
        $response['debug']['mysql_error'] = mysqli_error($conn);
        echo json_encode($response);
        exit();
    }

    $response['debug']['num_rows'] = mysqli_num_rows($result);

    $users = [];  // Changed from $admins to $users
    while ($row = mysqli_fetch_assoc($result)) {
        $users[] = [
            'id' => $row['id'],
            'full_name' => $row['Full_Name']
            // No admin_type field included
        ];
    }

    $response['success'] = true;
    $response['users'] = $users;  // Changed from 'admins' to 'users'
    $response['message'] = 'Users fetched successfully';  // Changed message
    $response['count'] = count($users);  // Changed from $admins to $users

} catch (Exception $e) {
    $response['message'] = 'Exception: ' . $e->getMessage();
    $response['debug']['exception'] = true;
    $response['debug']['exception_message'] = $e->getMessage();
}

// Return the response
echo json_encode($response);
exit();
?>