120 lines
3.7 KiB
TypeScript
120 lines
3.7 KiB
TypeScript
"use client";
|
|||
|
|
|
||
|
|
import { useActionState } from "react";
|
||
|
|
import Link from "next/link";
|
||
|
|
import { Button } from "@/components/ui/button";
|
||
|
|
import { Input, Textarea, Select } from "@/components/ui/input";
|
||
|
|
import { Label } from "@/components/ui/label";
|
||
|
|
import { fmtDate } from "@/lib/utils";
|
||
|
|
import type { ActionState } from "./actions";
|
||
|
|
|
||
|
|
type CustomerValues = {
|
||
|
|
customerName?: string | null;
|
||
|
|
contacts?: string | null;
|
||
|
|
balanceUser?: string | null;
|
||
|
|
phone?: string | null;
|
||
|
|
detail?: string | null;
|
||
|
|
deposit?: string | null;
|
||
|
|
buy?: string | null;
|
||
|
|
birthdayType?: number | null;
|
||
|
|
birthday?: Date | null;
|
||
|
|
salesman?: string | null;
|
||
|
|
reportDate?: Date | null;
|
||
|
|
remark?: string | null;
|
||
|
|
};
|
||
|
|
|
||
|
|
function Field({
|
||
|
|
label,
|
||
|
|
name,
|
||
|
|
defaultValue,
|
||
|
|
required,
|
||
|
|
type = "text",
|
||
|
|
}: {
|
||
|
|
label: string;
|
||
|
|
name: string;
|
||
|
|
defaultValue?: string | null;
|
||
|
|
required?: boolean;
|
||
|
|
type?: string;
|
||
|
|
}) {
|
||
|
|
return (
|
||
|
|
<div className="space-y-1.5">
|
||
|
|
<Label htmlFor={name}>
|
||
|
|
{label}
|
||
|
|
{required && <span className="ml-0.5 text-[var(--color-danger)]">*</span>}
|
||
|
|
</Label>
|
||
|
|
<Input id={name} name={name} type={type} defaultValue={defaultValue ?? ""} />
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
export function CustomerForm({
|
||
|
|
action,
|
||
|
|
defaultValues,
|
||
|
|
submitLabel = "保存",
|
||
|
|
}: {
|
||
|
|
action: (prev: ActionState, formData: FormData) => Promise<ActionState>;
|
||
|
|
defaultValues?: CustomerValues;
|
||
|
|
submitLabel?: string;
|
||
|
|
}) {
|
||
|
|
const [state, formAction, pending] = useActionState(action, {});
|
||
|
|
const v = defaultValues ?? {};
|
||
|
|
|
||
|
|
return (
|
||
|
|
<form action={formAction} className="space-y-6">
|
||
|
|
<div className="grid grid-cols-1 gap-x-5 gap-y-4 sm:grid-cols-2 lg:grid-cols-3">
|
||
|
|
<Field label="客户名称" name="customerName" defaultValue={v.customerName} required />
|
||
|
|
<Field label="联系人" name="contacts" defaultValue={v.contacts} required />
|
||
|
|
<Field label="结算人" name="balanceUser" defaultValue={v.balanceUser} required />
|
||
|
|
<Field label="业务员" name="salesman" defaultValue={v.salesman} required />
|
||
|
|
<Field label="电话" name="phone" defaultValue={v.phone} />
|
||
|
|
<Field
|
||
|
|
label="报单时间"
|
||
|
|
name="reportDate"
|
||
|
|
type="date"
|
||
|
|
defaultValue={fmtDate(v.reportDate)}
|
||
|
|
required
|
||
|
|
/>
|
||
|
|
<Field label="押金" name="deposit" defaultValue={v.deposit} />
|
||
|
|
<Field label="买断" name="buy" defaultValue={v.buy} />
|
||
|
|
<div className="space-y-1.5">
|
||
|
|
<Label htmlFor="birthdayType">生日类型</Label>
|
||
|
|
<Select id="birthdayType" name="birthdayType" defaultValue={String(v.birthdayType ?? 1)}>
|
||
|
|
<option value="1">阳历</option>
|
||
|
|
<option value="2">阴历</option>
|
||
|
|
</Select>
|
||
|
|
</div>
|
||
|
|
<Field label="生日" name="birthday" type="date" defaultValue={fmtDate(v.birthday)} />
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div className="space-y-1.5">
|
||
|
|
<Label htmlFor="detail">
|
||
|
|
详情<span className="ml-0.5 text-[var(--color-danger)]">*</span>
|
||
|
|
</Label>
|
||
|
|
<Textarea id="detail" name="detail" defaultValue={v.detail ?? ""} rows={3} />
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div className="space-y-1.5">
|
||
|
|
<Label htmlFor="remark">备注</Label>
|
||
|
|
<Textarea id="remark" name="remark" defaultValue={v.remark ?? ""} rows={2} />
|
||
|
|
</div>
|
||
|
|
|
||
|
|
{state.error && (
|
||
|
|
<p className="rounded-md bg-[var(--color-danger-soft)] px-3 py-2 text-sm text-[var(--color-danger)]">
|
||
|
|
{state.error}
|
||
|
|
</p>
|
||
|
|
)}
|
||
|
|
|
||
|
|
<div className="flex gap-2 border-t pt-4">
|
||
|
|
<Button type="submit" disabled={pending}>
|
||
|
|
{pending ? "保存中…" : submitLabel}
|
||
|
|
</Button>
|
||
|
|
<Link href="/customers">
|
||
|
|
<Button type="button" variant="outline">
|
||
|
|
取消
|
||
|
|
</Button>
|
||
|
|
</Link>
|
||
|
|
</div>
|
||
|
|
</form>
|
||
|
|
);
|
||
|
|
}
|