2026-07-08 15:02:03 +08:00
|
|
|
"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;
|
2026-07-12 16:58:52 +08:00
|
|
|
birthdayOwner?: number | null;
|
2026-07-08 15:02:03 +08:00
|
|
|
birthday?: Date | null;
|
|
|
|
|
salesman?: string | null;
|
|
|
|
|
reportDate?: Date | null;
|
|
|
|
|
remark?: string | null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function Field({
|
|
|
|
|
label,
|
|
|
|
|
name,
|
|
|
|
|
defaultValue,
|
|
|
|
|
required,
|
|
|
|
|
type = "text",
|
2026-07-11 21:17:59 +08:00
|
|
|
pattern,
|
|
|
|
|
inputMode,
|
|
|
|
|
title,
|
2026-07-08 15:02:03 +08:00
|
|
|
}: {
|
|
|
|
|
label: string;
|
|
|
|
|
name: string;
|
|
|
|
|
defaultValue?: string | null;
|
|
|
|
|
required?: boolean;
|
|
|
|
|
type?: string;
|
2026-07-11 21:17:59 +08:00
|
|
|
pattern?: string;
|
|
|
|
|
inputMode?: React.HTMLAttributes<HTMLInputElement>["inputMode"];
|
|
|
|
|
title?: string;
|
2026-07-08 15:02:03 +08:00
|
|
|
}) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="space-y-1.5">
|
|
|
|
|
<Label htmlFor={name}>
|
|
|
|
|
{label}
|
|
|
|
|
{required && <span className="ml-0.5 text-[var(--color-danger)]">*</span>}
|
|
|
|
|
</Label>
|
2026-07-11 21:17:59 +08:00
|
|
|
<Input
|
|
|
|
|
id={name}
|
|
|
|
|
name={name}
|
|
|
|
|
type={type}
|
|
|
|
|
defaultValue={defaultValue ?? ""}
|
|
|
|
|
required={required}
|
|
|
|
|
pattern={pattern}
|
|
|
|
|
inputMode={inputMode}
|
|
|
|
|
title={title}
|
|
|
|
|
/>
|
2026-07-08 15:02:03 +08:00
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function CustomerForm({
|
|
|
|
|
action,
|
|
|
|
|
defaultValues,
|
|
|
|
|
submitLabel = "保存",
|
2026-07-11 21:17:59 +08:00
|
|
|
requireAll = false,
|
2026-07-08 15:02:03 +08:00
|
|
|
}: {
|
|
|
|
|
action: (prev: ActionState, formData: FormData) => Promise<ActionState>;
|
|
|
|
|
defaultValues?: CustomerValues;
|
|
|
|
|
submitLabel?: string;
|
2026-07-11 21:17:59 +08:00
|
|
|
requireAll?: boolean;
|
2026-07-08 15:02:03 +08:00
|
|
|
}) {
|
|
|
|
|
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">
|
2026-07-11 21:17:59 +08:00
|
|
|
<Field
|
2026-07-12 00:30:17 +08:00
|
|
|
label="商户名称"
|
2026-07-11 21:17:59 +08:00
|
|
|
name="customerName"
|
|
|
|
|
defaultValue={v.customerName}
|
|
|
|
|
required
|
|
|
|
|
pattern={requireAll ? "\\S+" : undefined}
|
2026-07-12 00:30:17 +08:00
|
|
|
title={requireAll ? "商户名称不能包含空格" : undefined}
|
2026-07-11 21:17:59 +08:00
|
|
|
/>
|
2026-07-08 15:02:03 +08:00
|
|
|
<Field label="联系人" name="contacts" defaultValue={v.contacts} required />
|
|
|
|
|
<Field label="结算人" name="balanceUser" defaultValue={v.balanceUser} required />
|
|
|
|
|
<Field label="业务员" name="salesman" defaultValue={v.salesman} required />
|
2026-07-11 21:17:59 +08:00
|
|
|
<Field
|
2026-07-12 00:30:17 +08:00
|
|
|
label="联系电话"
|
2026-07-11 21:17:59 +08:00
|
|
|
name="phone"
|
|
|
|
|
defaultValue={v.phone}
|
|
|
|
|
required={requireAll}
|
|
|
|
|
pattern={requireAll ? "1\\d{10}" : undefined}
|
|
|
|
|
inputMode={requireAll ? "numeric" : undefined}
|
|
|
|
|
title={requireAll ? "请输入 11 位手机号" : undefined}
|
|
|
|
|
/>
|
2026-07-08 15:02:03 +08:00
|
|
|
<Field
|
2026-07-12 00:30:17 +08:00
|
|
|
label="报单日期"
|
2026-07-08 15:02:03 +08:00
|
|
|
name="reportDate"
|
|
|
|
|
type="date"
|
|
|
|
|
defaultValue={fmtDate(v.reportDate)}
|
|
|
|
|
required
|
|
|
|
|
/>
|
2026-07-11 21:17:59 +08:00
|
|
|
<Field label="押金" name="deposit" defaultValue={v.deposit} required={requireAll} />
|
|
|
|
|
<Field label="买断" name="buy" defaultValue={v.buy} required={requireAll} />
|
2026-07-12 16:58:52 +08:00
|
|
|
{/* 生日类型 + 生日归属,同行两个下拉框 */}
|
|
|
|
|
<div className="grid grid-cols-2 gap-3">
|
|
|
|
|
<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>
|
|
|
|
|
<div className="space-y-1.5">
|
|
|
|
|
<Label htmlFor="birthdayOwner">生日归属</Label>
|
|
|
|
|
<Select id="birthdayOwner" name="birthdayOwner" defaultValue={String(v.birthdayOwner ?? 2)}>
|
|
|
|
|
<option value="1">结算人</option>
|
|
|
|
|
<option value="2">联系人</option>
|
|
|
|
|
</Select>
|
|
|
|
|
</div>
|
2026-07-08 15:02:03 +08:00
|
|
|
</div>
|
2026-07-11 21:17:59 +08:00
|
|
|
<Field
|
|
|
|
|
label="生日"
|
|
|
|
|
name="birthday"
|
|
|
|
|
type="date"
|
|
|
|
|
defaultValue={fmtDate(v.birthday)}
|
|
|
|
|
required={requireAll}
|
|
|
|
|
/>
|
2026-07-08 15:02:03 +08:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="space-y-1.5">
|
|
|
|
|
<Label htmlFor="detail">
|
2026-07-12 00:30:17 +08:00
|
|
|
产品明细<span className="ml-0.5 text-[var(--color-danger)]">*</span>
|
2026-07-08 15:02:03 +08:00
|
|
|
</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>
|
|
|
|
|
);
|
|
|
|
|
}
|