import { useState, useEffect } from 'react' import { Button, Form, Input, Popconfirm, Timeline } from 'antd' import moment from 'moment' import { CheckSquareOutlined, EditOutlined, SaveOutlined, PlusOutlined, CloseCircleOutlined, DeleteOutlined } from '@ant-design/icons' import { View } from './View' import LoaderPortal from '../../components/LoaderPortal' import { MeasureService } from '../../services/api' import '../../styles/index.css' import '../../styles/measure.css' import { invokeWebApiWrapperAsync } from '../../components/factory' const dateFormat = 'YYYY.MM.DD HH:mm' export const formatDate = (date) => date ? moment.utc(date).local().format(dateFormat) : 'Нет данных' export const MeasureTable = ({idWell, group, updateMeasuresFunc, additionalButtons}) => { const [showLoader, setShowLoader] = useState(false) const [displayedValues, setDisplayedValues] = useState({}) const [editingColumns, setEditingColumns] = useState(group.columns) const [isTableEditing, setIsTableEditing] = useState(false) const [editingActionName, setEditingActionName] = useState('') const [data, setData] = useState([]) const [measuresForm] = Form.useForm() const createEditingColumns = (cols, renderDelegate) => cols.map(col => ({ render: renderDelegate, ...col })) useEffect(() => { const data = [group.defaultValue].concat(group.values ?? []) setData(data) setDisplayedValues(data.at(-1)) }, [group.defaultValue, group.values]) useEffect(() => { const switchableColumns = createEditingColumns( group.columns, isTableEditing ? () => : null ) if(editingActionName === 'edit') measuresForm.setFieldsValue(displayedValues?.data) else if(editingActionName === 'add') measuresForm.resetFields() setEditingColumns(switchableColumns) }, [isTableEditing, group.columns, editingActionName, displayedValues?.data, measuresForm]) const markMeasuresAsDeleted = async () => await invokeWebApiWrapperAsync( async () => { await MeasureService.markAsDelete(idWell, displayedValues.id) updateMeasuresFunc() }, setShowLoader, `Не удалось удалить запись ${displayedValues.id} для скважины "${idWell}"` ) const isDataDefault = () => !!displayedValues?.isDefaultData const editTable = (action) => { setEditingActionName(action) setIsTableEditing(true) } const handleSubmitMeasuresForm = async (formData) => await invokeWebApiWrapperAsync( async () => { measuresForm.validateFields() const measureParams = { idWell: idWell, idCategory: group.idCategory, timestamp: new Date().toISOString(), data: formData } if(editingActionName === 'add') { await MeasureService.insert(idWell, measureParams) } else if (editingActionName === 'edit') { measureParams.id = displayedValues.id measureParams.timestamp = displayedValues.timestamp await MeasureService.update(idWell, measureParams) } setIsTableEditing(false) updateMeasuresFunc() }, setShowLoader, `Не удалось добавить/изменить запись для скаважины "${idWell}"` ) return ( <>