'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, Building2, Globe } from 'lucide-react' import SupplierForm from '@/components/suppliers/SupplierForm' import Link from 'next/link' import type { Supplier, SupplierType, CreateSupplierInput } from '@/types' const supplierTypeLabels: Record = { swedish_business: 'Svenskt företag', eu_business: 'EU-företag', non_eu_business: 'Utanför EU', } const supplierTypeIcons: Record = { swedish_business: Building2, eu_business: Globe, non_eu_business: Globe, } export default function SuppliersPage() { const [suppliers, setSuppliers] = 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(() => { fetchSuppliers() }, []) async function fetchSuppliers() { setIsLoading(true) const { data, error } = await supabase .from('suppliers') .select('*') .order('name', { ascending: true }) if (error) { toast({ title: 'Fel', description: 'Kunde inte hämta leverantörer', variant: 'destructive', }) } else { setSuppliers(data || []) } setIsLoading(false) } async function handleCreateSupplier(data: CreateSupplierInput) { setIsCreating(true) const response = await fetch('/api/suppliers', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data), }) const result = await response.json() if (!response.ok) { const fieldErrors = result.errors?.map((e: { field: string; message: string }) => `${e.field}: ${e.message}`).join(', ') toast({ title: 'Fel', description: fieldErrors || result.error || 'Kunde inte skapa leverantör', variant: 'destructive', }) } else { toast({ title: 'Leverantör skapad', description: `${data.name} har lagts till`, }) setSuppliers([...suppliers, result.data]) setIsDialogOpen(false) } setIsCreating(false) } const filteredSuppliers = suppliers.filter((s) => s.name.toLowerCase().includes(searchTerm.toLowerCase()) || s.email?.toLowerCase().includes(searchTerm.toLowerCase()) || s.org_number?.includes(searchTerm) ) return (

Leverantörer

Hantera dina leverantörer och deras betalningsuppgifter

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

Inga träffar

Inga leverantörer matchar "{searchTerm}"

) : (

Inga leverantörer

Lägg till din första leverantör för att börja registrera inköpsfakturor

)}
) : (
{filteredSuppliers.map((supplier) => { const Icon = supplierTypeIcons[supplier.supplier_type] return (
{supplier.name} {supplier.email || 'Ingen e-post'}
{supplierTypeLabels[supplier.supplier_type]}
{supplier.org_number && (

Org.nr: {supplier.org_number}

)} {supplier.bankgiro && (

Bankgiro: {supplier.bankgiro}

)} {supplier.city && (

{supplier.city}, {supplier.country}

)}
) })}
)}
) }