import { Head, Link, useForm } from '@inertiajs/react'; import { useEffect, useRef } from 'react'; import { Button } from '@/components/ui/button'; import { FormErrors, PageTitle, Panel, controlClass, } from '@/components/procurement'; type RecordVersion = { id: number; version: number; state: string; reason: string; data: { code?: string; proposed_code?: string | null; confirmed_code?: string; source_reference?: string | null; confirmation_source?: string; deviation?: boolean; requisition_item_id?: number; }; snapshot: { item?: { name?: string; description?: string }; confirmed_by?: { name: string }; master?: { id: number; version: number; item: { code: string; name: string }; }; }; }; type Page = { data: T[]; prev_page_url: string | null; next_page_url: string | null; }; type Props = { requisition: { id: number; number: string }; items: { id: number; description: string; quantity: string; unit: string; }[]; latest: Record | RecordVersion[]; history: Page; masters: Page; stockItems: Page<{ id: number; code: string; name: string; unit: string; tariff: RecordVersion | null; }>; canPropose: boolean; canConfirm: boolean; masterSearch: string; actorId: number; tenantId: number; }; function useGuard(dirty: boolean) { const ref = useRef(null); useEffect(() => { const form = ref.current; window.dispatchEvent( new CustomEvent('sgc:context-dirty', { detail: { form, dirty } }), ); return () => { window.dispatchEvent( new CustomEvent('sgc:context-dirty', { detail: { form, dirty: false }, }), ); }; }, [dirty]); return ref; } function Pagination({ page }: { page: Page }) { return ( ); } function ItemForm({ item, latest, props, scope, url, }: { item: Props['items'][number]; latest?: RecordVersion; props: Props; scope: string; url: string; }) { const form = useForm(`${scope}:item:${item.id}:${latest?.version ?? 0}`, { operation_key: crypto.randomUUID(), action: 'propose', version: latest?.version ?? 0, requisition_item_id: item.id, master_id: '', code: latest?.data.confirmed_code ?? latest?.data.proposed_code ?? '', source_reference: latest?.data.confirmation_source ?? latest?.data.source_reference ?? '', reason: '', }); const ref = useGuard(form.isDirty); const submit = (action: string) => { form.transform((data) => ({ ...data, action, master_id: action === 'confirm' ? null : data.master_id || null, })); form.post(url, { preserveScroll: true, onSuccess: () => { form.reset(); }, }); }; return (

{latest?.state === 'confirmed' ? `Confirmado: ${latest.data.confirmed_code} · ${latest.snapshot.confirmed_by?.name ?? 'Comprador registado'}` : 'Código pautal por confirmar pelo comprador.'}

{latest?.snapshot.master && (

Proposta reutilizada de {latest.snapshot.master.item.code} ·{' '} {latest.snapshot.master.item.name}, versão{' '} {latest.snapshot.master.version}.

)} {(props.canPropose || props.canConfirm) && (
{ e.preventDefault(); submit('propose'); }} > {props.canPropose && ( )}