'use client'; import { useState, useEffect } from 'react'; import { useRouter } from 'next/navigation'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; interface Tag { id: number; name: string; color: string | null; } export default function NewCustomerPage() { const router = useRouter(); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [tags, setTags] = useState([]); const [selectedTagIds, setSelectedTagIds] = useState([]); useEffect(() => { fetch('/api/customer-tags') .then((res) => res.json()) .then((data) => setTags(data.items || [])) .catch(() => {}); }, []); function toggleTag(tagId: number) { setSelectedTagIds((prev) => prev.includes(tagId) ? prev.filter((id) => id !== tagId) : [...prev, tagId], ); } async function handleSubmit(e: React.FormEvent) { e.preventDefault(); setLoading(true); setError(null); const formData = new FormData(e.currentTarget); const body = { firstName: formData.get('firstName') as string, lastName: formData.get('lastName') as string, email: (formData.get('email') as string) || undefined, phone: (formData.get('phone') as string) || undefined, whatsapp: (formData.get('whatsapp') as string) || undefined, address: (formData.get('address') as string) || undefined, city: (formData.get('city') as string) || undefined, notes: (formData.get('notes') as string) || undefined, customerType: (formData.get('customerType') as string) || 'RETAIL', tagIds: selectedTagIds.length > 0 ? selectedTagIds : undefined, }; try { const res = await fetch('/api/customers', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }); if (!res.ok) { const data = await res.json(); setError(data.error || 'Error al crear cliente'); return; } const customer = await res.json(); router.push(`/admin21/customers/${customer.id}`); } catch { setError('Error al crear cliente'); } finally { setLoading(false); } } return (

Nuevo Cliente

Agregá un nuevo cliente

Información del Cliente
{error && (
{error}
)}
{tags.length > 0 && (
{tags.map((tag) => ( ))}
)}