import { GlobalDataContext } from '@/app/GlobalDataProvider' import QSelectBox from '@/components/common/select/QSelectBox' import { useModuleBasicSetting } from '@/hooks/module/useModuleBasicSetting' import { useModuleTrestle } from '@/hooks/module/useModuleTrestle' import { useMessage } from '@/hooks/useMessage' import { currentAngleTypeSelector, pitchTextSelector } from '@/store/canvasAtom' import { roofsState } from '@/store/roofAtom' import { moduleSelectionDataState, selectedModuleState } from '@/store/selectedModuleOptions' import { forwardRef, useContext, useEffect, useImperativeHandle, useRef, useState } from 'react' import { useRecoilState, useRecoilValue } from 'recoil' const Trestle = forwardRef((props, ref) => { const { tabNum, setTabNum, trestleTrigger, roofs, setRoofs, moduleSelectionData, setModuleSelectionData, setRoofsStore } = props const { getMessage } = useMessage() // const [selectedTrestle, setSelectedTrestle] = useState() const currentAngleType = useRecoilValue(currentAngleTypeSelector) const pitchText = useRecoilValue(pitchTextSelector) const [selectedRoof, setSelectedRoof] = useState(null) const { trestleState, trestleDetail, dispatch, raftBaseList, trestleList, constMthdList, roofBaseList, constructionList, eavesMargin, ridgeMargin, kerabaMargin, setEavesMargin, setRidgeMargin, setKerabaMargin, lengthBase, setLengthBase, hajebichi, setHajebichi, cvrYn, cvrChecked, snowGdPossYn, snowGdChecked, setCvrYn, setCvrChecked, setSnowGdPossYn, setSnowGdChecked, } = useModuleTrestle({ selectedRoof, }) const selectedModules = useRecoilValue(selectedModuleState) //선택된 모듈 // const [moduleSelectionData, setModuleSelectionData] = useRecoilState(moduleSelectionDataState) const [selectedRaftBase, setSelectedRaftBase] = useState(null) const [selectedTrestle, setSelectedTrestle] = useState(null) const [selectedConstMthd, setSelectedConstMthd] = useState(null) const [selectedConstruction, setSelectedConstruction] = useState(null) const [selectedRoofBase, setSelectedRoofBase] = useState(null) const { managementState } = useContext(GlobalDataContext) const { restoreModuleInstArea } = useModuleBasicSetting() const [flag, setFlag] = useState(false) const tempModuleSelectionData = useRef(null) useEffect(() => { if (roofs && !selectedRoof) { setSelectedRoof(roofs[0]) } //모듈 설치 영역 복구 restoreModuleInstArea() }, [roofs]) useEffect(() => { if (flag && moduleSelectionData) { if (JSON.stringify(tempModuleSelectionData.current) === JSON.stringify(moduleSelectionData)) { setTabNum(tabNum + 1) } } }, [flag, moduleSelectionData]) useEffect(() => { if (selectedRoof) { if (moduleSelectionData?.roofConstructions?.length >= selectedRoof.index + 1) { const { construction, trestle, trestleDetail } = moduleSelectionData?.roofConstructions[selectedRoof.index] dispatch({ type: 'SET_INITIALIZE', roof: { common: moduleSelectionData.common, module: moduleSelectionData.module, construction, trestle, trestleDetail, ...selectedRoof }, }) } else { dispatch({ type: 'SET_INITIALIZE', roof: { ...selectedRoof, common: moduleSelectionData.common, module: moduleSelectionData.module } }) } } }, [selectedRoof]) useEffect(() => { if (raftBaseList.length > 0) { setSelectedRaftBase(raftBaseList.find((raft) => raft.clCode === trestleState?.raftBaseCd) ?? null) } else { setSelectedRaftBase(null) } }, [raftBaseList]) useEffect(() => { if (trestleList.length > 0) { setSelectedTrestle(trestleList.find((trestle) => trestle.trestleMkrCd === trestleState?.trestleMkrCd) ?? null) } else { setSelectedTrestle(null) } }, [trestleList]) useEffect(() => { if (roofBaseList.length > 0) { setSelectedRoofBase(roofBaseList.find((roofBase) => roofBase.roofBaseCd === trestleState?.roofBaseCd) ?? null) } else { setSelectedRoofBase(null) } }, [roofBaseList]) useEffect(() => { if (constMthdList.length > 0) { setSelectedConstMthd(constMthdList.find((constMthd) => constMthd.constMthdCd === trestleState?.constMthdCd) ?? null) } else { setSelectedConstMthd(null) } }, [constMthdList]) useEffect(() => { if (constructionList.length > 0) { setSelectedConstruction(constructionList.find((construction) => construction.constTp === trestleState?.construction?.constTp) ?? null) } else { setSelectedConstruction(null) } }, [constructionList]) const getConstructionState = (index) => { if (constructionList && constructionList.length > 0) { if (constructionList[index].constPossYn === 'Y') { if (trestleState && trestleState.constTp === constructionList[index].constTp) { return 'blue' } return 'white' } return 'no-click' } return 'no-click' } const onChangeRaftBase = (e) => { setSelectedRaftBase(e) dispatch({ type: 'SET_RAFT_BASE', roof: { moduleTpCd: selectedModules.itemTp ?? '', roofMatlCd: selectedRoof?.roofMatlCd ?? '', raftBaseCd: e.clCode, }, }) } const onChangeTrestleMaker = (e) => { setSelectedTrestle(e) dispatch({ type: 'SET_TRESTLE_MAKER', roof: { moduleTpCd: selectedModules.itemTp ?? '', roofMatlCd: selectedRoof?.roofMatlCd ?? '', raftBaseCd: selectedRaftBase?.clCode, trestleMkrCd: e.trestleMkrCd, }, }) } const onChangeConstMthd = (e) => { setSelectedConstMthd(e) dispatch({ type: 'SET_CONST_MTHD', roof: { moduleTpCd: selectedModules.itemTp ?? '', roofMatlCd: selectedRoof?.roofMatlCd ?? '', raftBaseCd: selectedRaftBase?.clCode, trestleMkrCd: selectedTrestle.trestleMkrCd, constMthdCd: e.constMthdCd, }, }) } const onChangeRoofBase = (e) => { setSelectedRoofBase(e) dispatch({ type: 'SET_ROOF_BASE', roof: { moduleTpCd: selectedModules.itemTp ?? '', roofMatlCd: selectedRoof?.roofMatlCd ?? '', raftBaseCd: selectedRaftBase?.clCode, trestleMkrCd: selectedTrestle.trestleMkrCd, constMthdCd: selectedConstMthd.constMthdCd, roofBaseCd: e.roofBaseCd, illuminationTp: managementState?.surfaceTypeValue ?? '', instHt: managementState?.installHeight ?? '', stdWindSpeed: managementState?.standardWindSpeedId ?? '', stdSnowLd: managementState?.verticalSnowCover ?? '', inclCd: selectedRoof?.pitch ?? 0, roofPitch: Math.round(selectedRoof?.roofPchBase ?? 0), }, }) } const handleConstruction = (index) => { if (constructionList[index]?.constPossYn === 'Y') { dispatch({ type: 'SET_CONSTRUCTION', roof: { moduleTpCd: selectedModules.itemTp ?? '', roofMatlCd: selectedRoof?.roofMatlCd ?? '', raftBaseCd: selectedRaftBase?.clCode, trestleMkrCd: selectedTrestle.trestleMkrCd, constMthdCd: selectedConstMthd.constMthdCd, roofBaseCd: selectedRoofBase.roofBaseCd, illuminationTp: managementState?.surfaceTypeValue ?? '', instHt: managementState?.installHeight ?? '', stdWindSpeed: managementState?.standardWindSpeedId ?? '', stdSnowLd: +managementState?.verticalSnowCover ?? '', inclCd: selectedRoof?.pitch ?? 0, roofPitch: Math.round(selectedRoof?.roofPchBase ?? 0), constTp: constructionList[index].constTp, snowGdPossYn: constructionList[index].snowGdPossYn, cvrYn: constructionList[index].cvrYn, mixMatlNo: selectedModules.mixMatlNo, workingWidth: selectedRoof?.length?.toString() ?? '', }, }) setCvrYn(constructionList[index].cvrYn) setSnowGdPossYn(constructionList[index].snowGdPossYn) setCvrChecked(false) setSnowGdChecked(false) } } const handleChangeRoofMaterial = (index) => { const newAddedRoofs = roofs.map((roof, i) => { if (i === selectedRoof.index) { return { ...selectedRoof, hajebichi, eavesMargin, ridgeMargin, kerabaMargin, roofIndex: selectedRoof.index, trestle: { hajebichi: hajebichi, lengthBase: lengthBase, ...selectedRaftBase, ...selectedTrestle, ...selectedConstMthd, ...selectedRoofBase, }, construction: { ...constructionList.find((data) => data.constTp === trestleState.constTp), cvrYn: cvrYn, snowGdPossYn: snowGdPossYn, cvrChecked: cvrChecked, snowGdChecked: snowGdChecked, }, trestleDetail: trestleDetail, } } return roof }) setRoofs(newAddedRoofs) setSelectedRoof(newAddedRoofs[index]) } const isComplete = async () => { const newAddedRoofs = roofs.map((roof, i) => { if (i === selectedRoof?.index) { return { ...selectedRoof, eavesMargin, ridgeMargin, kerabaMargin, roofIndex: roof.index, trestle: { lengthBase: lengthBase, hajebichi: hajebichi, ...selectedRaftBase, ...selectedTrestle, ...selectedConstMthd, ...selectedRoofBase, }, construction: { ...constructionList.find((data) => data.constTp === trestleState.constTp), cvrYn, snowGdPossYn, cvrChecked, snowGdChecked, setupCover: cvrChecked, setupSnowCover: snowGdChecked, }, trestleDetail: trestleDetail, } } return roof }) let result = true newAddedRoofs.forEach((roof) => { if (!roof?.eavesMargin || !roof?.ridgeMargin || !roof?.kerabaMargin) result = false if (!roof.trestle?.trestleMkrCd) result = false if (!roof.trestle?.constMthdCd) result = false if (!roof.trestle?.roofBaseCd) result = false if (!roof.construction?.constTp) result = false if (roof.lenAuth === 'C') { if (!roof.trestle?.lengthBase) result = false } if (['C', 'R'].includes(roof.raftAuth)) { if (!roof?.raftBaseCd) result = false } if (['C', 'R'].includes(roof.roofPchAuth)) { if (!roof?.roofPchBase) result = false } }) const newRoofs = newAddedRoofs.map((roof) => { const { addRoof, construction, trestle, trestleDetail, roofConstructions, ...rest } = roof return rest }) if (result) { setModuleSelectionData({ ...moduleSelectionData, roofConstructions: newAddedRoofs.map((roof, index) => ({ roofIndex: newRoofs[index].index, trestle: roof.trestle, addRoof: newRoofs[index], construction: roof.construction, trestleDetail: roof.trestleDetail, })), }) setFlag(true) tempModuleSelectionData.current = { ...moduleSelectionData, roofConstructions: newAddedRoofs.map((roof, index) => ({ roofIndex: newRoofs[index].index, trestle: roof.trestle, addRoof: newRoofs[index], construction: roof.construction, trestleDetail: roof.trestleDetail, })), } const updatePromises = [ // new Promise((resolve) => { // resolve() // }), new Promise((resolve) => { setRoofs(newRoofs) resolve() }), new Promise((resolve) => { const roofConstruction = newAddedRoofs.map((roof, index) => ({ roofIndex: newRoofs[index].index, addRoof: newRoofs[index], trestle: { ...roof.trestle, raftBaseCd: roof.raftBaseCd, }, construction: { // ...constructionList.find((construction) => newAddedRoofs[index].construction.constTp === construction.constTp), ...roof.construction, roofIndex: roof.index, setupCover: cvrChecked, setupSnowCover: snowGdChecked, selectedIndex: roof.index, }, trestleDetail: roof.trestleDetail, })) trestleTrigger({ roofConstruction, }) setRoofsStore(roofConstruction) resolve() }), ] await Promise.all(updatePromises) return true } return false } useImperativeHandle(ref, () => ({ isComplete, })) return (