$ids) { if (!is_array($ids) || empty($ids)) jsonError("El grupo '$grupo' no tiene tomas seleccionadas."); foreach ($ids as $id) { if (!preg_match('/^[a-zA-Z0-9_]+$/', $id)) jsonError("ID de firma inválido: $id"); } } $pdo = db(); $stmt = $pdo->prepare( "SELECT id, datos_respuestas FROM turnero_consentimientos WHERE token = ? LIMIT 1" ); $stmt->execute([$token]); $tc = $stmt->fetch(PDO::FETCH_ASSOC); if (!$tc) jsonError('Consentimiento no encontrado.', 404); $dr = []; if ($tc['datos_respuestas']) { $d = json_decode($tc['datos_respuestas'], true); if (is_array($d)) $dr = $d; } // Store per-exam nested: { examName: { groupKey: [firmaIds] } } $existing = isset($dr['_tomas_config']) && is_array($dr['_tomas_config']) ? $dr['_tomas_config'] : []; // Reset old flat format (values were arrays of strings, not arrays of arrays) if (!empty($existing)) { $__first = reset($existing); if (is_array($__first) && !empty($__first) && is_string(reset($__first))) $existing = []; } $existing[$examType] = $cfg; $dr['_tomas_config'] = $existing; // Guardar selección completa de exámenes (array) para que el reload restaure todos $examTypes = isset($body['exam_types']) && is_array($body['exam_types']) ? array_values(array_filter($body['exam_types'])) : []; if ($examTypes) { $dr['_c8j2g16'] = count($examTypes) === 1 ? $examTypes[0] : $examTypes; } elseif ($examType !== '' && !array_key_exists('_c8j2g16', $dr)) { $dr['_c8j2g16'] = $examType; } if ($otroHora !== '') $dr['_otro_hora'] = $otroHora; $pdo->prepare("UPDATE turnero_consentimientos SET datos_respuestas = ? WHERE id = ?") ->execute([json_encode($dr, JSON_UNESCAPED_UNICODE), $tc['id']]); jsonOk([], 'Configuración de tomas guardada.');