feat(F-186): completed feature

This commit is contained in:
chattie
2026-08-22 22:08:09 +02:00
parent 63a305bdd4
commit a3f6edd325
30 changed files with 3603 additions and 624 deletions

View File

@@ -1,9 +1,13 @@
'use client';
import { useEffect, useState } from 'react';
import { useCallback, useEffect, useState } from 'react';
import { api } from '@/lib/api-client';
interface PosStore { id: string; name: string; active: boolean }
interface PosStore {
id: string;
name: string;
active: boolean;
}
interface PosTerminal {
id: string;
storeId: string;
@@ -11,29 +15,106 @@ interface PosTerminal {
bindingCode: string | null;
boundAt: string | null;
status: string;
lastSeenAt: string | null;
settings?: { touchNavigationEnabled?: boolean; quickProductVariantIds?: Array<string | null> };
settings?: {
touchNavigationEnabled?: boolean;
quickProductVariantIds?: Array<string | null>;
lineDiscountsEnabled?: boolean;
};
}
interface CatalogOption { variantId: string; productId: string; name: string; sku: string; priceCents: number }
interface CatalogOption {
variantId: string;
name: string;
sku: string;
}
interface PaymentMethod {
id: string;
storeId: string;
code: string;
label: string;
kind: 'cash' | 'card' | 'other';
active: boolean;
sortOrder: number;
}
interface ReceiptSettings {
storeId: string;
name: string;
address: string;
taxId: string;
contactEmail: string;
contactPhone: string;
receiptHeader: string;
receiptFooter: string;
prefix: string;
nextNumber: number;
padding: number;
returnPolicy: string;
}
const emptyReceipt: ReceiptSettings = {
storeId: '',
name: '',
address: '',
taxId: '',
contactEmail: '',
contactPhone: '',
receiptHeader: '',
receiptFooter: '',
prefix: 'TPV',
nextNumber: 1,
padding: 6,
returnPolicy: 'Se admiten devoluciones durante 15 días con el ticket de compra.',
};
export default function PosAdminPage() {
const [stores, setStores] = useState<PosStore[]>([]);
const [terminals, setTerminals] = useState<PosTerminal[]>([]);
const [catalogOptions, setCatalogOptions] = useState<CatalogOption[]>([]);
const [storeId, setStoreId] = useState('');
const [name, setName] = useState('');
const [terminalName, setTerminalName] = useState('');
const [loading, setLoading] = useState(true);
const [creating, setCreating] = useState(false);
const [error, setError] = useState('');
const [newCode, setNewCode] = useState('');
const [copied, setCopied] = useState('');
const [catalogOptions, setCatalogOptions] = useState<CatalogOption[]>([]);
const [configuring, setConfiguring] = useState<PosTerminal | null>(null);
const [touchEnabled, setTouchEnabled] = useState(true);
const [quickSlots, setQuickSlots] = useState<Array<string | null>>([null, null, null, null]);
const [discountsEnabled, setDiscountsEnabled] = useState(true);
const [quickSlots, setQuickSlots] = useState<Array<string | null>>(
Array.from({ length: 8 }, () => null),
);
const [savingTouch, setSavingTouch] = useState(false);
const [touchMessage, setTouchMessage] = useState('');
const [paymentMethods, setPaymentMethods] = useState<PaymentMethod[]>([]);
const [newMethod, setNewMethod] = useState({
code: '',
label: '',
kind: 'other' as PaymentMethod['kind'],
});
const [savingMethod, setSavingMethod] = useState(false);
const [paymentMessage, setPaymentMessage] = useState('');
const [receipt, setReceipt] = useState<ReceiptSettings>(emptyReceipt);
const [savingReceipt, setSavingReceipt] = useState(false);
const [receiptMessage, setReceiptMessage] = useState('');
const load = async () => {
const loadStoreConfiguration = useCallback(async (selectedStoreId: string) => {
if (!selectedStoreId) return;
try {
const [methodData, receiptData] = await Promise.all([
api.get<{ items: PaymentMethod[] }>(
`/api/pos/admin/payment-methods?storeId=${encodeURIComponent(selectedStoreId)}`,
),
api.get<Partial<ReceiptSettings>>(
`/api/pos/admin/receipt-settings?storeId=${encodeURIComponent(selectedStoreId)}`,
),
]);
setPaymentMethods(methodData.items);
setReceipt({ ...emptyReceipt, ...receiptData, storeId: selectedStoreId });
} catch (err) {
setError(err instanceof Error ? err.message : 'No se pudo cargar la configuración de cobro');
}
}, []);
const load = useCallback(async () => {
setLoading(true);
setError('');
try {
@@ -45,15 +126,26 @@ export default function PosAdminPage() {
setStores(storeData.stores);
setTerminals(terminalData.terminals);
setCatalogOptions(catalogData.items);
setStoreId((current) => current || storeData.stores.find((store) => store.active)?.id || '');
const selected = storeId || storeData.stores.find((store) => store.active)?.id || '';
setStoreId(selected);
await loadStoreConfiguration(selected);
} catch (err) {
setError(err instanceof Error ? err.message : 'No se pudo cargar la configuración TPV');
} finally {
setLoading(false);
}
};
}, [loadStoreConfiguration, storeId]);
useEffect(() => { void load(); }, []);
useEffect(() => {
void load();
}, []); // eslint-disable-line react-hooks/exhaustive-deps
const changeStore = (nextStoreId: string) => {
setStoreId(nextStoreId);
setReceiptMessage('');
setPaymentMessage('');
void loadStoreConfiguration(nextStoreId);
};
const createTerminal = async (event: React.FormEvent) => {
event.preventDefault();
@@ -61,8 +153,11 @@ export default function PosAdminPage() {
setError('');
setNewCode('');
try {
const terminal = await api.post<PosTerminal>('/api/pos/admin/terminals', { storeId, name: name.trim() });
setName('');
const terminal = await api.post<PosTerminal>('/api/pos/admin/terminals', {
storeId,
name: terminalName.trim(),
});
setTerminalName('');
setNewCode(terminal.bindingCode ?? '');
await load();
} catch (err) {
@@ -75,8 +170,9 @@ export default function PosAdminPage() {
const openTouchConfig = (terminal: PosTerminal) => {
setConfiguring(terminal);
setTouchEnabled(terminal.settings?.touchNavigationEnabled !== false);
setDiscountsEnabled(terminal.settings?.lineDiscountsEnabled !== false);
const configured = terminal.settings?.quickProductVariantIds ?? [];
setQuickSlots([0, 1, 2, 3].map((slot) => configured[slot] ?? null));
setQuickSlots(Array.from({ length: 8 }, (_, slot) => configured[slot] ?? null));
setTouchMessage('');
};
@@ -87,9 +183,10 @@ export default function PosAdminPage() {
try {
await api.patch(`/api/pos/admin/terminals/${configuring.id}/touch-config`, {
touchNavigationEnabled: touchEnabled,
lineDiscountsEnabled: discountsEnabled,
quickProductVariantIds: quickSlots,
});
setTouchMessage('Configuración táctil guardada');
setTouchMessage('Configuración del terminal guardada');
await load();
} catch (err) {
setTouchMessage(err instanceof Error ? err.message : 'No se pudo guardar');
@@ -98,98 +195,458 @@ export default function PosAdminPage() {
}
};
const createPaymentMethod = async (event: React.FormEvent) => {
event.preventDefault();
setSavingMethod(true);
setPaymentMessage('');
try {
await api.post('/api/pos/admin/payment-methods', {
storeId,
code: newMethod.code.trim().toLowerCase(),
label: newMethod.label.trim(),
kind: newMethod.kind,
active: true,
sortOrder: paymentMethods.length,
});
setNewMethod({ code: '', label: '', kind: 'other' });
setPaymentMessage('Forma de pago creada');
await loadStoreConfiguration(storeId);
} catch (err) {
setPaymentMessage(err instanceof Error ? err.message : 'No se pudo crear la forma de pago');
} finally {
setSavingMethod(false);
}
};
const togglePaymentMethod = async (method: PaymentMethod) => {
setPaymentMessage('');
try {
await api.patch(`/api/pos/admin/payment-methods/${method.id}`, { active: !method.active });
setPaymentMessage(method.active ? 'Forma de pago desactivada' : 'Forma de pago activada');
await loadStoreConfiguration(storeId);
} catch (err) {
setPaymentMessage(err instanceof Error ? err.message : 'No se pudo actualizar');
}
};
const saveReceipt = async (event: React.FormEvent) => {
event.preventDefault();
setSavingReceipt(true);
setReceiptMessage('');
try {
await api.patch('/api/pos/admin/receipt-settings', receipt);
setReceiptMessage('Datos y numeración del ticket guardados');
await loadStoreConfiguration(storeId);
} catch (err) {
setReceiptMessage(err instanceof Error ? err.message : 'No se pudo guardar el ticket');
} finally {
setSavingReceipt(false);
}
};
const copyCode = async (code: string) => {
await navigator.clipboard.writeText(code);
setCopied(code);
setTimeout(() => setCopied(''), 2000);
window.setTimeout(() => setCopied(''), 2000);
};
return (
<div className="space-y-6">
<div>
<header>
<h1 className="text-2xl font-bold text-gray-900">TPV</h1>
<p className="mt-0.5 text-sm text-gray-500">Configura dispositivos y obtén el código para vincular cada caja.</p>
</div>
<p className="mt-0.5 text-sm text-gray-500">
Configura terminales táctiles, formas de pago y contenido fiscal del ticket.
</p>
</header>
<div className="grid gap-4 md:grid-cols-2">
<div className="rounded-xl border border-blue-200 bg-blue-50 p-5 text-sm text-blue-900">
<h2 className="font-bold">1. Vincular terminal</h2>
<p className="mt-1">Crea un terminal aquí y escribe su código de 8 caracteres en el navegador del TPV. Esto identifica qué caja física está vendiendo.</p>
</div>
<div className="rounded-xl border border-amber-200 bg-amber-50 p-5 text-sm text-amber-900">
<h2 className="font-bold">2. Abrir sesión de caja</h2>
<p className="mt-1">Después de vincular, el cajero indica el efectivo inicial en el propio TPV. La sesión representa el turno diario de caja.</p>
</div>
</div>
<label className="block max-w-md text-sm font-medium text-gray-700">
Tienda que estás configurando
<select
value={storeId}
onChange={(event) => changeStore(event.target.value)}
className="mt-1 w-full rounded-xl border border-gray-300 bg-white px-3 py-2.5"
>
{stores
.filter((store) => store.active)
.map((store) => (
<option key={store.id} value={store.id}>
{store.name}
</option>
))}
</select>
</label>
<form onSubmit={createTerminal} className="rounded-xl border border-gray-200 bg-white p-6">
<h2 className="mb-4 font-bold text-gray-900">Crear terminal</h2>
<div className="grid gap-4 md:grid-cols-[1fr_1fr_auto] md:items-end">
<label className="text-sm font-medium text-gray-700">
Tienda
<select value={storeId} onChange={(event) => setStoreId(event.target.value)} required className="mt-1 w-full rounded-xl border border-gray-300 bg-white px-3 py-2.5">
{stores.filter((store) => store.active).map((store) => <option key={store.id} value={store.id}>{store.name}</option>)}
</select>
</label>
<label className="text-sm font-medium text-gray-700">
<h2 className="mb-4 font-bold text-gray-900">Crear y vincular terminal</h2>
<div className="flex flex-wrap items-end gap-4">
<label className="min-w-64 flex-1 text-sm font-medium text-gray-700">
Nombre del terminal
<input value={name} onChange={(event) => setName(event.target.value)} required maxLength={100} placeholder="Caja principal" className="mt-1 w-full rounded-xl border border-gray-300 px-3 py-2.5" />
<input
value={terminalName}
onChange={(event) => setTerminalName(event.target.value)}
required
maxLength={100}
placeholder="Caja principal"
className="mt-1 w-full rounded-xl border border-gray-300 px-3 py-2.5"
/>
</label>
<button type="submit" disabled={creating || !storeId || !name.trim()} className="rounded-xl bg-[#2D6A4F] px-5 py-2.5 text-sm font-semibold text-white disabled:opacity-50">
<button
disabled={creating || !storeId || !terminalName.trim()}
className="rounded-xl bg-[#2D6A4F] px-5 py-2.5 text-sm font-semibold text-white disabled:opacity-50"
>
{creating ? 'Creando…' : 'Crear terminal'}
</button>
</div>
{newCode && <p className="mt-4 rounded-lg bg-green-50 p-3 text-sm text-green-800">Código creado: <strong className="font-mono text-lg tracking-widest">{newCode}</strong></p>}
{newCode && (
<p className="mt-4 rounded-lg bg-green-50 p-3 text-sm text-green-800">
Código: <strong className="font-mono text-lg tracking-widest">{newCode}</strong>
</p>
)}
{error && <p className="mt-4 text-sm text-red-600">{error}</p>}
</form>
<div className="overflow-hidden rounded-xl border border-gray-200 bg-white">
<div className="border-b border-gray-200 px-6 py-4"><h2 className="font-bold text-gray-900">Terminales</h2></div>
{loading ? <p className="p-6 text-sm text-gray-500">Cargando</p> : terminals.length === 0 ? <p className="p-6 text-sm text-gray-500">No hay terminales.</p> : (
<section className="overflow-hidden rounded-xl border border-gray-200 bg-white">
<div className="border-b px-6 py-4">
<h2 className="font-bold">Terminales</h2>
</div>
{loading ? (
<p className="p-6 text-sm text-gray-500">Cargando</p>
) : (
<div className="overflow-x-auto">
<table className="w-full text-sm">
<thead className="bg-gray-50 text-left text-xs uppercase text-gray-500"><tr><th className="px-4 py-3">Nombre</th><th className="px-4 py-3">Tienda</th><th className="px-4 py-3">Código</th><th className="px-4 py-3">Estado</th><th className="px-4 py-3">Vinculado</th><th className="px-4 py-3">Interfaz</th></tr></thead>
<tbody className="divide-y divide-gray-100">
{terminals.map((terminal) => {
const store = stores.find((item) => item.id === terminal.storeId);
return <tr key={terminal.id}><td className="px-4 py-3 font-medium">{terminal.name}</td><td className="px-4 py-3">{store?.name ?? terminal.storeId}</td><td className="px-4 py-3"><button type="button" onClick={() => terminal.bindingCode && void copyCode(terminal.bindingCode)} disabled={!terminal.bindingCode} className="rounded-lg bg-gray-100 px-3 py-1 font-mono tracking-wider hover:bg-gray-200 disabled:opacity-40">{copied === terminal.bindingCode ? 'Copiado' : terminal.bindingCode ?? '—'}</button></td><td className="px-4 py-3 capitalize">{terminal.status}</td><td className="px-4 py-3">{terminal.boundAt ? 'Sí' : 'No'}</td><td className="px-4 py-3"><button type="button" onClick={() => openTouchConfig(terminal)} className="rounded-lg border border-[#2D6A4F] px-3 py-1.5 text-xs font-semibold text-[#2D6A4F] hover:bg-green-50">Configurar táctil</button></td></tr>;
})}
<thead className="bg-gray-50 text-left text-xs uppercase text-gray-500">
<tr>
<th className="px-4 py-3">Nombre</th>
<th className="px-4 py-3">Código</th>
<th className="px-4 py-3">Estado</th>
<th className="px-4 py-3">Vinculado</th>
<th className="px-4 py-3">Configuración</th>
</tr>
</thead>
<tbody className="divide-y">
{terminals
.filter((terminal) => terminal.storeId === storeId)
.map((terminal) => (
<tr key={terminal.id}>
<td className="px-4 py-3 font-medium">{terminal.name}</td>
<td className="px-4 py-3">
<button
type="button"
onClick={() =>
terminal.bindingCode && void copyCode(terminal.bindingCode)
}
disabled={!terminal.bindingCode}
className="rounded-lg bg-gray-100 px-3 py-1 font-mono"
>
{copied === terminal.bindingCode
? 'Copiado'
: (terminal.bindingCode ?? '—')}
</button>
</td>
<td className="px-4 py-3">{terminal.status}</td>
<td className="px-4 py-3">{terminal.boundAt ? 'Sí' : 'No'}</td>
<td className="px-4 py-3">
<button
type="button"
onClick={() => openTouchConfig(terminal)}
className="rounded-lg border border-[#2D6A4F] px-3 py-1.5 text-xs font-semibold text-[#2D6A4F]"
>
Configurar terminal
</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</div>
</section>
{configuring && (
<section className="rounded-xl border border-gray-200 bg-white p-6">
<div className="flex items-start justify-between gap-4">
<div className="flex justify-between">
<div>
<h2 className="font-bold text-gray-900">Interfaz táctil · {configuring.name}</h2>
<p className="mt-1 text-sm text-gray-500">Las categorías y subcategorías se administran en Categorías. Aquí activas su navegación en este terminal y eliges cuatro productos rápidos.</p>
<h2 className="font-bold">Interfaz táctil · {configuring.name}</h2>
<p className="mt-1 text-sm text-gray-500">
Elige hasta ocho productos rápidos y las funciones disponibles en esta caja.
</p>
</div>
<button type="button" onClick={() => setConfiguring(null)} className="text-gray-400 hover:text-gray-700" aria-label="Cerrar"></button>
<button type="button" onClick={() => setConfiguring(null)} aria-label="Cerrar">
</button>
</div>
<label className="mt-5 flex items-center gap-3 rounded-xl bg-gray-50 p-4 text-sm font-medium text-gray-800">
<input type="checkbox" checked={touchEnabled} onChange={(event) => setTouchEnabled(event.target.checked)} className="h-5 w-5 accent-[#2D6A4F]" />
Activar botones grandes categoría subcategoría producto
</label>
<div className="mt-5 grid gap-3 md:grid-cols-2">
<label className="flex items-center gap-3 rounded-xl bg-gray-50 p-4 text-sm font-medium">
<input
type="checkbox"
checked={touchEnabled}
onChange={(event) => setTouchEnabled(event.target.checked)}
className="h-5 w-5 accent-[#2D6A4F]"
/>{' '}
Navegación táctil por categorías
</label>
<label className="flex items-center gap-3 rounded-xl bg-gray-50 p-4 text-sm font-medium">
<input
type="checkbox"
checked={discountsEnabled}
onChange={(event) => setDiscountsEnabled(event.target.checked)}
className="h-5 w-5 accent-[#2D6A4F]"
/>{' '}
Permitir descuentos por línea
</label>
</div>
<p className="mt-2 text-xs text-gray-500">
Desactiva descuentos para terminales de autopago.
</p>
<div className="mt-5 grid gap-4 md:grid-cols-2 xl:grid-cols-4">
{quickSlots.map((variantId, slot) => (
<label key={slot} className="text-sm font-medium text-gray-700">
Producto rápido {slot + 1}
<select value={variantId ?? ''} onChange={(event) => setQuickSlots((current) => current.map((value, index) => index === slot ? event.target.value || null : value))} className="mt-1 w-full rounded-xl border border-gray-300 bg-white px-3 py-2.5">
<select
value={variantId ?? ''}
onChange={(event) =>
setQuickSlots((current) =>
current.map((value, index) =>
index === slot ? event.target.value || null : value,
),
)
}
className="mt-1 w-full rounded-xl border bg-white px-3 py-2.5"
>
<option value="">Sin asignar</option>
{catalogOptions.map((product) => <option key={product.variantId} value={product.variantId}>{product.name} · {product.sku}</option>)}
{catalogOptions.map((product) => (
<option key={product.variantId} value={product.variantId}>
{product.name} · {product.sku}
</option>
))}
</select>
</label>
))}
</div>
<div className="mt-5 flex items-center gap-4">
<button type="button" onClick={() => void saveTouchConfig()} disabled={savingTouch} className="rounded-xl bg-[#2D6A4F] px-5 py-2.5 text-sm font-semibold text-white disabled:opacity-50">{savingTouch ? 'Guardando…' : 'Guardar interfaz táctil'}</button>
{touchMessage && <p className={`text-sm ${touchMessage.includes('guardada') ? 'text-green-700' : 'text-red-600'}`}>{touchMessage}</p>}
<button
type="button"
onClick={() => void saveTouchConfig()}
disabled={savingTouch}
className="rounded-xl bg-[#2D6A4F] px-5 py-2.5 text-sm font-semibold text-white disabled:opacity-50"
>
{savingTouch ? 'Guardando…' : 'Guardar terminal'}
</button>
{touchMessage && <p className="text-sm">{touchMessage}</p>}
</div>
</section>
)}
<section className="rounded-xl border border-gray-200 bg-white p-6">
<div>
<h2 className="font-bold">Formas de pago</h2>
<p className="mt-1 text-sm text-gray-500">
Crea Bizum, Stripe, Apple Pay u otro método. Tarjeta y otro se registran
manualmente; no activan una pasarela externa.
</p>
</div>
<div className="mt-4 grid gap-3 md:grid-cols-2 xl:grid-cols-3">
{paymentMethods.map((method) => (
<div
key={method.id}
className={`flex items-center justify-between rounded-xl border p-4 ${method.active ? 'border-green-200 bg-green-50' : 'border-gray-200 bg-gray-50 opacity-70'}`}
>
<div>
<strong className="block">{method.label}</strong>
<span className="text-xs text-gray-500">
{method.code} · {method.kind}
</span>
</div>
<button
type="button"
onClick={() => void togglePaymentMethod(method)}
className="rounded-lg border bg-white px-3 py-2 text-xs font-semibold"
>
{method.active ? 'Desactivar' : 'Activar'}
</button>
</div>
))}
</div>
<form
onSubmit={createPaymentMethod}
className="mt-5 grid gap-3 rounded-xl bg-gray-50 p-4 md:grid-cols-[1fr_1fr_1fr_auto] md:items-end"
>
<label className="text-sm font-medium">
Código
<input
value={newMethod.code}
onChange={(event) =>
setNewMethod({
...newMethod,
code: event.target.value.toLowerCase().replace(/[^a-z0-9_-]/g, ''),
})
}
required
maxLength={32}
placeholder="bizum"
className="mt-1 w-full rounded-xl border px-3 py-2.5"
/>
</label>
<label className="text-sm font-medium">
Nombre visible
<input
value={newMethod.label}
onChange={(event) => setNewMethod({ ...newMethod, label: event.target.value })}
required
maxLength={64}
placeholder="Bizum"
className="mt-1 w-full rounded-xl border px-3 py-2.5"
/>
</label>
<label className="text-sm font-medium">
Tipo
<select
value={newMethod.kind}
onChange={(event) =>
setNewMethod({ ...newMethod, kind: event.target.value as PaymentMethod['kind'] })
}
className="mt-1 w-full rounded-xl border bg-white px-3 py-2.5"
>
<option value="cash">Efectivo</option>
<option value="card">Tarjeta</option>
<option value="other">Otro</option>
</select>
</label>
<button
disabled={savingMethod}
className="rounded-xl bg-[#2D6A4F] px-5 py-2.5 text-sm font-semibold text-white disabled:opacity-50"
>
Añadir
</button>
</form>
{paymentMessage && <p className="mt-3 text-sm">{paymentMessage}</p>}
</section>
<form onSubmit={saveReceipt} className="rounded-xl border border-gray-200 bg-white p-6">
<div>
<h2 className="font-bold">Empresa, numeración y política del ticket</h2>
<p className="mt-1 text-sm text-gray-500">
Estos datos se congelan en cada ticket emitido.
</p>
</div>
<div className="mt-5 grid gap-4 md:grid-cols-2 xl:grid-cols-3">
<Field
label="Razón social / nombre"
value={receipt.name}
onChange={(value) => setReceipt({ ...receipt, name: value })}
required
/>
<Field
label="NIF / CIF"
value={receipt.taxId}
onChange={(value) => setReceipt({ ...receipt, taxId: value })}
/>
<Field
label="Dirección"
value={receipt.address}
onChange={(value) => setReceipt({ ...receipt, address: value })}
/>
<Field
label="Email"
type="email"
value={receipt.contactEmail}
onChange={(value) => setReceipt({ ...receipt, contactEmail: value })}
/>
<Field
label="Teléfono"
value={receipt.contactPhone}
onChange={(value) => setReceipt({ ...receipt, contactPhone: value })}
/>
<Field
label="Cabecera"
value={receipt.receiptHeader}
onChange={(value) => setReceipt({ ...receipt, receiptHeader: value })}
/>
<Field
label="Pie"
value={receipt.receiptFooter}
onChange={(value) => setReceipt({ ...receipt, receiptFooter: value })}
/>
<Field
label="Prefijo de ticket"
value={receipt.prefix}
onChange={(value) =>
setReceipt({ ...receipt, prefix: value.toUpperCase().replace(/[^A-Z0-9_-]/g, '') })
}
required
/>
<Field
label="Próximo número"
type="number"
value={String(receipt.nextNumber)}
onChange={(value) => setReceipt({ ...receipt, nextNumber: Number(value) })}
required
min={1}
/>
<Field
label="Dígitos (relleno)"
type="number"
value={String(receipt.padding)}
onChange={(value) => setReceipt({ ...receipt, padding: Number(value) })}
required
min={1}
max={12}
/>
</div>
<label className="mt-4 block text-sm font-medium">
Política de devolución
<textarea
value={receipt.returnPolicy}
onChange={(event) => setReceipt({ ...receipt, returnPolicy: event.target.value })}
required
maxLength={2000}
rows={3}
className="mt-1 w-full rounded-xl border px-3 py-2.5"
/>
</label>
<div className="mt-5 flex items-center gap-4">
<button
disabled={savingReceipt}
className="rounded-xl bg-[#2D6A4F] px-5 py-2.5 text-sm font-semibold text-white disabled:opacity-50"
>
{savingReceipt ? 'Guardando…' : 'Guardar ticket'}
</button>
{receiptMessage && <p className="text-sm">{receiptMessage}</p>}
</div>
</form>
</div>
);
}
function Field({
label,
value,
onChange,
type = 'text',
required = false,
min,
max,
}: {
label: string;
value: string;
onChange: (value: string) => void;
type?: string;
required?: boolean;
min?: number;
max?: number;
}) {
return (
<label className="text-sm font-medium text-gray-700">
{label}
<input
type={type}
value={value}
onChange={(event) => onChange(event.target.value)}
required={required}
min={min}
max={max}
className="mt-1 w-full rounded-xl border border-gray-300 px-3 py-2.5"
/>
</label>
);
}