import { Head, Link, router, useForm } from '@inertiajs/react'; import { useState } from 'react'; import type { FormEvent, ReactNode } from 'react'; import { PageTitle, Panel, FormErrors, TableViewport, controlClass, } from '@/components/procurement'; import { Button } from '@/components/ui/button'; import { usePreparationDirty } from '@/hooks/use-preparation-dirty'; import { useProcurementPreparationIdentity } from '@/hooks/use-procurement-preparation-identity'; type Row = Record; type Basis = { hash: string; rows: T[] }; type Account = { id: number; code: string; label: string; currency: string; is_movement: boolean; }; type Person = { id: number; name: string; email: string }; type Page = { data: T[]; prev_page_url: string | null; next_page_url: string | null; }; type Access = Basis & { user: Person; eligible: boolean; person: string; actions: string[]; other_actions: string[]; identity_inconsistent: boolean; }; type Props = { actorId: number; tenantId: number; q: string; users: Page; selectedAccess: Access | null; accounts: Basis; periods: Basis; rules: Basis; actions: Record; effects: Record; history: Page<{ id: number; actor_id: number; kind: string; reason: string; before: Basis; after: Basis; created_at: string; }>; }; type Kind = 'access' | 'account' | 'period' | 'rule'; type Data = { operation_key: string; basis_hash: string; reason: string; user_id?: number; person?: string; actions?: string[]; code?: string; label?: string; currency?: string; is_movement?: boolean; period_key?: string; starts_on?: string; ends_on?: string; rule_key?: string; effect_type?: string; valid_from?: string; valid_to?: string; debit_account_id?: string; credit_account_id?: string; }; const base = '/administracao/financeiro'; function useConfigurationForm( kind: Kind, hash: string, identity: string, fields: Partial, clearAfterSave = true, ) { const initial: Data = { ...fields, operation_key: '', basis_hash: hash, reason: '', }; const form = useForm(identity, initial); const ref = usePreparationDirty(form.isDirty); const current = form.data.basis_hash === hash; const review = () => { form.setData({ ...form.data, operation_key: '', basis_hash: hash }); form.clearErrors(); }; const submit = (event: FormEvent) => { event.preventDefault(); if (!current || form.processing) return; const operation = form.data.operation_key || crypto.randomUUID(); form.setData('operation_key', operation); form.transform((data) => ({ ...data, operation_key: operation })); form.post(`${base}/${kind}`, { preserveScroll: true, onSuccess: (page) => { const source = page.props[ kind === 'access' ? 'selectedAccess' : kind === 'account' ? 'accounts' : kind === 'period' ? 'periods' : 'rules' ]; if ( !source || typeof source !== 'object' || !('hash' in source) || typeof source.hash !== 'string' ) return; const next = { ...(clearAfterSave ? initial : form.data), operation_key: '', basis_hash: source.hash, reason: '', }; form.setData(next); form.setDefaults(next); form.clearErrors(); }, }); }; return { form, ref, current, review, submit }; } function Reason({ value, change, id, }: { value: string; change: (value: string) => void; id: string; }) { return (