'use client';
import PendapatanAsliDesa from '@/app/admin/(dashboard)/_state/ekonomi/PADesa';
import colors from '@/con/colors';
import {
Box,
Button,
Group,
Loader,
MultiSelect,
Paper,
Skeleton,
Stack,
Text,
TextInput,
Title
} from '@mantine/core';
import { useShallowEffect } from '@mantine/hooks';
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 CreateAPBDesa() {
const apbDesaState = useProxy(PendapatanAsliDesa.ApbDesa);
const router = useRouter();
const [isSubmitting, setIsSubmitting] = useState(false);
// Check if form is valid
const isFormValid = () => {
return (
apbDesaState.create.form.tahun !== null &&
apbDesaState.create.form.tahun > 0 &&
apbDesaState.create.form.pendapatanIds.length > 0 &&
apbDesaState.create.form.belanjaIds.length > 0 &&
apbDesaState.create.form.pembiayaanIds.length > 0
);
};
const resetForm = () => {
apbDesaState.create.form = {
tahun: 0,
pendapatanIds: [],
belanjaIds: [],
pembiayaanIds: [],
};
};
const handleSubmit = async () => {
try {
setIsSubmitting(true);
await apbDesaState.create.submit();
resetForm();
router.push('/admin/ekonomi/PADesa-pendapatan-asli-desa/apbdesa');
} catch (error) {
console.error('Error creating APB Desa:', error);
toast.error('Gagal membuat APB Desa');
} finally {
setIsSubmitting(false);
}
};
return (
{/* Header */}
Tambah APB Desa
{/* Form */}
{
apbDesaState.create.form.tahun = Number(val.target.value);
}}
label={Tahun}
placeholder="Masukkan tahun anggaran"
required
/>
{
apbDesaState.create.form.pendapatanIds = ids;
}}
/>
{
apbDesaState.create.form.belanjaIds = ids;
}}
/>
{
apbDesaState.create.form.pembiayaanIds = ids;
}}
/>
{/* Action */}
{/* Tombol Simpan */}
);
/* ---------- Select Pendapatan ---------- */
interface SelectPendapatanProps {
selectedIds: string[];
onSelectionChange: (ids: string[]) => void;
}
function SelectPendapatan({ selectedIds = [], onSelectionChange }: SelectPendapatanProps) {
const pendapatanState = useProxy(PendapatanAsliDesa.pendapatan);
useShallowEffect(() => {
pendapatanState.findMany.load();
}, []);
if (!pendapatanState.findMany.data) {
return ;
}
return (
Pendapatan}
data={pendapatanState.findMany.data.map((p) => ({
value: p.id,
label: p.name,
}))}
value={selectedIds}
onChange={onSelectionChange}
searchable
clearable
placeholder="Pilih pendapatan..."
nothingFoundMessage="Tidak ditemukan"
/>
);
}
/* ---------- Select Belanja ---------- */
interface SelectBelanjaProps {
selectedIds: string[];
onSelectionChange: (ids: string[]) => void;
}
function SelectBelanja({ selectedIds = [], onSelectionChange }: SelectBelanjaProps) {
const belanjaState = useProxy(PendapatanAsliDesa.belanja);
useShallowEffect(() => {
belanjaState.findMany.load();
}, []);
if (!belanjaState.findMany.data) {
return ;
}
return (
Belanja}
data={belanjaState.findMany.data.map((b) => ({
value: b.id,
label: b.name,
}))}
value={selectedIds}
onChange={onSelectionChange}
searchable
clearable
placeholder="Pilih belanja..."
nothingFoundMessage="Tidak ditemukan"
/>
);
}
/* ---------- Select Pembiayaan ---------- */
interface SelectPembiayaanProps {
selectedIds: string[];
onSelectionChange: (ids: string[]) => void;
}
function SelectPembiayaan({ selectedIds = [], onSelectionChange }: SelectPembiayaanProps) {
const pembiayaanState = useProxy(PendapatanAsliDesa.pembiayaan);
useShallowEffect(() => {
pembiayaanState.findMany.load();
}, []);
if (!pembiayaanState.findMany.data) {
return ;
}
return (
Pembiayaan}
data={pembiayaanState.findMany.data.map((b) => ({
value: b.id,
label: b.name,
}))}
value={selectedIds}
onChange={onSelectionChange}
searchable
clearable
placeholder="Pilih pembiayaan..."
nothingFoundMessage="Tidak ditemukan"
/>
);
}
}
export default CreateAPBDesa;