import { useRecoilState, useRecoilValue, useResetRecoilState } from 'recoil' import { useContext, useEffect, useState } from 'react' import { GlobalDataContext } from '@/app/GlobalDataProvider' import { useMasterController } from '@/hooks/common/useMasterController' import { useCommonCode } from '@/hooks/common/useCommonCode' import { selectedModuleState, moduleSelectionInitParamsState, moduleSelectionDataState } from '@/store/selectedModuleOptions' import { isObjectNotEmpty } from '@/util/common-utils' import { canvasState } from '@/store/canvasAtom' import { POLYGON_TYPE } from '@/common/common' import { moduleStatisticsState } from '@/store/circuitTrestleAtom' import { useModuleBasicSetting } from '@/hooks/module/useModuleBasicSetting' export function useModuleSelection(props) { const canvas = useRecoilValue(canvasState) const { managementState, setManagementState, managementStateLoaded } = useContext(GlobalDataContext) const [roughnessCodes, setRoughnessCodes] = useState([]) //면조도 목록 const [windSpeedCodes, setWindSpeedCodes] = useState([]) //기준풍속 목록 const [moduleList, setModuleList] = useState([{}]) //모듈 목록 const [selectedSurfaceType, setSelectedSurfaceType] = useState({}) //선택된 면조도 const [installHeight, setInstallHeight] = useState(managementState?.installHeight) //설치 높이 const [standardWindSpeed, setStandardWindSpeed] = useState() //기준풍속 const [verticalSnowCover, setVerticalSnowCover] = useState(managementState?.verticalSnowCover) //수직적설량 const [selectedModules, setSelectedModules] = useRecoilState(selectedModuleState) //선택된 모듈 const [moduleSelectionInitParams, setModuleSelectionInitParams] = useRecoilState(moduleSelectionInitParamsState) //모듈 기본 데이터 ex) 면조도, 높이등등 const { getModuleTypeItemList } = useMasterController() const { findCommonCode } = useCommonCode() const resetStatisticsData = useResetRecoilState(moduleStatisticsState) const { restoreModuleInstArea } = useModuleBasicSetting() const bindInitData = () => { console.log('bindInitData', managementState) setInstallHeight(managementState?.installHeight) setStandardWindSpeed(managementState?.standardWindSpeedId) setVerticalSnowCover(managementState?.verticalSnowCover) setSelectedSurfaceType(managementState?.surfaceType) } const [moduleSelectionData, setModuleSelectionData] = useRecoilState(moduleSelectionDataState) //다음으로 넘어가는 최종 데이터 //탭별 파라메터 초기화 useEffect(() => { bindInitData() const initParams = { illuminationTp: managementState?.surfaceTypeValue, //면조도 illuminationTpNm: managementState?.surfaceType, //면조도명 instHt: managementState?.installHeight, //설치높이 stdWindSpeed: managementState?.standardWindSpeedId, //기준풍속 stdSnowLd: managementState?.verticalSnowCover, //기준적설량 saleStoreNorthFlg: managementState?.saleStoreNorthFlg, //북쪽 설치 여부 } if (selectedModules) { initParams.moduleTpCd = selectedModules.itemTp initParams.moduleItemId = selectedModules.itemId } setModuleSelectionInitParams(initParams) }, [managementState]) useEffect(() => { // 113700 면조도 const roughnessCodeList = findCommonCode('113700') roughnessCodeList.forEach((obj) => { obj.name = obj.clCodeNm obj.id = obj.clCode }) setRoughnessCodes(roughnessCodeList) // 202000 풍속 const windCodeList = findCommonCode('202000') windCodeList.forEach((obj) => { obj.name = obj.clCodeNm obj.id = obj.clCode }) setWindSpeedCodes(windCodeList) //지붕재 선택 const roofsIds = props.addedRoofs.filter((obj) => obj.roofMatlCd).map((obj) => obj.roofMatlCd) if (roofsIds.length === 0) { return } //새로고침시 데이터 날아가는거 방지 if (managementState === null) { setManagementState(managementStateLoaded) } else { bindInitData() } getModuleData(roofsIds) //모듈설치면 초기화 // restoreModuleInstArea() resetStatisticsData() }, []) const getModuleData = async (roofsIds) => { const list = await getModuleTypeItemList(roofsIds) if (list.data.length > 0) { //selectbox에 이름을 넣는다 list.data.forEach((item) => { item.name = item.itemNm }) //셀렉트박스 데이터 초기화 setModuleList(list.data) } } //데이터가 있으면 모듈 자동 선택 //1번 모듈 리스트 조회 useEffect(() => { //모듈리스트의 데이터가 변경 되면 모듈 선택으로 이벤트 if (moduleList.length > 0 && isObjectNotEmpty(moduleSelectionData.module)) { handleChangeModule(moduleSelectionData.module) } }, [moduleList]) const handleChangeModule = (option) => { //선택된 모듈 setSelectedModules(option) //선택값 저장 //init 데이터에 선택된 모듈 추가 setModuleSelectionInitParams({ ...moduleSelectionInitParams, moduleTpCd: option.itemTp, moduleItemId: option.itemId, }) } const handleChangeSurfaceType = (option) => { setModuleSelectionInitParams({ ...moduleSelectionInitParams, illuminationTp: option.clCode, illuminationTpNm: option.clCodeNm, }) setManagementState({ ...managementState, surfaceType: option.clCodeNm, surfaceTypeValue: option.clCode, }) } const handleChangeWindSpeed = (option) => { setModuleSelectionInitParams({ ...moduleSelectionInitParams, stdWindSpeed: option.clCode, }) setManagementState({ ...managementState, standardWindSpeedId: option.clCode, }) } const handleChangeInstallHeight = (option) => { setInstallHeight(option) setModuleSelectionInitParams({ ...moduleSelectionInitParams, instHt: option, }) setManagementState({ ...managementState, installHeight: option, }) } const handleChangeVerticalSnowCover = (option) => { setVerticalSnowCover(option) setModuleSelectionInitParams({ ...moduleSelectionInitParams, stdSnowLd: option, }) setManagementState({ ...managementState, verticalSnowCover: option, }) } //TODO: 설치높이, 기준적설량 debounce 적용해서 추가해야됨 // useEffect(() => { // getConstructionListData(constructionListParams) // }, [constructionListParams]) // const getConstructionListData = async (params) => { // if (params.trestleMkrCd && params.constMthdCd && params.roofBaseCd) { // const optionsList = await getConstructionList(params) // console.log('optionsList', optionsList) // setConstructionList(optionsList.data) // } // } //state 배열에 데이터 추가 함수 // const addRoofTabParams = (key, value, excludeArray = []) => { // const index = roofTabParams.findIndex((obj) => obj.roofTab === roofTab) // if (index !== -1) { // roofTabParams[index][key] = value // if (excludeArray.length > 0) { // excludeArray.forEach((exclude) => { // roofTabParams[index][exclude] = '' // }) // } // setRoofTabParams((prev) => [...prev.slice(0, index), roofTabParams[index], ...prev.slice(index + 1)]) // } // } return { moduleSelectionInitParams, selectedModules, roughnessCodes, windSpeedCodes, managementState, setManagementState, moduleList, setSelectedModules, selectedSurfaceType, setSelectedSurfaceType, installHeight, setInstallHeight, standardWindSpeed, setStandardWindSpeed, verticalSnowCover, setVerticalSnowCover, handleChangeModule, handleChangeSurfaceType, handleChangeWindSpeed, handleChangeInstallHeight, handleChangeVerticalSnowCover, } }