<?php

include_once './dbconn.php';

// Initialize response array
$response = ['success' => false];

// Check if selected IDs are received via POST
if (isset($_POST['selected']) && is_array($_POST['selected'])) {
    // Array to store IDs of successfully denied items
    $deniedIds = [];
    $current_date = date("Y-m-d H:i:s");
    
    // Loop through each selected ID
    foreach ($_POST['selected'] as $id) {
        // Update status to 'Denied' for the current ID
        $sql = "UPDATE web_app_enrollee_data 
                SET status = 'Deactivated',
                newchanges = 'yes',
                approval_date = '$current_date',
                changedate = '$current_date'
                WHERE id = '$id'";
                
        if ($conn->query($sql) === TRUE) {
            $deniedIds[] = $id; // Add ID to the list of successfully denied items
        } else {
            // Handle error
            // You can log the error or perform other actions as needed
            echo "Error updating record: " . $conn->error;
        }
    }

    // Update response on successful bulk denial
    if (!empty($deniedIds)) {
        $response['success'] = true;
        $response['deniedItems'] = $deniedIds;
    }
} else {
    // If no selected IDs received, handle the situation accordingly
    $response['message'] = "No selected items received for bulk deactivation.";
}

// Close connection
$conn->close();

// Send JSON response
header('Content-Type: application/json');
echo json_encode($response);

?>
