'use client' import { useState } from 'react' import { useForm, Controller } from 'react-hook-form' import { zodResolver } from '@hookform/resolvers/zod' import { z } from 'zod' 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 { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select' import { useToast } from '@/components/ui/use-toast' import { Loader2, CheckCircle, XCircle } from 'lucide-react' import type { CreateCustomerInput, CustomerType } from '@/types' const schema = z.object({ name: z.string().min(1, 'Namn krävs'), customer_type: z.enum(['individual', 'swedish_business', 'eu_business', 'non_eu_business']), email: z.string().email('Ogiltig e-postadress').optional().or(z.literal('')), phone: z.string().optional(), address_line1: z.string().optional(), address_line2: z.string().optional(), postal_code: z.string().optional(), city: z.string().optional(), country: z.string().optional(), org_number: z.string().optional(), vat_number: z.string().optional(), default_payment_terms: z.number().min(1).optional(), notes: z.string().optional(), }) type FormData = z.infer interface CustomerFormProps { onSubmit: (data: CreateCustomerInput) => Promise isLoading: boolean initialData?: Partial } export default function CustomerForm({ onSubmit, isLoading, initialData, }: CustomerFormProps) { const { toast } = useToast() const [isValidatingVat, setIsValidatingVat] = useState(false) const [vatValidationResult, setVatValidationResult] = useState<{ valid: boolean name?: string } | null>(null) const { register, handleSubmit, watch, control, setValue, formState: { errors }, } = useForm({ resolver: zodResolver(schema), defaultValues: { name: initialData?.name || '', customer_type: initialData?.customer_type || 'swedish_business', email: initialData?.email || '', phone: initialData?.phone || '', address_line1: initialData?.address_line1 || '', postal_code: initialData?.postal_code || '', city: initialData?.city || '', country: initialData?.country || 'Sweden', org_number: initialData?.org_number || '', vat_number: initialData?.vat_number || '', default_payment_terms: initialData?.default_payment_terms || 30, notes: initialData?.notes || '', }, }) const customerType = watch('customer_type') const vatNumber = watch('vat_number') const handleValidateVat = async () => { if (!vatNumber) return setIsValidatingVat(true) setVatValidationResult(null) try { const response = await fetch('/api/vat/validate', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ vat_number: vatNumber }), }) const result = await response.json() setVatValidationResult({ valid: result.valid, name: result.name, }) if (result.valid && result.name) { toast({ title: 'VAT-nummer verifierat', description: `Företag: ${result.name}`, }) } else if (!result.valid) { toast({ title: 'Verifiering misslyckades', description: result.error || 'VAT-numret kunde inte verifieras', variant: 'destructive', }) } } catch { toast({ title: 'Fel', description: 'Kunde inte verifiera VAT-nummer', variant: 'destructive', }) } finally { setIsValidatingVat(false) } } const onFormSubmit = (data: FormData) => { onSubmit({ ...data, email: data.email || undefined, }) } return (
{/* Customer Type */}
( )} />

Kundtypen påverkar hur moms hanteras på fakturor

{/* Name */}
{errors.name && (

{errors.name.message}

)}
{/* Contact */}
{errors.email && (

{errors.email.message}

)}
{/* Address */}

Adress

{/* Business info */} {customerType !== 'individual' && (

Företagsuppgifter

{(customerType === 'eu_business' || customerType === 'swedish_business') && (
{customerType === 'eu_business' && ( )}
{customerType === 'eu_business' && (

Verifiera VAT-numret för att kunna fakturera med omvänd skattskyldighet (0% moms)

)}
)}
)} {/* Payment terms */}
{/* Notes */}