import { use, useContext, useEffect, useReducer, useState } from 'react' import { useCommonCode } from '../common/useCommonCode' import { useMasterController } from '../common/useMasterController' import { selectedModuleState } from '@/store/selectedModuleOptions' import { useRecoilState, useRecoilValue } from 'recoil' import { GlobalDataContext } from '@/app/GlobalDataProvider' import { popSpinnerState } from '@/store/popupAtom' const RAFT_BASE_CODE = '203800' const trestleReducer = (state, action) => { switch (action.type) { case 'SET_LENGTH': case 'SET_RAFT_BASE': case 'SET_TRESTLE_MAKER': case 'SET_CONST_MTHD': case 'SET_ROOF_BASE': case 'SET_CONSTRUCTION': case 'SET_TRESTLE_DETAIL': return { ...action.roof, } case 'SET_INITIALIZE': return { moduleTpCd: action.roof.module?.itemTp ?? '', roofMatlCd: action.roof?.roofMatlCd ?? '', hajebichi: action.roof?.hajebichi ?? 0, raftBaseCd: action.roof?.raft ?? null, trestleMkrCd: action.roof.trestle?.trestleMkrCd ?? null, constMthdCd: action.roof.trestle?.constMthdCd ?? null, constTp: action.roof.construction?.constTp ?? null, roofBaseCd: action.roof.trestle?.roofBaseCd ?? null, workingWidth: action.roof.workingWidth ?? 0, lengthBase: action.roof?.length ?? 0, illuminationTp: action.roof.common?.illuminationTp ?? null, instHt: action.roof.common?.instHt ?? null, stdWindSpeed: action.roof.common?.stdWindSpeed ?? null, stdSnowLd: action.roof.common?.stdSnowLd ?? null, inclCd: action.roof?.pitch ?? null, roofPitch: action.roof?.roofPchBase ?? 0, eavesMargin: action.roof?.eavesMargin ?? null, ridgeMargin: action.roof?.ridgeMargin ?? null, kerabaMargin: action.roof?.kerabaMargin ?? null, } default: return state } } export function useModuleTrestle(props) { const { selectedRoof } = props const { findCommonCode } = useCommonCode() const [raftBaseList, setRaftBaseList] = useState([]) const [trestleList, setTrestleList] = useState([]) const [constMthdList, setConstMthdList] = useState([]) const [roofBaseList, setRoofBaseList] = useState([]) const [constructionList, setConstructionList] = useState([]) const { getTrestleList, getConstructionList, getTrestleDetailList } = useMasterController() const [lengthBase, setLengthBase] = useState(0) const [hajebichi, setHajebichi] = useState(0) const [cvrYn, setCvrYn] = useState('N') const [cvrChecked, setCvrChecked] = useState(false) const [snowGdPossYn, setSnowGdPossYn] = useState('N') const [snowGdChecked, setSnowGdChecked] = useState(false) const [eavesMargin, setEavesMargin] = useState(0) const [ridgeMargin, setRidgeMargin] = useState(0) const [kerabaMargin, setKerabaMargin] = useState(0) const [trestleState, dispatch] = useReducer(trestleReducer, null) const [trestleDetail, setTrestleDetail] = useState(null) const [popSpinnerStore, setPopSpinnerStore] = useRecoilState(popSpinnerState) useEffect(() => { const raftCodeList = findCommonCode(RAFT_BASE_CODE) setRaftBaseList(raftCodeList) setTrestleList([]) setConstMthdList([]) setRoofBaseList([]) setConstructionList([]) // setEavesMargin(selectedRoof?.addRoof?.eavesMargin ?? 0) // setRidgeMargin(selectedRoof?.addRoof?.ridgeMargin ?? 0) // setKerabaMargin(selectedRoof?.addRoof?.kerabaMargin ?? 0) setHajebichi(selectedRoof?.hajebichi ?? 0) setEavesMargin(selectedRoof?.eavesMargin ?? 0) setRidgeMargin(selectedRoof?.ridgeMargin ?? 0) setKerabaMargin(selectedRoof?.kerabaMargin ?? 0) setLengthBase(Math.round(selectedRoof?.length ?? 0)) setCvrYn(selectedRoof?.construction?.cvrYn ?? 'N') setCvrChecked(selectedRoof?.construction?.cvrChecked ?? false) setSnowGdPossYn(selectedRoof?.construction?.snowGdPossYn ?? 'N') setSnowGdChecked(selectedRoof?.construction?.snowGdChecked ?? false) setTrestleDetail(selectedRoof?.trestleDetail) }, [selectedRoof]) useEffect(() => { if (trestleState) { handleSetTrestleList() if (!trestleState?.trestleMkrCd) { setConstMthdList([]) setRoofBaseList([]) setConstructionList([]) setTrestleDetail(null) setEavesMargin(0) setRidgeMargin(0) setKerabaMargin(0) return } handleSetConstMthdList() if (!trestleState?.constMthdCd) { setRoofBaseList([]) setConstructionList([]) setTrestleDetail(null) setEavesMargin(0) setRidgeMargin(0) setKerabaMargin(0) return } handleSetRoofBaseList() if (!trestleState?.roofBaseCd) { setConstructionList([]) setTrestleDetail(null) setEavesMargin(0) setRidgeMargin(0) setKerabaMargin(0) return } handleSetConstructionList() if (!trestleState?.constTp) { setTrestleDetail(null) setEavesMargin(0) setRidgeMargin(0) setKerabaMargin(0) return } if (!trestleState?.eavesMargin) { handleSetTrestleDetailData() } } }, [trestleState]) const handleSetTrestleList = () => { setPopSpinnerStore(true) getTrestleList({ moduleTpCd: trestleState?.moduleTpCd ?? '', roofMatlCd: trestleState?.roofMatlCd ?? '', raftBaseCd: trestleState?.raftBaseCd ?? '', }) .then((res) => { if (res?.data) setTrestleList(res.data) setPopSpinnerStore(false) }) .catch((e) => { setPopSpinnerStore(false) }) } const handleSetConstMthdList = () => { setPopSpinnerStore(true) getTrestleList({ moduleTpCd: trestleState?.moduleTpCd ?? '', roofMatlCd: trestleState?.roofMatlCd ?? '', raftBaseCd: trestleState?.raftBaseCd ?? '', trestleMkrCd: trestleState?.trestleMkrCd ?? '', }) .then((res) => { if (res?.data) setConstMthdList(res.data) setPopSpinnerStore(false) }) .catch((e) => { setPopSpinnerStore(false) }) } const handleSetRoofBaseList = () => { setPopSpinnerStore(true) getTrestleList({ moduleTpCd: trestleState?.moduleTpCd ?? '', roofMatlCd: trestleState?.roofMatlCd ?? '', raftBaseCd: trestleState?.raftBaseCd ?? '', trestleMkrCd: trestleState?.trestleMkrCd ?? '', constMthdCd: trestleState?.constMthdCd ?? '', }) .then((res) => { if (res?.data) setRoofBaseList(res.data) setPopSpinnerStore(false) }) .catch((e) => { setPopSpinnerStore(false) }) } const handleSetConstructionList = () => { setPopSpinnerStore(true) getConstructionList({ moduleTpCd: trestleState?.moduleTpCd ?? '', roofMatlCd: trestleState?.roofMatlCd ?? '', trestleMkrCd: trestleState?.trestleMkrCd ?? '', constMthdCd: trestleState?.constMthdCd ?? '', roofBaseCd: trestleState?.roofBaseCd ?? '', illuminationTp: trestleState.illuminationTp ?? '', instHt: trestleState.instHt ?? '', stdWindSpeed: trestleState.stdWindSpeed ?? '', stdSnowLd: trestleState.stdSnowLd ?? '', inclCd: trestleState.inclCd ?? '', raftBaseCd: trestleState.raftBaseCd ?? '', roofPitch: Math.round(trestleState.roofPitch) ?? '', }) .then((res) => { if (res?.data) setConstructionList(res.data) setPopSpinnerStore(false) }) .catch((e) => { setPopSpinnerStore(false) }) } const handleSetTrestleDetailData = () => { setPopSpinnerStore(true) getTrestleDetailList([ { moduleTpCd: trestleState.moduleTpCd ?? '', roofMatlCd: trestleState.roofMatlCd ?? '', trestleMkrCd: trestleState.trestleMkrCd ?? '', constMthdCd: trestleState.constMthdCd ?? '', roofBaseCd: trestleState.roofBaseCd ?? '', illuminationTp: trestleState.illuminationTp ?? '', instHt: trestleState.instHt ?? '', stdWindSpeed: trestleState.stdWindSpeed ?? '', stdSnowLd: trestleState.stdSnowLd ?? '', inclCd: trestleState.inclCd ?? '', constTp: trestleState.constTp ?? '', mixMatlNo: trestleState.mixMatlNo ?? '', roofPitch: trestleState.roofPitch ?? '', // workingWidth: trestleState.length ?? '', workingWidth: lengthBase ?? '', }, ]) .then((res) => { if (res.length > 0) { if (!res[0].data) return setEavesMargin(res[0].data.eaveIntvl) setRidgeMargin(res[0].data.ridgeIntvl) setKerabaMargin(res[0].data.kerabaIntvl) setTrestleDetail(res[0].data) // dispatch({ // type: 'SET_TRESTLE_DETAIL', // roof: { // ...trestleState, // eavesMargin: res[0].data.eaveIntvl, // ridgeMargin: res[0].data.ridgeIntvl, // kerabaMargin: res[0].data.kerabaIntvl, // }, // }) } setPopSpinnerStore(false) }) .catch((e) => { setPopSpinnerStore(false) }) } return { trestleState, trestleDetail, dispatch, raftBaseList, trestleList, constMthdList, roofBaseList, constructionList, handleSetTrestleList, handleSetConstMthdList, handleSetRoofBaseList, handleSetConstructionList, handleSetTrestleDetailData, lengthBase, setLengthBase, hajebichi, setHajebichi, cvrYn, cvrChecked, snowGdPossYn, snowGdChecked, eavesMargin, ridgeMargin, kerabaMargin, setEavesMargin, setRidgeMargin, setKerabaMargin, setCvrYn, setCvrChecked, setSnowGdPossYn, setSnowGdChecked, } }