Files
Investidor-V2/resources/js/components/market-analysis-plan.tsx
T

154 lines
5.5 KiB
TypeScript

import type { BusinessFinancialPlanInput } from '@/lib/business-financial-plan';
import {
buildComplementaryTables,
isMarketAnalysis,
} from '@/lib/complementary-report';
import { InvestmentReportTables } from './investment-report-tables';
export function MarketAnalysisPlan({
plan,
onChange,
}: {
plan: BusinessFinancialPlanInput;
onChange: (p: BusinessFinancialPlanInput) => void;
}) {
const a = plan.marketAnalysis ?? {
baseYear: plan.reportAssumptions?.startYear ?? '',
years: '3',
segments: [],
};
if (!isMarketAnalysis(a))
return (
<p role="alert">
A análise de mercado guardada tem uma estrutura desconhecida.
Foi preservada; reveja o ficheiro antes de editar.
</p>
);
const update = (patch: Partial<typeof a>) =>
onChange({ ...plan, marketAnalysis: { ...a, ...patch } });
const input = (
label: string,
value: string,
change: (v: string) => void,
type = 'text',
) => (
<label className="grid min-w-0 gap-1 text-sm">
{label}
<input
className="min-w-0 rounded border p-2"
type={type}
step={type === 'number' ? 'any' : undefined}
value={value}
onChange={(e) => change(e.target.value)}
/>
</label>
);
return (
<details className="my-4 rounded border p-3">
<summary>Análise de Mercado — segmentos e hipóteses</summary>
<p className="my-2 text-sm">
Obrigatório para completar esta tabela: ano-base, horizonte de 2
a 10 anos e, por segmento, nome, clientes potenciais inteiros,
crescimento anual e fonte ou hipótese identificada. Não são
vendas nem procura confirmada. O horizonte sugerido de três anos
é editável; não representa dados económicos.
</p>
<div className="grid gap-3 sm:grid-cols-2">
{input(
'Ano-base de mercado',
a.baseYear,
(v) => update({ baseYear: v }),
'number',
)}
{input(
'Horizonte de mercado (anos)',
a.years,
(v) => update({ years: v }),
'number',
)}
</div>
{a.segments.map((s, i) => (
<fieldset
key={i}
className="my-3 grid gap-3 rounded border p-3 sm:grid-cols-2"
>
<legend>Segmento {i + 1}</legend>
{(
[
['label', 'Nome do segmento', 'text'],
[
'customers',
'Clientes potenciais no ano-base',
'number',
],
[
'growthPercent',
'Crescimento anual do segmento (%)',
'number',
],
[
'basis',
'Fonte e data, ou hipótese ilustrativa',
'text',
],
] as const
).map(([key, label, type]) => (
<div className="min-w-0" key={key}>
{input(
label,
s[key],
(v) =>
update({
segments: a.segments.map((r, j) =>
j === i ? { ...r, [key]: v } : r,
),
}),
type,
)}
</div>
))}
<button
type="button"
className="rounded border p-2"
onClick={() =>
update({
segments: a.segments.filter((_, j) => j !== i),
})
}
>
Remover segmento {i + 1}
</button>
</fieldset>
))}
<button
type="button"
className="my-2 rounded border p-2"
disabled={a.segments.length >= 100}
onClick={() =>
update({
segments: [
...a.segments,
{
label: '',
customers: '',
growthPercent: '',
basis: '',
},
],
})
}
>
Adicionar segmento de mercado
</button>
<InvestmentReportTables
tables={[
buildComplementaryTables(
{},
{ ...plan, marketAnalysis: a },
)[0],
]}
/>
</details>
);
}