181 lines
6.0 KiB
TypeScript
181 lines
6.0 KiB
TypeScript
import { useWrStore } from "@/app/store";
|
|
import { excelToPbnPrfData } from "@/app/store/prfmncReg/pbnstPrfmncReg/excel";
|
|
import { pbnPrfRegExcelZod } from "@/app/store/prfmncReg/pbnstPrfmncReg/headList";
|
|
import type { FcPrfType, PrfType } from "@/app/store/prfmncReg/pbnstPrfmncReg/type";
|
|
import { yearOptions } from "@/components/lib/func/commom";
|
|
import { ToastFormUi } from "@/components/ui/toast/toastFromUi";
|
|
import {
|
|
FileUpload,
|
|
FormField,
|
|
FormUi,
|
|
SelectBox,
|
|
Form,
|
|
Title,
|
|
type SelectDataType,
|
|
filePicked,
|
|
} from "@common/business_components/ui";
|
|
import { Button, Input } from "@common/components/ui";
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
import { useEffect, useState } from "react";
|
|
import { useForm } from "react-hook-form";
|
|
import toast from "react-hot-toast";
|
|
import type { z } from "zod";
|
|
|
|
export default function PbnPrfExcelModal({ data ,setIsExcelOPen ,setModalExcelData}: { data: FcPrfType ,setIsExcelOPen:(open:boolean )=>void,setModalExcelData:(excelData :PrfType)=>void }) {
|
|
const { prfmnRegData } = useWrStore((state) => state);
|
|
|
|
const [file, setFile] = useState<File | null>(null);
|
|
const [excelData, setExcelData] = useState<PrfType[]>();
|
|
const form = useForm<z.infer<typeof pbnPrfRegExcelZod>>({
|
|
resolver: zodResolver(pbnPrfRegExcelZod),
|
|
defaultValues: {
|
|
ym: String(new Date().getFullYear()),
|
|
},
|
|
});
|
|
|
|
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
const selectedFile = e.target.files?.[0];
|
|
if (selectedFile) {
|
|
setFile(selectedFile);
|
|
}
|
|
};
|
|
const onSubmit = (values: z.infer<typeof pbnPrfRegExcelZod>) => {
|
|
|
|
if (excelData) {
|
|
// prfmnRegData(1,'41', excelData as unknown as PrfType);
|
|
|
|
setModalExcelData(excelData as unknown as PrfType)
|
|
setIsExcelOPen(false)
|
|
}
|
|
|
|
|
|
};
|
|
useEffect(() => {
|
|
if (data && data.bscFctNm)
|
|
form.setValue("pbnstRuseFcltFk", data.pbnstRuseFcltPk);
|
|
form.setValue("bscFctNm", data.bscFctNm || "");
|
|
form.setValue("pbnstRuseFcltNm", data.ruseUtztnFcltNm);
|
|
}, [data]);
|
|
|
|
|
|
useEffect(() => {
|
|
if (file) {
|
|
const isFile = file
|
|
if (form.getValues("ym").length < 1) {
|
|
toast.error("파일업로드 이전에 등록년도를 작성해주세요 " );
|
|
setFile(null);
|
|
return;
|
|
}
|
|
|
|
if (isFile) {
|
|
filePicked(isFile, 0).then((res) => {
|
|
if (res !== undefined)
|
|
setExcelData(excelToPbnPrfData(res as unknown as JSON[], form.getValues("ym"), form.getValues('pbnstRuseFcltFk')) as PrfType[])
|
|
});
|
|
}
|
|
}
|
|
}, [file]);
|
|
|
|
return (
|
|
<>
|
|
<div>
|
|
<Form {...form}>
|
|
<form onSubmit={form.handleSubmit(onSubmit)}>
|
|
<div className="flex justify-between items-center">
|
|
<Title title={"하수재이용 실적등록"}></Title>
|
|
<Button className="mr-6 mt-10" type="submit">
|
|
저장
|
|
</Button>
|
|
</div>
|
|
<h3 className="text-2xl font-bold text-gray-900 my-3 ml-7">
|
|
{"점검정보 입력"}
|
|
</h3>
|
|
|
|
<div className="mx-7">
|
|
<FormField
|
|
control={form.control}
|
|
name="ym"
|
|
render={({ field }) => (
|
|
<FormUi label="등록년도">
|
|
<SelectBox
|
|
className="w-1/2 pr-4 text-3xl"
|
|
selectClass={""}
|
|
label=""
|
|
selectArray={yearOptions}
|
|
value={field.value || new Date().getFullYear()}
|
|
onSelectValue={(val) => field.onChange(val)}
|
|
/>
|
|
</FormUi>
|
|
)}
|
|
/>
|
|
<div className="flex gap-4 mt-4">
|
|
<div className="flex-1">
|
|
<FormField
|
|
control={form.control}
|
|
name="bscFctNm"
|
|
render={({ field }) => (
|
|
<FormUi label="시설명">
|
|
<Input
|
|
className="text-3xl"
|
|
value={field.value || ''}
|
|
onChange={field.onChange}
|
|
readOnly
|
|
/>
|
|
</FormUi>
|
|
)}
|
|
/>
|
|
</div>
|
|
<div className="flex-1">
|
|
<FormField
|
|
control={form.control}
|
|
name="pbnstRuseFcltNm"
|
|
render={({ field }) => (
|
|
<FormUi label="재이용시설명">
|
|
<Input
|
|
className="flex-1 text-3xl"
|
|
value={field.value || ''}
|
|
onChange={field.onChange}
|
|
readOnly
|
|
/>
|
|
</FormUi>
|
|
)}
|
|
/>
|
|
</div>
|
|
</div>
|
|
<div className="mt-4">
|
|
<FormField
|
|
control={form.control}
|
|
name="file"
|
|
render={({ field }) => (
|
|
<FormUi label="점검결과 파일">
|
|
{/* <FileUpload
|
|
files={files || []}
|
|
setFiles={(files: File[] | null) => {
|
|
if (files) setFiles(files);
|
|
}}
|
|
fileTypes="xlsx"
|
|
maxFiles={1}
|
|
|
|
|
|
|
|
></FileUpload> */}
|
|
|
|
<div className="space-y-4 p-4">
|
|
<Input
|
|
type="file"
|
|
onChange={handleFileChange}
|
|
accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
|
|
/>
|
|
</div>
|
|
</FormUi>
|
|
)}
|
|
/>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</Form>
|
|
</div>
|
|
</>
|
|
);
|
|
}
|