148 lines
4.4 KiB
TypeScript
148 lines
4.4 KiB
TypeScript
import { ElementDefinition } from '@/entity/element/model/interface'
|
||
import { CellTarget } from '@/type/template'
|
||
import { Building2 } from 'lucide-react'
|
||
import {
|
||
VerificationConditionsConfig,
|
||
VerificationConditionsValue,
|
||
} from './types'
|
||
import { VerificationConditionsEditor } from './VerificationConditionsEditor'
|
||
import { VerificationConditionsPreview } from './VerificationConditionsPreview'
|
||
import { VerificationConditionsRender } from './VerificationConditionsRender'
|
||
|
||
// Форматирование даты как в функции ДАТАПРОТОКОЛА
|
||
const formatVerificationDate = (dateStr: string): string => {
|
||
try {
|
||
const months = [
|
||
'',
|
||
'января',
|
||
'февраля',
|
||
'марта',
|
||
'апреля',
|
||
'мая',
|
||
'июня',
|
||
'июля',
|
||
'августа',
|
||
'сентября',
|
||
'октября',
|
||
'ноября',
|
||
'декабря',
|
||
]
|
||
|
||
const dateObj = new Date(dateStr)
|
||
if (isNaN(dateObj.getTime())) {
|
||
return 'Неверный формат даты'
|
||
}
|
||
|
||
const day = dateObj.getDate().toString().padStart(2, '0')
|
||
const month = months[dateObj.getMonth() + 1]
|
||
const year = dateObj.getFullYear()
|
||
|
||
return `${day} ${month} ${year}`
|
||
} catch (e) {
|
||
return 'Неверный формат даты'
|
||
}
|
||
}
|
||
|
||
export const verificationConditionsElementDefinition: ElementDefinition<
|
||
VerificationConditionsConfig & { targetCells?: CellTarget[] },
|
||
VerificationConditionsValue
|
||
> = {
|
||
type: 'verification_conditions',
|
||
label: 'Условия поверки',
|
||
icon: <Building2 className="h-4 w-4" />,
|
||
version: 1,
|
||
|
||
defaultConfig: {
|
||
selectedLaboratoryId: undefined,
|
||
selectedDate: undefined,
|
||
conditionMappings: [],
|
||
showUnits: true,
|
||
targetCells: [],
|
||
id: '',
|
||
},
|
||
|
||
// Компоненты
|
||
Editor: VerificationConditionsEditor,
|
||
Preview: VerificationConditionsPreview,
|
||
Render: VerificationConditionsRender,
|
||
|
||
// Получение начальных значений из конфигурации с учетом localStorage
|
||
getInitialValue: config => {
|
||
let initialDate =
|
||
config.selectedDate || new Date().toISOString().split('T')[0]
|
||
|
||
// Попытка загрузить дату из localStorage
|
||
if (config.id) {
|
||
try {
|
||
const key = `verification_conditions_date_${config.id}`
|
||
const savedDate = localStorage.getItem(key)
|
||
if (savedDate) {
|
||
initialDate = savedDate
|
||
}
|
||
} catch (error) {
|
||
console.warn('Failed to get date from localStorage:', error)
|
||
}
|
||
}
|
||
|
||
return {
|
||
date: initialDate,
|
||
conditions: {},
|
||
}
|
||
},
|
||
|
||
// Получение ячеек для записи данных на основе маппинга условий
|
||
mapToCells: (config): CellTarget[] => {
|
||
// Возвращаем только те targetCells, которые замаплены к условиям
|
||
if (!config.conditionMappings || !config.targetCells) {
|
||
return []
|
||
}
|
||
|
||
return config.conditionMappings
|
||
.map(mapping => config.targetCells?.[mapping.cellIndex])
|
||
.filter(Boolean) as CellTarget[]
|
||
},
|
||
|
||
// Получение значения для записи в ячейки на основе реального маппинга
|
||
mapToCellValues: (
|
||
config,
|
||
value: VerificationConditionsValue
|
||
): Array<{ target: CellTarget; value: any }> => {
|
||
if (!config.conditionMappings || !config.targetCells) {
|
||
return []
|
||
}
|
||
|
||
return config.conditionMappings
|
||
.map(mapping => {
|
||
const targetCell = config.targetCells?.[mapping.cellIndex]
|
||
if (!targetCell) return null
|
||
|
||
// Обработка даты поверки
|
||
if (mapping.conditionKey === 'verification_date') {
|
||
if (value.date) {
|
||
return {
|
||
target: targetCell,
|
||
value: formatVerificationDate(value.date),
|
||
}
|
||
}
|
||
return null
|
||
}
|
||
|
||
// Обработка остальных условий
|
||
const conditionValue = value.conditions?.[mapping.conditionKey]
|
||
if (
|
||
conditionValue !== undefined &&
|
||
conditionValue !== null &&
|
||
String(conditionValue).trim() !== ''
|
||
) {
|
||
return {
|
||
target: targetCell,
|
||
value: conditionValue,
|
||
}
|
||
}
|
||
|
||
return null
|
||
})
|
||
.filter(Boolean) as Array<{ target: CellTarget; value: any }>
|
||
},
|
||
}
|