import { useState } from 'react' import { createFileRoute, useNavigate, Link } from '@tanstack/react-router' import { useQuery, useMutation } from '@tanstack/react-query' import { useForm } from 'react-hook-form' import { zodResolver } from '@hookform/resolvers/zod' import { RepairBatchCreateSchema } from '@forte/shared/schemas' import { repairBatchMutations } from '@/api/repairs' import { accountListOptions } from '@/api/accounts' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Textarea } from '@/components/ui/textarea' import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card' import { ArrowLeft, Search, Plus, X } from 'lucide-react' import { toast } from 'sonner' import type { Account } from '@/types/account' export const Route = createFileRoute('/_authenticated/repair-batches/new')({ component: NewRepairBatchPage, }) function NewRepairBatchPage() { const navigate = useNavigate() const [accountSearch, setAccountSearch] = useState('') const [selectedAccount, setSelectedAccount] = useState(null) const [showAccountDropdown, setShowAccountDropdown] = useState(false) const { data: accountsData } = useQuery( accountListOptions({ page: 1, limit: 20, q: accountSearch || undefined, order: 'asc', sort: 'name' }), ) const { register, handleSubmit, setValue, formState: { errors }, } = useForm({ resolver: zodResolver(RepairBatchCreateSchema), defaultValues: { accountId: '', contactName: '', contactPhone: '', contactEmail: '', notes: '', }, }) const mutation = useMutation({ mutationFn: repairBatchMutations.create, onSuccess: (batch) => { toast.success('Repair batch created') navigate({ to: '/repair-batches/$batchId', params: { batchId: batch.id }, search: {} as any }) }, onError: (err) => toast.error(err.message), }) function selectAccount(account: Account) { setSelectedAccount(account) setShowAccountDropdown(false) setAccountSearch('') setValue('accountId', account.id) if (account.phone) setValue('contactPhone', account.phone) if (account.email) setValue('contactEmail', account.email) setValue('contactName', account.name) } function clearAccount() { setSelectedAccount(null) setValue('accountId', '') setValue('contactName', '') setValue('contactPhone', '') setValue('contactEmail', '') } const accounts = accountsData?.data ?? [] return (

New Repair Batch

mutation.mutate(data))} className="space-y-6">
Account New Account
{!selectedAccount ? (
{ setAccountSearch(e.target.value); setShowAccountDropdown(true) }} onFocus={() => setShowAccountDropdown(true)} className="pl-9" />
{showAccountDropdown && accountSearch.length > 0 && (
{accounts.length === 0 ? (
No accounts found
) : ( accounts.map((a) => ( )) )}
)} {errors.accountId &&

{errors.accountId.message}

}
) : (

{selectedAccount.name}

{selectedAccount.phone && {selectedAccount.phone}} {selectedAccount.email && {selectedAccount.email}} {selectedAccount.accountNumber && #{selectedAccount.accountNumber}}
)}
Contact & Details