import { useState } from 'react'; import { useMutation, useQueryClient } from '@tanstack/react-query'; import { Alert, Button, Descriptions, Divider, Form, InputNumber, Space, Spin, Steps, Table, Tag, Typography, Input, } from 'antd'; import type { TableColumnsType } from 'antd'; import { DeleteOutlined, PlusOutlined } from '@ant-design/icons'; import { Link, useNavigate, useSearch } from '@tanstack/react-router'; import type { CreatePedido, CreatePedidoItem, ProdutoSummary } from '@sar/api-interface'; import { useClientDetail } from '../../lib/queries/clients'; import { useCatalog } from '../../lib/queries/catalog'; import { apiFetch } from '../../lib/api-client'; const { Title, Text } = Typography; const { Search } = Input; type CartItem = CreatePedidoItem & { key: string }; function calcItemTotal(qty: number, price: number, disc: number): number { return Math.round(qty * price * (1 - disc / 100) * 100) / 100; } function fmt(n: number): string { return n.toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' }); } // ─── Step 1 — Selecionar Produtos ──────────────────────────────────────────── function ProductStep({ cart, onAdd, onRemove, onQtyChange, onDiscChange, }: { cart: CartItem[]; onAdd: (p: ProdutoSummary) => void; onRemove: (key: string) => void; onQtyChange: (key: string, qty: number) => void; onDiscChange: (key: string, disc: number) => void; }) { const [q, setQ] = useState(''); const { data, isLoading } = useCatalog({ q: q || undefined, limit: 20 }); const cartKeys = new Set(cart.map((c) => String(c.idProduto))); const catalogColumns: TableColumnsType = [ { title: 'Código', dataIndex: 'codigo', width: 100 }, { title: 'Produto', dataIndex: 'descricao', ellipsis: true }, { title: 'Grupo', dataIndex: 'grupo', width: 110, render: (v: string | null) => (v ? {v} : null), }, { title: 'Preço', dataIndex: 'vlPreco1', width: 110, align: 'right', render: (v: string) => fmt(Number(v)), }, { title: '', width: 80, render: (_: unknown, row: ProdutoSummary) => ( ), }, ]; const cartColumns: TableColumnsType = [ { title: 'Produto', dataIndex: 'descProduto', ellipsis: true }, { title: 'Qtd', dataIndex: 'qtd', width: 100, render: (v: number, row: CartItem) => ( onQtyChange(row.key, n ?? 1)} /> ), }, { title: 'Desc %', dataIndex: 'descontoPerc', width: 100, render: (v: number, row: CartItem) => ( onDiscChange(row.key, n ?? 0)} /> ), }, { title: 'Subtotal', width: 120, align: 'right', render: (_: unknown, row: CartItem) => fmt(calcItemTotal(row.qtd, row.precoUnitario, row.descontoPerc)), }, { title: '', width: 40, render: (_: unknown, row: CartItem) => ( } {step < 2 && ( )} {step === 2 && ( )} ); }