import { useCrmTaskCreation } from '@/lib/crm-task-creation'; import type { TaskCreationContext } from '@/lib/crm-task-creation'; import type { Lead, Owner } from '@/pages/crm/types'; import { Field, button, control } from '@/pages/management/shared'; import { useState } from 'react'; export default function CrmTaskCreation({ lead, owners, context, }: { lead: Lead; owners: Owner[]; context: TaskCreationContext; }) { const edit = useCrmTaskCreation(lead, context); const { form } = edit; const [showPreparation, setShowPreparation] = useState(false); const preparation = edit.preparation; const labels = { title: 'Tarefa', due_at: 'Prazo', assignee_user_id: 'Responsável pela tarefa', }; function display(field: keyof typeof labels, value: string) { if (!value) return 'Por preencher'; if (field === 'assignee_user_id') return ( owners.find((owner) => String(owner.id) === value)?.name ?? 'Responsável anterior indisponível' ); if (field === 'due_at') { const date = new Date(value); return Number.isNaN(date.getTime()) ? 'Prazo por confirmar' : new Intl.DateTimeFormat('pt-MZ', { dateStyle: 'medium', timeStyle: 'short', timeZone: 'Africa/Maputo', }).format(date) + ' (Maputo)'; } return value; } const missing = form.data.assignee_user_id && !owners.some( (owner) => String(owner.id) === form.data.assignee_user_id, ); const sentDate = edit.sent ? new Date(edit.sent.due_at) : null; const sentDeadline = sentDate && !Number.isNaN(sentDate.getTime()) ? new Intl.DateTimeFormat('pt-MZ', { dateStyle: 'medium', timeStyle: 'short', timeZone: 'Africa/Maputo', }).format(sentDate) + ' (Maputo)' : 'Prazo por confirmar'; return (
{ e.preventDefault(); edit.submit(); }} >

Registar próxima acção

{context.preparation && (

{preparation.message || (context.preparation.installed ? 'A preparação acompanha as alterações e pode ser retomada nesta conta.' : 'A recuperação persistente ainda não está instalada. A criação manual continua disponível nesta página.')}

{preparation.comparison && ( <> {showPreparation && preparation.comparison.snapshot && ( <>

Compare antes de adoptar. As alterações desta página permanecem até confirmar as suas escolhas.

{( Object.keys( labels, ) as (keyof typeof labels)[] ).map((field) => (

{labels[field]}

Nesta página:{' '} {display( field, preparation.current .values[field], )}

Guardado:{' '} {display( field, preparation.comparison ?.snapshot?.values[ field ] ?? '', )}

))} {preparation.comparison.snapshot .pending && (

Existe uma tentativa por confirmar. A recuperação conserva os seus dados e não cria a tarefa automaticamente.

)} {!!preparation.unresolved.length && (

Existem alterações posteriores à escolha. Reveja esses campos antes de retomar.

)} )} )} {preparation.enabled && preparation.state === 'error' && ( )} {preparation.enabled && !preparation.comparison && ['error', 'unavailable'].includes( preparation.state, ) && ( )}
)} {edit.notice &&

{edit.notice}

} {edit.waiting && (

A recuperação repete os dados da tentativa:{' '} {edit.sent?.title}; {sentDeadline};{' '} {owners.find( (owner) => String(owner.id) === edit.sent?.assignee_user_id, )?.name ?? 'Responsável do envio anterior'} . As edições posteriores ficam conservadas para a próxima tarefa.

)} edit.change('title', e.target.value)} /> edit.change('due_at', e.target.value)} /> {Object.keys(form.errors).includes('client_key') && (

Não foi possível reconhecer o envio. Actualize as tarefas antes de continuar.

)}
); }