import React, { useEffect } from 'react'; import { Form, Input, Button, Switch, Radio, Upload, Typography } from 'antd'; import { DeleteOutlined, UploadOutlined } from '@ant-design/icons'; import { uploadFile, getFolderFromFileType } from '../../../../api/file-uploads'; import { NotifAlert, NotifOk } from '../../../../components/Global/ToastNotif'; const { Text } = Typography; const SolutionField = ({ fieldId, index, solutionStatus, isReadOnly, fileList, onRemove, onSolutionTypeChange, onSolutionStatusChange, onFileUpload, currentSolutionData, onFileView, errorCodeForm }) => { useEffect(() => { if (currentSolutionData && errorCodeForm) { if (currentSolutionData.solution_name) { errorCodeForm.setFieldValue(`solution_name_${fieldId}`, currentSolutionData.solution_name); } if (currentSolutionData.type_solution === 'text' && currentSolutionData.text_solution) { errorCodeForm.setFieldValue(`text_solution_${fieldId}`, currentSolutionData.text_solution); } if (currentSolutionData.type_solution) { const formValue = currentSolutionData.type_solution === 'image' || currentSolutionData.type_solution === 'pdf' ? 'file' : currentSolutionData.type_solution; errorCodeForm.setFieldValue(`solution_type_${fieldId}`, formValue); } if (currentSolutionData.is_active !== undefined) { errorCodeForm.setFieldValue(`solution_status_${fieldId}`, currentSolutionData.is_active); } } }, [currentSolutionData, fieldId, errorCodeForm]); const handleBeforeUpload = async (file) => { const isAllowedType = ['application/pdf', 'image/jpeg', 'image/png', 'image/gif'].includes(file.type); if (!isAllowedType) { NotifAlert({ icon: 'error', title: 'Error', message: `${file.name} bukan file PDF atau gambar yang diizinkan.` }); return Upload.LIST_IGNORE; } try { // Upload file immediately to get path const fileExtension = file.name.split('.').pop().toLowerCase(); const isImage = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp'].includes(fileExtension); const fileType = isImage ? 'image' : 'pdf'; const folder = getFolderFromFileType(fileType); const uploadResponse = await uploadFile(file, folder); const actualPath = uploadResponse.data?.path_solution || ''; if (actualPath) { file.uploadPath = actualPath; file.solution_name = file.name; file.solutionId = fieldId; file.type_solution = fileType; onFileUpload(file); NotifOk({ icon: 'success', title: 'Berhasil', message: `${file.name} berhasil diupload!` }); } else { NotifAlert({ icon: 'error', title: 'Gagal', message: `Gagal mengupload ${file.name}` }); } } catch (error) { console.error('Error uploading file:', error); NotifAlert({ icon: 'error', title: 'Error', message: `Gagal mengupload ${file.name}. Silakan coba lagi.` }); } return false; }; return (