/** * @ai-summary Form component for creating/editing ownership costs */ import React from 'react'; import { Button } from '../../../shared-minimal/components/Button'; import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider'; import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs'; import { DatePicker } from '@mui/x-date-pickers/DatePicker'; import dayjs from 'dayjs'; import { useCreateOwnershipCost } from '../hooks/useOwnershipCosts'; import type { OwnershipCostType } from '../types/ownership-costs.types'; import { useVehicles } from '../../vehicles/hooks/useVehicles'; import type { Vehicle } from '../../vehicles/types/vehicles.types'; import { useDocumentsList } from '../../documents/hooks/useDocuments'; import type { DocumentRecord } from '../../documents/types/documents.types'; interface OwnershipCostFormProps { onSuccess?: () => void; onCancel?: () => void; } export const OwnershipCostForm: React.FC = ({ onSuccess, onCancel }) => { const [vehicleID, setVehicleID] = React.useState(''); const [costType, setCostType] = React.useState('insurance'); const [amount, setAmount] = React.useState(''); const [description, setDescription] = React.useState(''); const [periodStart, setPeriodStart] = React.useState(''); const [periodEnd, setPeriodEnd] = React.useState(''); const [notes, setNotes] = React.useState(''); const [documentID, setDocumentID] = React.useState(''); const [error, setError] = React.useState(null); const { data: vehicles } = useVehicles(); const { data: documents } = useDocumentsList({ vehicleId: vehicleID }); const create = useCreateOwnershipCost(); const resetForm = () => { setVehicleID(''); setCostType('insurance'); setAmount(''); setDescription(''); setPeriodStart(''); setPeriodEnd(''); setNotes(''); setDocumentID(''); setError(null); }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(null); if (!vehicleID) { setError('Please select a vehicle.'); return; } const parsedAmount = parseFloat(amount); if (isNaN(parsedAmount) || parsedAmount <= 0) { setError('Please enter a valid positive amount.'); return; } try { await create.mutateAsync({ vehicleId: vehicleID, documentId: documentID || undefined, costType: costType, amount: parsedAmount, description: description.trim() || undefined, periodStart: periodStart || undefined, periodEnd: periodEnd || undefined, notes: notes.trim() || undefined, }); resetForm(); onSuccess?.(); } catch (err: any) { setError(err?.message || 'Failed to create ownership cost'); } }; const vehicleLabel = (v: Vehicle) => { if (v.nickname && v.nickname.trim().length > 0) return v.nickname.trim(); const parts = [v.year, v.make, v.model, v.trimLevel].filter(Boolean); const primary = parts.join(' ').trim(); if (primary.length > 0) return primary; if (v.vin && v.vin.length > 0) return v.vin; return v.id.slice(0, 8) + '...'; }; const documentLabel = (doc: DocumentRecord) => { return doc.title || `Document ${doc.id.slice(0, 8)}`; }; return (
$ setAmount(e.target.value)} required />
setDescription(e.target.value)} />
setPeriodStart(newValue?.format('YYYY-MM-DD') || '')} format="MM/DD/YYYY" slotProps={{ textField: { fullWidth: true, sx: { '& .MuiOutlinedInput-root': { minHeight: 44, }, }, }, }} />
setPeriodEnd(newValue?.format('YYYY-MM-DD') || '')} format="MM/DD/YYYY" slotProps={{ textField: { fullWidth: true, sx: { '& .MuiOutlinedInput-root': { minHeight: 44, }, }, }, }} />