import { createOperationKey } from '@/lib/operation-key'; import { router } from '@inertiajs/react'; import { useState } from 'react'; import { Management, Field, control, button } from './shared'; type Plan = { level: number; expected: number; resolved: number; pending: number; hash: string; scan_ready: boolean; source_sha256: string; correspondence_version: string; rows: { external_id: string; name: string; parent_name: string | null; location_id: string | null; status: string; correspondence: { target_name: string; target_source_code: string; basis: string; } | null; }[]; }; export default function GeometryImport({ plan }: { plan: Plan }) { const [reason, setReason] = useState(''); const [confirmed, setConfirmed] = useState(false); const [busy, setBusy] = useState(false); const [message, setMessage] = useState(''); const [batch, setBatch] = useState(''); const [applyKey, setApplyKey] = useState(() => createOperationKey()); const [reverseKey, setReverseKey] = useState(() => createOperationKey()); const execute = async (reverse: boolean) => { setBusy(true); try { const cookie = document.cookie .split('; ') .find((v) => v.startsWith('XSRF-TOKEN=')) ?.slice(11); const response = await fetch( `/management/territory-geometry-import${reverse ? `/${batch}/reverse` : ''}`, { method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json', 'X-XSRF-TOKEN': decodeURIComponent(cookie ?? ''), }, body: JSON.stringify({ level: plan.level, hash: plan.hash, reason, confirm: confirmed, idempotency_key: reverse ? reverseKey : applyKey, }), }, ); const result = await response.json(); if (!response.ok) throw new Error( result.message ?? 'Operação recusada. Reveja o plano e a autenticação.', ); if (!reverse) setBatch(result.batch_id); setMessage( reverse ? `Revertidas ${result.reversed}; preservadas ${result.reused_preserved} geometrias reutilizadas.` : `Criadas ${result.inserted}; reutilizadas ${result.reused}; pendentes ${result.pending}. Lote ${result.batch_id}. Nenhuma camada publicada.`, ); } catch (e) { setMessage(e instanceof Error ? e.message : 'Erro de ligação.'); } finally { setBusy(false); } }; return (

Exemplar INE de 07/06/2019 aceite. Correspondência exata de nome e pai, ou correspondência explícita fundamentada abaixo; sem alterações de classificações territoriais, disponibilidade fundiária ou cálculos.

Esperado: {plan.expected}; resolvido: {plan.resolved}; pendente:{' '} {plan.pending}. Scan:{' '} {plan.scan_ready ? 'limpo e atual' : 'pendente ou inconclusivo'} .

SHA-256 do original: {plan.source_sha256}

Versão das correspondências: {plan.correspondence_version}.

{plan.rows.map((r) => ( ))}
Identificador Nome Pai Correspondência Fundamento Ação prevista
{r.external_id} {r.name} {r.parent_name ?? '—'} {r.location_id ?? 'Pendente'} {r.correspondence ? ( <> {r.correspondence.target_name}{' '} (código{' '} { r.correspondence .target_source_code } )

{r.correspondence.basis}

) : ( 'Nome único sob o pai correspondido' )}
{r.status}