Files
frontend/apps/wr/components/ui/modal/chkgdModal/chkgdModal.tsx
T

216 lines
6.5 KiB
TypeScript

import { useWrStore } from "@/app/store";
import { ScrollArea, Title, UiTable } from "@common/business_components/ui";
import { Button } from "@common/components/ui";
import { useEffect, useState } from "react";
import { GuideDetailsTable } from "../../table/chkgdTable/guideDetailTable/guideDetailTable";
import type { splRuseWaterType } from "@/app/store/prfmncReg/chkgdnReg/rainChkgdn";
import { headMakeprfmnc } from "@/app/ruseFcltMng/prfmncReg/chkgdnReg/TableHead";
export default function ChkgdModal({ type, obj }: { type: string; obj: any }) {
const { rainChkgdn, rainChkDetilUpdate, rainChkGuidUpdate } = useWrStore(
(state) => state
);
const [guidData, setGuidData] = useState<splRuseWaterType[]>();
const rswtnodeList = headMakeprfmnc(
rainChkgdn?.rswtDetailHeadList,
rainChkgdn?.rainHeadNm
);
const { rainChk, ruseHeadList, splHeadList } = rainChkgdn;
const [updateData, setUpdateData] = useState<splRuseWaterType[]>();
useEffect(() => {
const result = obj.guideDetails?.reduce((acc: any, cur: any) => {
const code = cur.utztnUsgCd;
const month = cur.ym?.slice(4); // "MM"
const ruseMonthKey = `ruse${month}`;
const splMonthKey = `spl${month}`;
const ruseValue = cur.ruseFcltUtztnQnt;
const splValue = cur.splyUswtrUtztnQnt;
// 해당 utztnUsgCd 객체가 없으면 새로 생성
let target = acc.find(
(it: { utztnUsgCd: string }) => it?.utztnUsgCd === code
);
if (!target) {
target = { utztnUsgCd: code, ruseTotal: 0, splTotal: 0 };
acc.push(target);
}
// 월별 값 누적
target[ruseMonthKey] = (target[ruseMonthKey] ?? 0) + ruseValue;
target[splMonthKey] = (target[splMonthKey] ?? 0) + splValue;
// total 누적
target.ruseTotal += ruseValue;
target.splTotal += splValue;
return acc;
}, []);
setGuidData(result);
}, [obj]);
const cellClick = (val: number | undefined, headId: string) => {};
const setChGuidData = (data: splRuseWaterType[]) => {
if (data.length > 0) {
setUpdateData(data);
}
};
const guidDataUpdate = () => {
const realGuideDetail = obj.guideDetails;
if (updateData === undefined || updateData.length === 0) return;
realGuideDetail.map((item: any) => {
const findObj = updateData.find(
(ch: any) => ch.utztnUsgCd === item.utztnUsgCd
);
const month = item.ym?.slice(4);
if (month === undefined || month === null) {
console.log("month ", month);
}
if (findObj) {
const ruseMonthKey = `ruse${month}` as keyof splRuseWaterType;
const splMonthKey = `spl${month}` as keyof splRuseWaterType;
if (
findObj[ruseMonthKey] !== undefined &&
findObj[splMonthKey] !== undefined
) {
item.splyUswtrUtztnQnt = findObj[splMonthKey];
item.ruseFcltUtztnQnt = findObj[ruseMonthKey];
return item;
}
}
});
obj.guideDetails = realGuideDetail;
rainChk.map((item: any) => {
if (item.chkgdnPk === obj.chkgdnPk) {
item.guideDetails = realGuideDetail;
}
});
console.log("realGuideDetail", rainChk);
rainChkGuidUpdate(obj);
};
return (
<>
<div>
<div>
<Title
title={
type === "RNB"
? "빗물이용시설"
: "중수도 이용시설" + "지도점검 결과등록"
}
></Title>
</div>
<div className="flex justify-between">
<h3 className="text-2xl font-bold text-gray-900">
{"○" + "" + `(${"11"})`}
</h3>
<Button className="" onClick={guidDataUpdate}>
저장
</Button>
</div>
<ScrollArea className="h-[60vh]">
<GuideDetailsTable
chgRes={guidData}
headList={ruseHeadList}
type={""}
setChGuidData={(data) => {
setChGuidData(data);
}}
/>
<GuideDetailsTable
chgRes={guidData}
headList={splHeadList}
type={""}
setChGuidData={(data) => {
setChGuidData(data);
}}
/>
{type !== "RNB" ? (
<div>
<UiTable
headName={"연간이용수질(1분기)"}
tableData={[]}
headlist={rainChkgdn?.rswtDetailHeadList}
pageSize={0}
total={0}
cellClick={(val, headId) => {
cellClick(val, headId);
}}
onPageChange={function (page: number): void {
throw new Error("Function not implemented.");
}}
>
{rswtnodeList}
</UiTable>
<UiTable
headName={"연간이용수질(2분기)"}
tableData={[]}
headlist={rainChkgdn?.rswtDetailHeadList}
pageSize={0}
total={0}
cellClick={(val, headId) => {
cellClick(val, headId);
}}
onPageChange={function (page: number): void {
throw new Error("Function not implemented.");
}}
>
{rswtnodeList}
</UiTable>
<UiTable
headName={"연간이용수질(3분기)"}
tableData={[]}
headlist={rainChkgdn?.rswtDetailHeadList}
pageSize={0}
total={0}
cellClick={(val, headId) => {
cellClick(val, headId);
}}
onPageChange={function (page: number): void {
throw new Error("Function not implemented.");
}}
>
{rswtnodeList}
</UiTable>
<UiTable
headName={"연간이용수질(4분기)"}
tableData={[]}
headlist={rainChkgdn?.rswtDetailHeadList}
pageSize={0}
total={0}
cellClick={(val, headId) => {
cellClick(val, headId);
}}
onPageChange={function (page: number): void {
throw new Error("Function not implemented.");
}}
>
{rswtnodeList}
</UiTable>
</div>
) : (
<></>
)}
</ScrollArea>
</div>
</>
);
}