'use client' import { useState, useEffect } from 'react' import { createClient } from '@/lib/supabase/client' import { Button } from '@/components/ui/button' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' import { Badge } from '@/components/ui/badge' import { Input } from '@/components/ui/input' import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog' import { useToast } from '@/components/ui/use-toast' import { Plus, Search, Users, Building, Globe, User } from 'lucide-react' import CustomerForm from '@/components/customers/CustomerForm' import { EmptyCustomers } from '@/components/ui/empty-state' import Link from 'next/link' import type { Customer, CustomerType, CreateCustomerInput } from '@/types' const customerTypeLabels: Record = { individual: 'Privatperson', swedish_business: 'Svenskt företag', eu_business: 'EU-företag', non_eu_business: 'Utanför EU', } const customerTypeIcons: Record = { individual: User, swedish_business: Building, eu_business: Globe, non_eu_business: Globe, } export default function CustomersPage() { const [customers, setCustomers] = useState([]) const [isLoading, setIsLoading] = useState(true) const [searchTerm, setSearchTerm] = useState('') const [isDialogOpen, setIsDialogOpen] = useState(false) const [isCreating, setIsCreating] = useState(false) const { toast } = useToast() const supabase = createClient() useEffect(() => { fetchCustomers() }, []) async function fetchCustomers() { setIsLoading(true) const { data, error } = await supabase .from('customers') .select('*') .order('name', { ascending: true }) if (error) { toast({ title: 'Fel', description: 'Kunde inte hämta kunder', variant: 'destructive', }) } else { setCustomers(data || []) } setIsLoading(false) } async function handleCreateCustomer(data: CreateCustomerInput) { setIsCreating(true) const response = await fetch('/api/customers', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data), }) const result = await response.json() if (!response.ok) { toast({ title: 'Fel', description: result.error || 'Kunde inte skapa kund', variant: 'destructive', }) } else { toast({ title: 'Kund skapad', description: `${data.name} har lagts till`, }) setCustomers([...customers, result.data]) setIsDialogOpen(false) } setIsCreating(false) } const filteredCustomers = customers.filter((customer) => customer.name.toLowerCase().includes(searchTerm.toLowerCase()) || customer.email?.toLowerCase().includes(searchTerm.toLowerCase()) || customer.org_number?.includes(searchTerm) ) return (

Kunder

Hantera dina kunder och deras faktureringsuppgifter

Lägg till kund
{/* Search */}
setSearchTerm(e.target.value)} className="pl-10" />
{/* Customer list */} {isLoading ? (
{[1, 2, 3].map((i) => (
))}
) : filteredCustomers.length === 0 ? ( {searchTerm ? (

Inga träffar

Inga kunder matchar "{searchTerm}"

) : ( setIsDialogOpen(true)} /> )}
) : (
{filteredCustomers.map((customer) => { const Icon = customerTypeIcons[customer.customer_type] return (
{customer.name} {customer.email || 'Ingen e-post'}
{customerTypeLabels[customer.customer_type]}
{customer.org_number && (

Org.nr: {customer.org_number}

)} {customer.vat_number && (

VAT: {customer.vat_number} {customer.vat_number_validated && ( Verifierad )}

)} {customer.city && (

{customer.city}, {customer.country}

)}
) })}
)}
) }