"use client"; import { RouterInvestasi_OLD } from "@/app/lib/router_hipmi/router_investasi"; import { AccentColor, MainColor, } from "@/app_modules/_global/color/color_pallet"; import { ComponentGlobal_WarningMaxUpload, maksimalUploadFile, } from "@/app_modules/_global/component/waring_popup"; import { ComponentGlobal_NotifikasiBerhasil } from "@/app_modules/_global/notif_global/notifikasi_berhasil"; import { ComponentGlobal_NotifikasiGagal } from "@/app_modules/_global/notif_global/notifikasi_gagal"; import { ComponentGlobal_NotifikasiPeringatan } from "@/app_modules/_global/notif_global/notifikasi_peringatan"; import { MODEL_DEFAULT_MASTER_OLD } from "@/app_modules/model_global/model_default_master"; import notifikasiToAdmin_funCreate from "@/app_modules/notifikasi/fun/create/create_notif_to_admin"; import mqtt_client from "@/util/mqtt_client"; import { AspectRatio, Box, Button, Center, Divider, FileButton, Group, Image, Paper, Select, Stack, Text, TextInput, } from "@mantine/core"; import { IconUpload } from "@tabler/icons-react"; import { useAtom } from "jotai"; import _ from "lodash"; import { useRouter } from "next/navigation"; import { useState } from "react"; import { funCreateInvestasi } from "../fun/fun_create_investasi"; import { gs_investas_menu, gs_investasi_status } from "../g_state"; export default function InvestasiCreate({ pencarianInvestor, periodeDeviden, pembagianDeviden, }: { pencarianInvestor: MODEL_DEFAULT_MASTER_OLD[]; periodeDeviden: MODEL_DEFAULT_MASTER_OLD[]; pembagianDeviden: MODEL_DEFAULT_MASTER_OLD[]; }) { const router = useRouter(); const [fl, setFl] = useState(null); const [img, setImg] = useState(); const [pdf, setPdf] = useState(null); const [filePdf, setFilePdf] = useState(null); const [changeColor, setChangeColor] = useAtom(gs_investas_menu); const [activeTab, setActiveTab] = useAtom(gs_investasi_status); const [totalLembar, setTotalLembar] = useState(0); const [isLoading, setLoading] = useState(false); const [value, setValue] = useState({ title: "", targetDana: 0, hargaLembar: 0, roi: 0, pencarianInvestorId: "", periodeDevidenId: "", pembagianDevidenId: "", }); const [target, setTarget] = useState(""); const [harga, setHarga] = useState(""); async function onSubmit() { const body = { title: value.title, targetDana: value.targetDana, hargaLembar: value.hargaLembar, totalLembar: totalLembar, roi: value.roi, masterPeriodeDevidenId: value.periodeDevidenId, masterPembagianDevidenId: value.pembagianDevidenId, masterPencarianInvestorId: value.pencarianInvestorId, }; // if (_.values(body).includes("")) return toast("Lengkapi data"); if (!fl) return ComponentGlobal_NotifikasiPeringatan("Gambar Kosong"); if (!pdf) return ComponentGlobal_NotifikasiPeringatan("File Kosong"); const gmbr = new FormData(); gmbr.append("file", fl as any); const flPdf = new FormData(); flPdf.append("file", pdf as any); const res = await funCreateInvestasi(gmbr, flPdf, body as any); if (res.status === 201) { const dataNotif = { appId: res.data?.id, status: res.data?.MasterStatusInvestasi?.name, userId: res.data?.authorId, pesan: res.data?.title, kategoriApp: "INVESTASI", title: "Investasi baru", }; const notif = await notifikasiToAdmin_funCreate({ data: dataNotif as any, }); if (notif.status === 201) { mqtt_client.publish( "ADMIN", JSON.stringify({ count: 1, }) ); setChangeColor(1); setActiveTab("Review"); setLoading(true); ComponentGlobal_NotifikasiBerhasil(res.message); router.push(RouterInvestasi_OLD.portofolio); } } else { ComponentGlobal_NotifikasiGagal(res.message); } } async function onTotalLembar({ target, harga, }: { target: number; harga: number; }) { const hasil: any = target / harga; setTotalLembar(_.floor(hasil === Infinity ? 0 : hasil)); } return ( <> {/* Inputan Create */} {img ? ( Foto ) : (
Upload Gambar
)} {/* Upload Foto */} { try { const buffer = URL.createObjectURL( new Blob([new Uint8Array(await files.arrayBuffer())]) ); // console.log(files.size); if (files.size > maksimalUploadFile) { ComponentGlobal_WarningMaxUpload({}); } else { setImg(buffer); setFl(files); } } catch (error) { console.log(error); } }} accept="image/png,image/jpeg" > {(props) => ( )} {/* Upload File */} {!pdf ? ( Upload File Prospektus ) : ( {pdf.name} )} {/* {JSON.stringify(filePdf)} */} { try { const buffer = URL.createObjectURL( new Blob([new Uint8Array(await files.arrayBuffer())]) ); if (files.size > maksimalUploadFile) { ComponentGlobal_WarningMaxUpload({}); } else { setFilePdf(buffer); setPdf(files); } } catch (error) { console.log(error); } }} > {(props) => ( )} { setValue({ ...value, title: val.target.value, }); }} /> Rp.} min={0} withAsterisk label="Dana Dibutuhkan" placeholder="0" value={target} onChange={(val) => { // console.log(typeof val) const match = val.currentTarget.value .replace(/\./g, "") .match(/^[0-9]+$/); if (val.currentTarget.value === "") return setTarget(0 + ""); if (!match?.[0]) return null; const nilai = val.currentTarget.value.replace(/\./g, ""); const targetNilai = Intl.NumberFormat("id-ID").format(+nilai); setTarget(targetNilai); setValue({ ...value, targetDana: +nilai, }); }} /> Rp.} min={0} withAsterisk label="Harga Per Lembar" placeholder="0" value={harga} onChange={(val) => { try { // console.log(typeof +val.currentTarget.value); const match = val.currentTarget.value .replace(/\./g, "") .match(/^[0-9]+$/); if (val.currentTarget.value === "") return setHarga(0 + ""); if (!match?.[0]) return null; const nilai = val.currentTarget.value.replace(/\./g, ""); const targetNilai = Intl.NumberFormat("id-ID").format(+nilai); onTotalLembar({ target: value.targetDana, harga: +nilai, }); setHarga(targetNilai); setValue({ ...value, hargaLembar: +nilai, }); } catch (error) { console.log(error); } }} /> Total Lembar {totalLembar} *Total lembar dihitung dari, Target Dana : Harga Perlembar % } withAsterisk type="number" label={"Rasio Keuntungan / ROI %"} placeholder="Masukan rasio keuntungan" onChange={(val) => { setValue({ ...value, roi: _.toNumber(val.target.value), }); }} /> ({ value: e.id, label: e.name }))} onChange={(val) => { setValue({ ...(value as any), periodeDevidenId: val, }); }} />