// React Imports import { useState, useEffect } from 'react' // MUI Imports import Button from '@mui/material/Button' import Drawer from '@mui/material/Drawer' import IconButton from '@mui/material/IconButton' import MenuItem from '@mui/material/MenuItem' import Typography from '@mui/material/Typography' import Box from '@mui/material/Box' // Third-party Imports import { useForm, Controller } from 'react-hook-form' // Component Imports import CustomTextField from '@core/components/mui/TextField' import CustomAutocomplete from '@/@core/components/mui/Autocomplete' import { useChartOfAccountTypes } from '@/services/queries/chartOfAccountType' // Account Type export type AccountType = { id: number code: string name: string category: string balance: string } export interface ChartOfAccountType { id: string name: string code: string description: string is_active: boolean created_at: string updated_at: string } type Props = { open: boolean handleClose: () => void accountData?: AccountType[] setData: (data: AccountType[]) => void editingAccount?: AccountType | null } type FormValidateType = { name: string code: string category: string parentAccount?: string } // Fallback categories (used when API data is not available) const fallbackAccountCategories = [ 'Kas & Bank', 'Piutang', 'Persediaan', 'Aset Tetap', 'Hutang', 'Ekuitas', 'Pendapatan', 'Beban' ] // Parent accounts (dummy data for dropdown) const parentAccounts = [ { id: 1, code: '1-10001', name: 'Kas' }, { id: 2, code: '1-10002', name: 'Bank BCA' }, { id: 3, code: '1-10003', name: 'Bank Mandiri' }, { id: 4, code: '1-10101', name: 'Piutang Usaha' }, { id: 5, code: '1-10201', name: 'Persediaan Barang' }, { id: 6, code: '2-20001', name: 'Hutang Usaha' }, { id: 7, code: '3-30001', name: 'Modal Pemilik' }, { id: 8, code: '4-40001', name: 'Penjualan' }, { id: 9, code: '5-50001', name: 'Beban Gaji' } ] // Vars const initialData = { name: '', code: '', category: '', parentAccount: '' } const AccountFormDrawer = (props: Props) => { // Props const { open, handleClose, accountData, setData, editingAccount } = props // Determine if we're editing const isEdit = !!editingAccount const { data: accountTypes, isLoading } = useChartOfAccountTypes() // Process account types for the dropdown const categoryOptions = accountTypes?.data.length ? accountTypes.data .filter(type => type.is_active) // Only show active types .map(type => ({ id: type.id, name: type.name, code: type.code, description: type.description })) : fallbackAccountCategories.map(category => ({ id: category.toLowerCase().replace(/\s+/g, '_'), name: category, code: category, description: category })) // Hooks const { control, reset: resetForm, handleSubmit, formState: { errors } } = useForm({ defaultValues: initialData }) // Reset form when editingAccount changes or drawer opens useEffect(() => { if (open) { if (editingAccount) { // Populate form with existing data resetForm({ name: editingAccount.name, code: editingAccount.code, category: editingAccount.category, parentAccount: '' }) } else { // Reset to initial data for new account resetForm(initialData) } } }, [editingAccount, open, resetForm]) const onSubmit = (data: FormValidateType) => { if (isEdit && editingAccount) { // Update existing account const updatedAccounts = accountData?.map(account => account.id === editingAccount.id ? { ...account, code: data.code, name: data.name, category: data.category } : account ) || [] setData(updatedAccounts) } else { // Create new account const newAccount: AccountType = { id: accountData?.length ? Math.max(...accountData.map(a => a.id)) + 1 : 1, code: data.code, name: data.name, category: data.category, balance: '0' } setData([...(accountData ?? []), newAccount]) } handleClose() resetForm(initialData) } const handleReset = () => { handleClose() resetForm(initialData) } return ( {/* Sticky Header */}
{isEdit ? 'Edit Akun' : 'Tambah Akun Baru'}
{/* Scrollable Content */}
onSubmit(data))}>
{/* Nama */}
Nama * ( )} />
{/* Kode */}
Kode * ( )} />
{/* Kategori */}
Kategori * ( option.name === value) || null} onChange={(_, newValue) => onChange(newValue?.name || '')} getOptionLabel={option => option.name} renderOption={(props, option) => (
{option.code} - {option.name}
)} renderInput={params => ( )} isOptionEqualToValue={(option, value) => option.name === value.name} disabled={isLoading} /> )} />
{/* Sub Akun dari */}
Sub Akun dari ( `${account.code} ${account.name}` === value) || null} onChange={(_, newValue) => onChange(newValue ? `${newValue.code} ${newValue.name}` : '')} getOptionLabel={option => `${option.code} ${option.name}`} renderInput={params => } isOptionEqualToValue={(option, value) => `${option.code} ${option.name}` === `${value.code} ${value.name}` } /> )} />
{/* Sticky Footer */}
) } export default AccountFormDrawer