// Order detail + confirmation flow with inline WhatsApp composer function OrderDetailScreen({ pedido, onClose, onMarkEnviado, onMarkCompletado, onDownload, onSendWA, onOpenFactura }) { const T = window.T; const { Icon, ProductTile, StatusPill } = window; const [mode, setMode] = React.useState('detail'); // detail | confirm | whatsapp const [waNumber, setWaNumber] = React.useState(''); const total = pedido.productos.reduce((s, x) => s + x.precio * x.cantidad, 0); const items = pedido.productos.reduce((s, x) => s + x.cantidad, 0); // Look up the factura created for this pedido (matched by pedidoId) // Lee facturas de Supabase vía useBilling, con fallback a loadBilling local const billing = window.useBilling ? window.useBilling(pedido?.vendedoraId || (pedido && pedido._remote ? null : null)) : { state: { facturas: [] }, loading: false }; const factura = React.useMemo(() => { try { const list = billing.state.facturas; const local = list.find(f => f.pedidoId === pedido.id || f.pedidoId === pedido.codigo); if (local) return local; // fallback a loadBilling local const st = window.loadBilling ? window.loadBilling() : { facturas: [] }; return st.facturas.find(f => f.pedidoId === pedido.id || f.pedidoId === pedido.codigo) || null; } catch { return null; } }, [pedido]); // Timeline steps const steps = [ { key: 'creado', label: 'Creado', done: true, time: pedido.fechaCreacion }, { key: 'enviado', label: 'Enviado a sistema', done: pedido.enviado, time: pedido.enviado ? pedido.fechaCreacion : null }, { key: 'completado', label: 'Completado', done: pedido.estado === 'completado', time: pedido.fechaEntrega || null }, ]; const message = React.useMemo(() => ( `Hola Inovabot, pedido listo.\n\n` + `*Pedido ${pedido.codigo}*\n` + `Vendedora: ${pedido.vendedora || 'María Pérez'} · ${pedido.zona || 'Valencia'}\n\n` + `*Productos:*\n` + pedido.productos.map(p => `• ${p.nombre} (${p.catalogo}) x${p.cantidad} — $${(p.precio * p.cantidad).toFixed(2)}`).join('\n') + `\n\n*Total: $${total.toFixed(2)}*` ), [pedido, total]); return (
{/* Header */}
{/* Title block */}
{pedido.codigo}
${total.toFixed(2)}
{items} ítems · {pedido.fechaCreacion.toLocaleDateString('es', { day: 'numeric', month: 'long', hour: '2-digit', minute: '2-digit' })}
{/* Body scroll */}
{/* Factura banner */} {factura && (
)} {/* Timeline */}
Estado
{steps.map((s, i) => (
{s.label}
{s.time && (
{s.time.toLocaleDateString('es', { day: '2-digit', month: 'short', hour: '2-digit', minute: '2-digit' })}
)}
))}
{/* Products */}
{pedido.productos.length} productos
{pedido.productos.map((p, i) => (
0 ? `1px solid ${T.hairline}` : 'none', }}>
{p.codigo} · {p.catalogo}
{p.nombre}
${p.precio.toFixed(2)} × {p.cantidad}
${(p.precio * p.cantidad).toFixed(2)}
))}
Total ${total.toFixed(2)}
{/* Bottom action bar */}
{/* WhatsApp composer sheet */} {mode === 'whatsapp' && (
setMode('detail')}>
e.stopPropagation()} style={{ width: '100%', background: T.paper, borderRadius: '28px 28px 0 0', padding: '12px 24px 32px', maxHeight: '82%', overflow: 'auto', }}>
Paso final
Enviar por WhatsApp
{/* message preview */}
{message}
Número destino
setWaNumber(e.target.value.replace(/\D/g, ''))} placeholder="04121234567" style={{ width: '100%', padding: '14px 16px', borderRadius: 14, border: `1px solid ${T.hairline}`, background: T.paper, fontFamily: T.mono, fontSize: 14, letterSpacing: 1, color: T.ink, outline: 'none', boxSizing: 'border-box', marginBottom: 10, }} />
{['04141234567', '04126655432'].map(n => ( ))}
{ onSendWA && onSendWA(pedido.id); setMode('detail'); }} icon={} > Abrir WhatsApp
)}
); } // Confirmation screen shown when creating a NEW order function ConfirmCreateScreen({ draft, vendedora, onConfirm, onBack }) { const T = window.T; const { Icon, ProductTile } = window; const total = draft.reduce((s, p) => s + p.precio * p.cantidad, 0); const items = draft.reduce((s, p) => s + p.cantidad, 0); const [sending, setSending] = React.useState(false); const handleConfirm = () => { setSending(true); setTimeout(() => onConfirm(), 500); }; return (
Confirmar
Revisa tu
pedido.
{/* vendedora card */}
Vendedora
{vendedora?.nombre || 'María Pérez'}
{vendedora?.zona} · {vendedora?.telefono}
{/* products */}
{draft.map((p, i) => (
0 ? `1px solid ${T.hairline}` : 'none', }}>
{p.nombre}
{p.codigo} · {p.catalogo} · ×{p.cantidad}
${(p.precio * p.cantidad).toFixed(2)}
))}
Total · {items} ítems ${total.toFixed(2)}
{/* verify notice */}
Verifica los códigos.
Los errores en código son responsabilidad de la vendedora.
} > {sending ? 'Confirmando…' : `Confirmar pedido · $${total.toFixed(2)}`}
); } Object.assign(window, { OrderDetailScreen, ConfirmCreateScreen });