import React, { useState } from 'react'; import { InspectionSection, InspectionCommonSection, InspectionMKDElement, Building } from '../../types'; import { FLOOR_ELEMENT_NAMES, ENTRANCE_ELEMENT_NAMES, COMMON_SECTION_ELEMENTS, buildElementsFromNames } from '../../inspectionElementTemplates'; import { ChevronDown, ChevronUp, Plus, X, Trash2, Camera, Image as ImageIcon, Gauge, DoorClosed, Building2, Save, Layers, Paperclip, ListPlus } from 'lucide-react'; interface DynamicInspectionSectionsProps { sections: InspectionSection[]; commonSections: InspectionCommonSection[]; building: Building; onUpdateSections: (sections: InspectionSection[]) => void; onUpdateCommonSections: (commonSections: InspectionCommonSection[]) => void; onSaveToBuilding?: (entranceData: any, floorData: any) => void; // Сохранение данных в building } export const DynamicInspectionSections: React.FC = ({ sections, commonSections, building, onUpdateSections, onUpdateCommonSections, onSaveToBuilding }) => { const [activeTab, setActiveTab] = useState(sections.length > 0 ? sections[0].id : null); const [editingElement, setEditingElement] = useState<{ sectionId: string; elementId: string | null } | null>(null); const [editingEntranceTech, setEditingEntranceTech] = useState(null); // Редактирование тех. характеристик подъезда // Группируем секции по типу const entranceSections = sections.filter(s => s.type === 'entrance'); const floorSections = sections.filter(s => s.type === 'floor'); const liftSections = sections.filter(s => s.type === 'lift'); const handleAddElement = (sectionId: string, isCommon: boolean = false) => { const newElement: InspectionMKDElement = { id: `element-${Date.now()}`, name: '', generalStatus: 'NOT_SELECTED', electroStatus: 'NOT_SELECTED', weldingStatus: 'NOT_SELECTED', repairType: '', mainPhoto: undefined, additionalPhotos: [] }; if (isCommon) { const updated = commonSections.map(s => s.id === sectionId ? { ...s, elements: [...s.elements, newElement] } : s ); onUpdateCommonSections(updated); } else { const updated = sections.map(s => s.id === sectionId ? { ...s, elements: [...s.elements, newElement] } : s ); onUpdateSections(updated); // Сохраняем элементы этажа в building, если это секция этажа const section = sections.find(s => s.id === sectionId); if (section && section.type === 'floor') { const match = sectionId.match(/entrance-(\d+)-floor-(\d+)/); if (match && onSaveToBuilding) { const entranceNum = parseInt(match[1]); const floorNum = parseInt(match[2]); const updatedSection = updated.find(s => s.id === sectionId); if (updatedSection) { onSaveToBuilding(null, { entranceNumber: entranceNum, floorNumber: floorNum, elements: updatedSection.elements }); } } } } setEditingElement({ sectionId, elementId: newElement.id }); }; const handleAddFromTemplate = (sectionId: string, isCommon: boolean = false) => { const existingNames = new Set( isCommon ? (commonSections.find(s => s.id === sectionId)?.elements || []).map(e => e.name) : (sections.find(s => s.id === sectionId)?.elements || []).map(e => e.name) ); let namesToAdd: string[] = []; let prefix = sectionId; if (isCommon) { const section = commonSections.find(s => s.id === sectionId); if (section) { const templateNames = COMMON_SECTION_ELEMENTS[section.title]; if (templateNames) namesToAdd = templateNames.filter(n => !existingNames.has(n)); } } else { const section = sections.find(s => s.id === sectionId); if (section) { if (section.type === 'floor') { const match = sectionId.match(/entrance-(\d+)-floor-(\d+)/); if (match) { namesToAdd = FLOOR_ELEMENT_NAMES.filter(n => !existingNames.has(n)); prefix = `entrance-${match[1]}-floor-${match[2]}`; } } else if (section.type === 'entrance') { namesToAdd = ENTRANCE_ELEMENT_NAMES.filter(n => !existingNames.has(n)); prefix = sectionId; } } } if (namesToAdd.length === 0) return; const newElements = buildElementsFromNames(namesToAdd, prefix); if (isCommon) { const updated = commonSections.map(s => s.id === sectionId ? { ...s, elements: [...s.elements, ...newElements] } : s ); onUpdateCommonSections(updated); } else { const updated = sections.map(s => s.id === sectionId ? { ...s, elements: [...s.elements, ...newElements] } : s ); onUpdateSections(updated); const section = sections.find(s => s.id === sectionId); if (section?.type === 'floor') { const match = sectionId.match(/entrance-(\d+)-floor-(\d+)/); if (match && onSaveToBuilding) { const entranceNum = parseInt(match[1]); const floorNum = parseInt(match[2]); const updatedSection = updated.find(s => s.id === sectionId); if (updatedSection) { onSaveToBuilding(null, { entranceNumber: entranceNum, floorNumber: floorNum, elements: updatedSection.elements }); } } } } }; const handleUpdateElement = (sectionId: string, elementId: string, updates: Partial, isCommon: boolean = false) => { if (isCommon) { const updated = commonSections.map(s => s.id === sectionId ? { ...s, elements: s.elements.map(el => el.id === elementId ? { ...el, ...updates } : el ) } : s ); onUpdateCommonSections(updated); } else { const updated = sections.map(s => s.id === sectionId ? { ...s, elements: s.elements.map(el => el.id === elementId ? { ...el, ...updates } : el ) } : s ); onUpdateSections(updated); // Сохраняем элементы этажа в building, если это секция этажа const section = sections.find(s => s.id === sectionId); if (section && section.type === 'floor') { // Извлекаем номер подъезда и этажа из sectionId (формат: entrance-{num}-floor-{floor}) const match = sectionId.match(/entrance-(\d+)-floor-(\d+)/); if (match && onSaveToBuilding) { const entranceNum = parseInt(match[1]); const floorNum = parseInt(match[2]); const updatedSection = updated.find(s => s.id === sectionId); if (updatedSection) { onSaveToBuilding(null, { entranceNumber: entranceNum, floorNumber: floorNum, elements: updatedSection.elements }); } } } } }; const handleDeleteElement = (sectionId: string, elementId: string, isCommon: boolean = false) => { if (isCommon) { const updated = commonSections.map(s => s.id === sectionId ? { ...s, elements: s.elements.filter(el => el.id !== elementId) } : s ); onUpdateCommonSections(updated); } else { const updated = sections.map(s => s.id === sectionId ? { ...s, elements: s.elements.filter(el => el.id !== elementId) } : s ); onUpdateSections(updated); // Сохраняем элементы этажа в building, если это секция этажа const section = sections.find(s => s.id === sectionId); if (section && section.type === 'floor') { const match = sectionId.match(/entrance-(\d+)-floor-(\d+)/); if (match && onSaveToBuilding) { const entranceNum = parseInt(match[1]); const floorNum = parseInt(match[2]); const updatedSection = updated.find(s => s.id === sectionId); if (updatedSection) { onSaveToBuilding(null, { entranceNumber: entranceNum, floorNumber: floorNum, elements: updatedSection.elements }); } } } } }; const handleToggleSection = (sectionId: string, isCommon: boolean = false) => { if (isCommon) { const updated = commonSections.map(s => s.id === sectionId ? { ...s, isCollapsed: !s.isCollapsed } : s ); onUpdateCommonSections(updated); } else { const updated = sections.map(s => s.id === sectionId ? { ...s, isCollapsed: !s.isCollapsed } : s ); onUpdateSections(updated); } }; const renderElement = (element: InspectionMKDElement, sectionId: string, isCommon: boolean = false) => { const isEditing = editingElement?.sectionId === sectionId && editingElement?.elementId === element.id; return (
{isEditing ? ( handleUpdateElement(sectionId, element.id, { name: e.target.value }, isCommon)} placeholder="Название элемента МКД" className="flex-1 px-3 py-2 bg-white border border-slate-200 rounded-xl text-sm font-bold focus:ring-2 focus:ring-primary-500 outline-none" autoFocus /> ) : (

{element.name || 'Новый элемент'}

)}