// 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 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 { AccountRequest } from '@/services/queries/chartOfAccountType' import { useChartOfAccount } from '@/services/queries/chartOfAccount' import { Account, ChartOfAccount } from '@/types/services/chartOfAccount' import { useAccountsMutation } from '@/services/mutations/account' type Props = { open: boolean handleClose: () => void accountData?: Account[] setData: (data: Account[]) => void editingAccount?: Account | null } type FormValidateType = { name: string code: string account_type: string opening_balance: number description: string chart_of_account_id: string } // Vars const initialData = { name: '', code: '', account_type: '', opening_balance: 0, description: '', chart_of_account_id: '' } // Static Account Types const staticAccountTypes = [ { id: '1', name: 'Cash', code: 'cash', description: 'Cash account' }, { id: '2', name: 'Wallet', code: 'wallet', description: 'Digital wallet account' }, { id: '3', name: 'Bank', code: 'bank', description: 'Bank account' }, { id: '4', name: 'Credit', code: 'credit', description: 'Credit account' }, { id: '5', name: 'Debit', code: 'debit', description: 'Debit account' }, { id: '6', name: 'Asset', code: 'asset', description: 'Asset account' }, { id: '7', name: 'Liability', code: 'liability', description: 'Liability account' }, { id: '8', name: 'Equity', code: 'equity', description: 'Equity account' }, { id: '9', name: 'Revenue', code: 'revenue', description: 'Revenue account' }, { id: '10', name: 'Expense', code: 'expense', description: 'Expense account' } ] const AccountFormDrawer = (props: Props) => { // Props const { open, handleClose, accountData, setData, editingAccount } = props // Determine if we're editing const isEdit = !!editingAccount const { data: accounts, isLoading: isLoadingAccounts } = useChartOfAccount({ page: 1, limit: 100 }) const { createAccount, updateAccount } = useAccountsMutation() // Use static account types const accountTypeOptions = staticAccountTypes // Process chart of accounts for the dropdown const chartOfAccountOptions = accounts?.data.length ? accounts.data .filter(account => account.is_active) // Only show active accounts .map(account => ({ id: account.id, code: account.code, name: account.name, description: account.description })) : [] // 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.number, account_type: editingAccount.account_type, opening_balance: editingAccount.opening_balance, description: editingAccount.description || '', chart_of_account_id: editingAccount.chart_of_account_id }) } else { // Reset to initial data for new account resetForm(initialData) } } }, [editingAccount, open, resetForm]) const onSubmit = (data: FormValidateType) => { if (isEdit && editingAccount) { const accountRequest: AccountRequest = { chart_of_account_id: data.chart_of_account_id, name: data.name, number: data.code, account_type: data.account_type, opening_balance: data.opening_balance, description: data.description } updateAccount.mutate( { id: editingAccount.id, payload: accountRequest }, { onSuccess: () => { handleClose() resetForm(initialData) } } ) } else { // Create new account - this would typically be sent as AccountRequest to API const accountRequest: AccountRequest = { chart_of_account_id: data.chart_of_account_id, name: data.name, number: data.code, account_type: data.account_type, opening_balance: data.opening_balance, description: data.description } createAccount.mutate(accountRequest, { onSuccess: () => { 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 * ( )} />
{/* Tipe Akun */}
Tipe Akun * ( option.code === value) || null} onChange={(_, newValue) => onChange(newValue?.code || '')} getOptionLabel={option => option.name} renderOption={(props, option) => (
{option.name}
)} renderInput={params => ( )} isOptionEqualToValue={(option, value) => option.code === value.code} /> )} />
{/* Chart of Account */}
Chart of Account * ( option.id === value) || null} onChange={(_, newValue) => onChange(newValue?.id || '')} getOptionLabel={option => `${option.code} - ${option.name}`} renderOption={(props, option) => (
{option.code} - {option.name} {option.description && ( {option.description} )}
)} renderInput={params => ( )} isOptionEqualToValue={(option, value) => option.id === value.id} disabled={isLoadingAccounts} noOptionsText={isLoadingAccounts ? 'Loading...' : 'Tidak ada chart of account tersedia'} /> )} />
{/* Opening Balance */}
Saldo Awal * ( field.onChange(Number(e.target.value))} {...(errors.opening_balance && { error: true, helperText: errors.opening_balance.type === 'min' ? 'Saldo awal tidak boleh negatif.' : 'Field ini wajib diisi.' })} /> )} />
{/* Deskripsi */}
Deskripsi ( )} />
{/* Sticky Footer */}
) } export default AccountFormDrawer