import { Head, Link, useForm } from '@inertiajs/react'; import { useEffect, useRef } from 'react'; import { FormErrors, PageTitle, Panel, controlClass, } from '@/components/procurement'; import { Button } from '@/components/ui/button'; import LegalChargeFields, { LegalChargeSummary, chargeDrafts, type LegalCharge, type LegalChargeDraft, } from '@/components/import-legal-charges'; import { estimateMinor, estimateMoney } from '@/lib/import-estimate-money'; import { date, money } from '@/types/procurement'; type LegalData = { title: string; official_reference: string; official_url: string | null; citation: string; valid_from: string; valid_until: string | null; match_code: string; suggested_code: string; origin_country: string | null; sector: string | null; obligations: string; document_types: string[]; charges?: LegalCharge[]; }; type LegalRule = { id: number; record_key: string; version: number; state: string; data: LegalData; reason: string; source_url: string; }; type Page = { data: T[]; prev_page_url: string | null; next_page_url: string | null; }; type Props = { sources?: Page<{ id: number; data: { official_reference: string; official_url: string | null }; }>; requisition: { id: number; number: string }; rules: Page; history: Page; suggestions: { id: number; version: number; data: LegalData; source_sha256: string; }[]; items: { id: number; description: string }[]; classifications: { version: number; data: { requisition_item_id: number }; }[]; canManage: boolean; estimates?: { id: number; version: number; state: string; data: { requisition_item_id: number; bases: Record< string, { value_minor: number | null; quantity: string | null; source: string | null; } >; }; snapshot: { legal_rule: { id: number; context: { code: string; origin_country: string; sector: string; date: string; }; }; item: { description: string }; result: { total_minor: number | null; known_subtotal_minor: number; lines: { key: string; label: string; amount_minor: number | null; }[]; }; }; }[]; estimateHistory?: Page<{ id: number; version: number; state: string; reason: string; }>; canPropose: boolean; actorId: number; tenantId: number; types: Record; filters: { code?: string; origin?: string; sector?: string; date: string }; }; const states: Record = { draft: 'Em preparação', published: 'Publicada pela área aduaneira', withdrawn: 'Retirada de novas sugestões', }; function useDirtyForm(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 Pages({ page }: { page: Page }) { return ( ); } function Proposal({ rule, props, }: { rule: Props['suggestions'][number]; props: Props; }) { const form = useForm( `legal-proposal:${props.tenantId}:${props.actorId}:${props.requisition.id}:${rule.id}:${JSON.stringify(props.filters)}`, { operation_key: '', requisition_item_id: '', reason: '', version: 0 }, ); const ref = useDirtyForm(form.isDirty); return (
{ event.preventDefault(); const operation = form.data.operation_key || crypto.randomUUID(); form.setData('operation_key', operation); form.transform((data) => ({ ...data, operation_key: operation, action: 'propose', code: rule.data.suggested_code, source_reference: `${rule.data.official_reference} — ${rule.data.citation}`, legal_rule_id: rule.id, legal_match_code: props.filters.code, legal_origin: props.filters.origin ?? '', legal_sector: props.filters.sector ?? '', legal_date: props.filters.date, })); form.post( `/requisicoes/${props.requisition.id}/classificacao-pautal`, { onSuccess: () => form.reset() }, ); }} >