isLoggedIn()) { die(json_encode(['success' => false, 'message' => 'Unauthorized'])); } if ($_SERVER['REQUEST_METHOD'] !== 'POST') { die(json_encode(['success' => false, 'message' => 'Invalid request method'])); } $db = Database::getInstance(); $project_id = isset($_POST['project_id']) ? (int)$_POST['project_id'] : 0; if (!$project_id) { die(json_encode(['success' => false, 'message' => 'Invalid project ID'])); } // Verify user has access to this project $stmt = $db->prepare("SELECT id FROM projects WHERE id = ? AND created_by = ?"); $stmt->bind_param('ii', $project_id, $_SESSION['user_id']); $stmt->execute(); if ($stmt->get_result()->num_rows === 0) { die(json_encode(['success' => false, 'message' => 'Access denied'])); } // Get POST data for filters $filters = isset($_POST['filters']) ? json_decode($_POST['filters'], true) : []; // Get all previously used panelist IDs in this project's selections $stmt = $db->prepare(" SELECT DISTINCT panelist_id FROM selection_members sm JOIN selections s ON sm.selection_id = s.id WHERE s.project_id = ? "); $stmt->bind_param('i', $project_id); $stmt->execute(); $used_panelists_result = $stmt->get_result(); $used_panelists = []; while ($row = $used_panelists_result->fetch_assoc()) { $used_panelists[] = $row['panelist_id']; } // Build WHERE clause based on filter criteria $where_conditions = []; // If we have used panelists, exclude them if (!empty($used_panelists)) { $used_panelists_str = "'" . implode("','", $used_panelists) . "'"; $where_conditions[] = "panelist_id NOT IN ($used_panelists_str)"; } // Process filter criteria from attributes if (!empty($filters)) { // Get attribute types $attr_types = []; $attr_query = $db->query("SELECT id, choice_type FROM attributes"); while ($attr = $attr_query->fetch_assoc()) { $attr_types[$attr['id']] = $attr['choice_type']; } foreach ($filters as $attr_id => $value) { if (!empty($value)) { // Skip attr_ prefix in keys if present $attr_id = str_replace('attr_', '', $attr_id); $choice_type = $attr_types[$attr_id] ?? 'single'; if ($choice_type === 'single') { // Single choice attributes $value = $db->escape($value); $where_conditions[] = "JSON_UNQUOTE(JSON_EXTRACT(attribute_values, '$.{$attr_id}')) = '{$value}'"; } else if ($choice_type === 'multiple' && is_array($value) && !empty($value)) { // Multiple choice attributes $or_conditions = []; foreach ($value as $v) { $escaped_value = $db->escape($v); // Use JSON_CONTAINS to check if array contains the value $or_conditions[] = "JSON_CONTAINS(attribute_values, '\"{$escaped_value}\"', '$.{$attr_id}')"; } if (!empty($or_conditions)) { $where_conditions[] = '(' . implode(' OR ', $or_conditions) . ')'; } } } } } // Build the SQL query to get count of eligible panelists $sql = "SELECT COUNT(*) as count FROM panel_data"; if (!empty($where_conditions)) { $sql .= " WHERE " . implode(' AND ', $where_conditions); } // For debugging error_log("Available count SQL: " . $sql); // Get the count $result = $db->query($sql); if (!$result) { die(json_encode(['success' => false, 'message' => 'Error querying panel data: ' . $db->getLastError()])); } $available_count = $result->fetch_assoc()['count']; // Return the count echo json_encode([ 'success' => true, 'count' => (int)$available_count ]); ?>