import { useState } from 'react'; import { useMutation, useQueryClient } from '@tanstack/react-query'; import { Alert, Badge, Button, Descriptions, Divider, Form, InputNumber, Modal, Space, Spin, Table, Tag, Timeline, Typography, Input, message, } from 'antd'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faShareNodes } from '@fortawesome/free-solid-svg-icons'; import { FilePdfOutlined } from '@ant-design/icons'; import type { TableColumnsType } from 'antd'; import { Link, useParams, useNavigate } from '@tanstack/react-router'; import type { PedidoItem, HistoricoPedido } from '@sar/api-interface'; import { SITUA_LABEL } from '@sar/api-interface'; import { useOrderDetail } from '../../lib/queries/orders'; import { useClientOrders } from '../../lib/queries/orders'; import { apiFetch } from '../../lib/api-client'; import { authStore } from '../../lib/auth-store'; const { Title, Text } = Typography; const { TextArea } = Input; // ─── Helpers ────────────────────────────────────────────────────────────────── const SITUA_COLOR: Record = { 0: 'default', 1: 'warning', 2: 'processing', 3: 'error', 4: 'success', }; function fmt(v: string | number): string { return Number(v).toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' }); } function buildShareText(order: { numPedSar: string; idCliente: number; total: string; itens: Array<{ descProduto: string | null; qtd: string; precoUnitario: string }>; }): string { const lines = [ `*Pedido ${order.numPedSar} — Cliente ${order.idCliente}*`, '', ...order.itens.map( (it) => `• ${it.descProduto ?? '?'} × ${Number(it.qtd).toLocaleString('pt-BR')} — ${fmt(it.precoUnitario)} un.`, ), '', `*Total: ${fmt(order.total)}*`, ]; return lines.join('\n'); } function getRoleFromToken(): string { const token = authStore.get(); if (!token) return 'rep'; try { const payload = JSON.parse(atob(token.split('.')[1] ?? '')); return (payload.role as string) ?? 'rep'; } catch { return 'rep'; } } // ─── Subcomponents ──────────────────────────────────────────────────────────── const itemColumns: TableColumnsType = [ { title: 'Código', dataIndex: 'codProduto', width: 100 }, { title: 'Produto', dataIndex: 'descProduto', ellipsis: true }, { title: 'Qtd', dataIndex: 'qtd', width: 90, align: 'right' }, { title: 'Preço Unit.', dataIndex: 'precoUnitario', width: 120, align: 'right', render: (v: string) => fmt(v), }, { title: 'Desc %', dataIndex: 'descontoPerc', width: 80, align: 'right', render: (v: string) => `${v}%`, }, { title: 'Total', dataIndex: 'total', width: 130, align: 'right', render: (v: string) => fmt(v), }, ]; function HistoryTimeline({ history }: { history: HistoricoPedido[] }) { return ( ({ color: SITUA_COLOR[h.situaNova] === 'success' ? 'green' : SITUA_COLOR[h.situaNova] === 'warning' ? 'orange' : SITUA_COLOR[h.situaNova] === 'error' ? 'red' : 'blue', children: (
{SITUA_LABEL[h.situaNova] ?? String(h.situaNova)} {h.situaAnterior != null && ( {' '} (de {SITUA_LABEL[h.situaAnterior] ?? String(h.situaAnterior)}) )}
{new Date(h.changedAt).toLocaleString('pt-BR')} — cod. {h.changedBy} {h.nota && (
"{h.nota}"
)}
), }))} /> ); } // ─── Approve Modal ──────────────────────────────────────────────────────────── function ApproveModal({ open, originalDiscount, onConfirm, onCancel, loading, }: { open: boolean; originalDiscount: string; onConfirm: (descontoPerc?: number, nota?: string) => void; onCancel: () => void; loading: boolean; }) { const [disc, setDisc] = useState(null); const [nota, setNota] = useState(''); return ( onConfirm(disc ?? undefined, nota || undefined)} onCancel={onCancel} okText="Confirmar Aprovação" cancelText="Voltar" confirmLoading={loading} >
setDisc(v)} addonAfter="%" style={{ width: 160 }} />