import * as Ariakit from "@ariakit/react";
import { useState } from "react";
import { ApiError, api, type Line, type Lookups, type Row } from "./api.js";
type Form = {
vendor_name: string; invoice_number: string; invoice_date: string; amount: string; project_customer_id: string; contract_id: string;
target_employee: string; invoice_url: string; payment_url: string; pay_link: string; description: string; project: string; no_invoice_file: boolean; review_date: string; need_employee_refund: boolean; is_employee_refunded: boolean; is_paid_manual: boolean; pay_method: string;
};
const empty: Form = {
vendor_name: "", invoice_number: "", invoice_date: new Date().toISOString().slice(0, 10), amount: "", project_customer_id: "", contract_id: "",
target_employee: "", invoice_url: "", payment_url: "", pay_link: "", description: "", project: "", no_invoice_file: false, review_date: "", need_employee_refund: false, is_employee_refunded: false, is_paid_manual: false, pay_method: "",
};
const from = (r: Row): Form => ({
vendor_name: r.vendor_name, invoice_number: r.invoice_number ?? "", invoice_date: r.invoice_date ?? "", amount: String(r.amount), project_customer_id: r.project_customer_id ?? "", contract_id: r.contract_id ?? "",
target_employee: r.target_employee ?? "", invoice_url: r.invoice_url ?? "", payment_url: r.payment_url ?? "", pay_link: r.pay_link ?? "", description: r.description ?? "",
project: r.internal_project_id ? `internal:${r.internal_project_id}` : r.project_customer_id ?? "", no_invoice_file: r.no_invoice_file, review_date: r.review_date ?? "", need_employee_refund: r.need_employee_refund, is_employee_refunded: r.is_employee_refunded,
is_paid_manual: r.is_paid_manual, pay_method: r.pay_method ?? "",
});
const nz = (s: string): string | null => (s.trim() ? s.trim() : null);
const NEW_FIELDS = ["vendor_name", "invoice_number", "invoice_date", "amount", "project_customer_id", "internal_project_id", "contract_id", "target_employee", "invoice_url", "payment_url", "need_employee_refund", "pay_method", "pay_link", "description", "no_invoice_file"];
interface Props {
store: Ariakit.DialogStore;
lookups: Lookups;
/** null - создание нового черновика */
row: Row | null;
lines: Line[];
onSaved: (id: string) => void;
/** человек по умолчанию для «Кто ждёт инвойс» при создании (автор) */
defaultPerson: string;
}
export function FormDialog(p: Props) {
const open = Ariakit.useStoreState(p.store, "open");
return open ?