import { useForm } from '@inertiajs/react'; import { Loader, Plus, Trash2 } from 'lucide-react'; import { X, Save } from 'lucide-react'; import { useEffect } from 'react'; import { NumericFormat } from 'react-number-format'; import { toast } from 'sonner'; import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog" import { Empty, EmptyDescription, EmptyHeader, EmptyTitle } from '@/components/ui/empty'; import { FieldGroup } from "@/components/ui/field" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { RadioGroup, RadioGroupItem } from '@/components/ui/radio-group'; import payrollRoutes from '@/routes/payroll'; import type { Payroll } from '@/types'; interface PayrollFormModalProps { isOpen: boolean; onClose: () => void; payroll: Payroll | null; } export function PayrollFormModal({ isOpen, onClose, payroll }: PayrollFormModalProps) { const { data, setData, patch, processing, errors, reset, clearErrors } = useForm({ adjustments: [] as { type: 'bonus' | 'deduction', amount: number, description: string }[], }); useEffect(() => { if (payroll) { setData({ adjustments: payroll.adjustments?.map(adj => ({ type: adj.type, amount: adj.amount, description: adj.description })) || [], }); } else { reset(); } }, [payroll]); const handleClose = () => { onClose(); setTimeout(() => { reset(); clearErrors(); }, 200); }; const addAdjustment = () => { setData('adjustments', [ ...data.adjustments, { type: 'bonus', amount: 0, description: '' } ]); }; const removeAdjustment = (index: number) => { const newAdjustments = [...data.adjustments]; newAdjustments.splice(index, 1); setData('adjustments', newAdjustments); }; const updateAdjustment = (index: number, key: string, value: any) => { const newAdjustments = [...data.adjustments]; newAdjustments[index] = { ...newAdjustments[index], [key]: value }; setData('adjustments', newAdjustments); }; const onSubmit = (e: React.FormEvent) => { e.preventDefault(); if (payroll) { patch(payrollRoutes.update(payroll.id).url, { onSuccess: (response: any) => { toast.success(response.props.flash.success); handleClose(); }, }); } }; return ( !open && handleClose()}> Penyesuaian Penggajian - {payroll?.user?.name}

Bonus & Potongan

{data.adjustments.length === 0 && ( Ooops... Tidak ada data yang ditemukan. )} {data.adjustments.map((adj, index) => (
updateAdjustment(index, 'type', val)} className="flex items-center gap-6 w-fit" >
{ updateAdjustment(index, 'amount', values.value) }} placeholder="Rp 0" autoComplete='off' />
updateAdjustment(index, 'description', e.target.value) } placeholder="Contoh: Lembur" />
))}
); }