import React, { useState } from 'react'; import { Form, Input, Button, Switch, Radio, Typography, Space, Card } from 'antd'; import { DeleteOutlined, EyeOutlined, FileOutlined } from '@ant-design/icons'; import FileUploadHandler from './FileUploadHandler'; import { NotifAlert } from '../../../../components/Global/ToastNotif'; import { getFileUrl, getFolderFromFileType } from '../../../../api/file-uploads'; const { Text } = Typography; const { TextArea } = Input; const SolutionFieldNew = ({ fieldKey, fieldName, index, solutionType, solutionStatus, isReadOnly = false, canRemove = true, onTypeChange, onStatusChange, onRemove, onFileUpload, onFileView, fileList = [], originalSolutionData = null }) => { const form = Form.useFormInstance(); const [currentFile, setCurrentFile] = useState(null); const [isDeleted, setIsDeleted] = useState(false); const fileUpload = Form.useWatch(['solution_items', fieldKey, 'fileUpload'], form); const file = Form.useWatch(['solution_items', fieldKey, 'file'], form); const getSolutionData = () => { try { return form.getFieldValue(['solution_items', fieldKey]) || {}; } catch (error) { return {}; } }; const nameValue = Form.useWatch(['solution_items', fieldKey, 'name'], form); const typeValue = Form.useWatch(['solution_items', fieldKey, 'type'], form); const textValue = Form.useWatch(['solution_items', fieldKey, 'text'], form); const fileNameValue = Form.useWatch(['solution_items', fieldKey, 'fileName'], form); const pathSolution = Form.useWatch(['solution_items', fieldKey, 'path_solution'], form); const [deleteCounter, setDeleteCounter] = useState(0); React.useEffect(() => { const getFileFromFormValues = () => { const hasValidFileUpload = fileUpload && typeof fileUpload === 'object' && Object.keys(fileUpload).length > 0; const hasValidFile = file && typeof file === 'object' && Object.keys(file).length > 0; const hasValidPath = pathSolution && pathSolution.trim() !== ''; const wasExplicitlyDeleted = (fileUpload === null || file === null || pathSolution === null) && !hasValidFileUpload && !hasValidFile && !hasValidPath; if (wasExplicitlyDeleted) { return null; } if (solutionType === 'text') { return null; } if (hasValidFileUpload) { return fileUpload; } if (hasValidFile) { return file; } if (hasValidPath) { return { name: fileNameValue || pathSolution.split('/').pop() || 'File', uploadPath: pathSolution, url: pathSolution, path: pathSolution }; } return null; }; const fileFromForm = getFileFromFormValues(); if (JSON.stringify(currentFile) !== JSON.stringify(fileFromForm)) { setCurrentFile(fileFromForm); } }, [fileUpload, file, pathSolution, solutionType, deleteCounter, fileNameValue, fieldKey]); const renderSolutionContent = () => { if (solutionType === 'text') { return (