import { use, useContext, useEffect, useReducer, useState } from 'react' import { useCommonCode } from '../common/useCommonCode' import { useMasterController } from '../common/useMasterController' import { selectedModuleState } from '@/store/selectedModuleOptions' import { useRecoilValue } from 'recoil' import { GlobalDataContext } from '@/app/GlobalDataProvider' const RAFT_BASE_CODE = '203800' const trestleReducer = (state, action) => { console.log('🚀 ~ trestleReducer ~ state:', state) console.log('🚀 ~ trestleReducer ~ action:', action) switch (action.type) { 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': console.log('SET_INITIALIZE') return { moduleTpCd: action.roof.moduleTpCd ?? '', roofMatlCd: action.roof.roofMatlCd ?? '', raftBaseCd: action.roof.raftBaseCd ?? null, trestleMkrCd: action.roof.trestleMkrCd ?? null, constMthdCd: action.roof.constMthdCd ?? null, constTp: action.roof.constTp ?? null, roofBaseCd: action.roof.roofBaseCd ?? null, workingWidth: action.roof.workingWidth ?? 0, lengthBase: action.roof.length ?? 0, illuminationTp: action.roof.illuminationTp ?? null, instHt: action.roof.instHt ?? null, stdWindSpeed: action.roof.stdWindSpeed ?? null, stdSnowLd: action.roof.stdSnowLd ?? null, inclCd: action.roof.inclCd ?? 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 [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) useEffect(() => { const raftCodeList = findCommonCode(RAFT_BASE_CODE) setRaftBaseList(raftCodeList) setTrestleList([]) setConstMthdList([]) setRoofBaseList([]) setConstructionList([]) setEavesMargin(selectedRoof?.eavesMargin ?? 0) setRidgeMargin(selectedRoof?.ridgeMargin ?? 0) setKerabaMargin(selectedRoof?.kerabaMargin ?? 0) setCvrYn(selectedRoof?.cvrYn ?? 'N') setCvrChecked(selectedRoof?.cvrChecked ?? false) setSnowGdPossYn(selectedRoof?.snowGdPossYn ?? 'N') setSnowGdChecked(selectedRoof?.snowGdChecked ?? false) }, [selectedRoof]) useEffect(() => { if (trestleState) { console.log('🚀 ~ useEffect ~ trestleState:', trestleState) handleSetTrestleList() if (!trestleState.trestleMkrCd) { setConstMthdList([]) setRoofBaseList([]) setConstructionList([]) setTrestleDetail(null) return } handleSetConstMthdList() if (!trestleState.constMthdCd) { setRoofBaseList([]) setConstructionList([]) setTrestleDetail(null) return } handleSetRoofBaseList() if (!trestleState.roofBaseCd) { setConstructionList([]) setTrestleDetail(null) return } handleSetConstructionList() if (!trestleState.constTp) { setTrestleDetail(null) return } if (!trestleState.eavesMargin) { handleSetTrestleDetailData() } } }, [trestleState]) const handleSetTrestleList = () => { getTrestleList({ moduleTpCd: trestleState.moduleTpCd ?? '', roofMatlCd: trestleState.roofMatlCd ?? '', raftBaseCd: trestleState.raftBaseCd ?? '', }).then((res) => { if (res?.data) setTrestleList(res.data) }) } const handleSetConstMthdList = () => { getTrestleList({ moduleTpCd: trestleState.moduleTpCd ?? '', roofMatlCd: trestleState.roofMatlCd ?? '', raftBaseCd: trestleState.raftBaseCd ?? '', trestleMkrCd: trestleState.trestleMkrCd ?? '', }).then((res) => { if (res?.data) setConstMthdList(res.data) }) } const handleSetRoofBaseList = () => { getTrestleList({ moduleTpCd: trestleState.moduleTpCd ?? '', roofMatlCd: trestleState.roofMatlCd ?? '', raftBaseCd: trestleState.raftBaseCd ?? '', trestleMkrCd: trestleState.trestleMkrCd ?? '', constMthdCd: trestleState.constMthdCd ?? '', }).then((res) => { if (res?.data) setRoofBaseList(res.data) }) } const handleSetConstructionList = () => { 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: trestleState.roofPitch ?? '', }).then((res) => { if (res?.data) setConstructionList(res.data) }) } const handleSetTrestleDetailData = () => { 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.workingWidth ?? '', }, ]).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, // }, // }) } }) } return { trestleState, trestleDetail, dispatch, raftBaseList, trestleList, constMthdList, roofBaseList, constructionList, handleSetTrestleList, handleSetConstMthdList, handleSetRoofBaseList, handleSetConstructionList, handleSetTrestleDetailData, eavesMargin, ridgeMargin, kerabaMargin, setEavesMargin, setRidgeMargin, setKerabaMargin, cvrYn, cvrChecked, snowGdPossYn, snowGdChecked, setCvrYn, setCvrChecked, setSnowGdPossYn, setSnowGdChecked, } }