// Handlers
import { genCrudHandler } from '~/handlers/_handler'

// Services
import { create, update, remove } from '~/services/supporting-document.service'

export const {
  recId,
  recAction,
  recItem,
  isReadonly,
  isProcessing,
  isFormEntryDialogOpen,
  isRecordConfirmationOpen,
  onResetState,
  handleActionSave,
  handleActionEdit,
  handleActionRemove,
  handleCancelForm,
} = genCrudHandler({
  create,
  update,
  remove,
})

export async function uploadAttachmentCustom(payload: any) {
  const { user } = useUserStore()
  
  const formData = new FormData()
  formData.append('content', payload.file)
  formData.append('entityType_code', payload.entityTypeCode)
  formData.append('type_code', payload.type)
  formData.append('ref_id', payload.refId)
  formData.append('upload_employee_id', user.employee_id)

  const response = payload.id ? await update(payload.id, formData) : await create(formData)
  return response?.body?.data
}
