'use client'; import PendapatanAsliDesa from '@/app/admin/(dashboard)/_state/ekonomi/PADesa'; import colors from '@/con/colors'; import { Box, Button, Group, Loader, Paper, Stack, Text, TextInput, Title } from '@mantine/core'; import { IconArrowBack } from '@tabler/icons-react'; import { useRouter } from 'next/navigation'; import { useState } from 'react'; import { toast } from 'react-toastify'; import { useProxy } from 'valtio/utils'; function CreatePembiayaan() { const pembiayaanState = useProxy(PendapatanAsliDesa.pembiayaan); const router = useRouter(); const [isSubmitting, setIsSubmitting] = useState(false); // Check if form is valid const isFormValid = () => { return ( pembiayaanState.create.form.name?.trim() !== '' && pembiayaanState.create.form.value !== null && pembiayaanState.create.form.value > 0 ); }; const formatRupiah = (value: number | string) => { const number = typeof value === 'number' ? value : Number(value.replace(/\D/g, '')); return new Intl.NumberFormat('id-ID', { style: 'currency', currency: 'IDR', minimumFractionDigits: 0, }).format(number); }; const unformatRupiah = (value: string) => { return Number(value.replace(/\D/g, '')); }; const resetForm = () => { pembiayaanState.create.form = { name: '', value: 0, }; }; const handleSubmit = async () => { try { setIsSubmitting(true); if (!pembiayaanState.create.form.name || !pembiayaanState.create.form.value) { return toast.warn('Nama dan nilai wajib diisi'); } await pembiayaanState.create.submit(); resetForm(); router.push('/admin/ekonomi/PADesa-pendapatan-asli-desa/pembiayaan'); } catch (error) { console.error('Error creating pembiayaan:', error); toast.error('Gagal menambahkan jenis pembiayaan'); } finally { setIsSubmitting(false); } }; return ( {/* Header */} Tambah Jenis Pembiayaan {/* Form Card */} Nama Jenis Pembiayaan} placeholder="Masukkan nama jenis pembiayaan" value={pembiayaanState.create.form.name} onChange={(e) => { pembiayaanState.create.form.name = e.currentTarget.value; }} required /> Nilai} placeholder="Masukkan nilai" value={formatRupiah(pembiayaanState.create.form.value)} onChange={(e) => { const raw = e.currentTarget.value; pembiayaanState.create.form.value = unformatRupiah(raw); }} required /> {/* Tombol Simpan */} ); } export default CreatePembiayaan;