import { Link } from '@inertiajs/react'; import { contextApi, type ContextReference } from '@/lib/investor-context'; import { TerritoryFields } from './investment-territory'; import type { OpportunityChoice } from '@/lib/investor-context'; import { card, input } from '@/pages/investment/shared'; import { useEffect, useState } from 'react'; import type { TerritoryOption } from './investment-territory'; export type CorridorOption = { key: string; name: string; provinces: TerritoryOption[]; }; function ChoiceCard({ c, i, corridors, disabled, update, remove, contextId, }: { c: OpportunityChoice; i: number; corridors: CorridorOption[]; disabled: boolean; update: (change: Partial) => void; remove?: () => void; contextId?: string; }) { const [notice, setNotice] = useState(''); const [parks, setParks] = useState< { id: string; name: string; version_no: number }[] >([]); const [parkError, setParkError] = useState(''); const [parkLoading, setParkLoading] = useState(false); const corridor = corridors.find((r) => r.key === c.corridor_key) || corridors.find((r) => r.name === c.coverage); const path = c.territory_path ?? []; function location(next: TerritoryOption[], key = corridor?.key) { update({ corridor_key: key, territory_path: next, territory: next.length ? { key: 'territory:' + next.at(-1)!.id, kind: 'territory', id: next.at(-1)!.id, title: next.map((p) => p.name).join(' / '), href: '/oportunidades', version: null, sha256: '', fields: {}, } : null, }); } useEffect(() => { if (!remove || !corridor) return; if ( path.length && !corridor.provinces.some((p) => p.id === path[0].id) ) { setNotice( 'A localização anterior foi retirada porque não pertence ao corredor escolhido. Escolha uma localização compatível.', ); location(corridor.provinces.length === 1 ? corridor.provinces : []); } else if (!path.length && corridor.provinces.length === 1) { setNotice( 'Província seleccionada automaticamente: ' + corridor.provinces[0].name + '.', ); location(corridor.provinces); } }, [ corridor?.key, JSON.stringify(corridor?.provinces), path[0]?.id, !!remove, ]); useEffect(() => { if (!remove || !corridor?.key) return; let active = true; setParkLoading(true); setParkError(''); const params = new URLSearchParams(); if (corridor?.key) params.set('corridor', corridor.key); if (c.territory?.id) params.set('territory', c.territory.id); void contextApi<{ parks: { id: string; name: string; version_no: number }[]; }>('/investimentos/opcoes-corredor?' + params) .then((data) => { if (!active) return; setParks(data.parks); if ( c.park && !data.parks.some((p) => 'park:' + p.id === c.park?.key) ) { update({ park: null }); setNotice( 'A zona ou parque desta oportunidade foi retirado por incompatibilidade com a localização escolhida. As restantes oportunidades conservaram-se.', ); } }) .catch((e) => { if (active) setParkError(e.message); }) .finally(() => { if (active) setParkLoading(false); }); return () => { active = false; }; }, [corridor?.key, c.territory?.id, !!remove]); return (

{i + 1}. {c.opportunity.title}

{remove && ( )} {!remove && (

Corredor escolhido: {c.corridor_name ?? 'Por definir'}

)} {!remove && c.coverage && c.coverage !== c.corridor_name && (

Referência documental de origem: {c.coverage}

)} {notice &&

{notice}

}

Localização: {c.territory?.title ?? 'Por escolher'}

{!!c.portfolio_origins?.length && (

Associações escolhidas

    {c.portfolio_origins.map((origin) => (
  • {origin.title} {remove && ( )}
  • ))}
)} {c.park && !remove && (

Zona/parque:{' '} {c.park.title}

)} {remove && ( <> {parkError &&

{parkError}

} {!parkLoading && !parkError && (

{!c.territory ? 'Escolha a localização desta oportunidade.' : parks.length ? 'A consulta não confirma disponibilidade, admissão ou incentivos.' : 'Sem zona ou parque com enquadramento documentado nesta localização.'}

)}
Actividades que pretende realizar nesta oportunidade {Object.entries({ production: 'Produção agrícola', processing: 'Transformação agroindustrial', trade: 'Comercialização e exportação', transport: 'Transporte e logística', water: 'Captação/utilização de água', forestry: 'Actividade florestal', animal: 'Produção/serviços pecuários', }).map(([key, label]) => ( ))}
location(next)} />