149 lines
4.6 KiB
TypeScript
149 lines
4.6 KiB
TypeScript
import Link from "next/link";
|
|||
|
|
import { prisma } from "@/lib/prisma";
|
||
|
|
import { fmtDate } from "@/lib/utils";
|
||
|
|
import { Card } from "@/components/ui/card";
|
||
|
|
import { Table, THead, TR, TH, TD } from "@/components/ui/table";
|
||
|
|
import { Badge } from "@/components/ui/badge";
|
||
|
|
import { Button } from "@/components/ui/button";
|
||
|
|
import { CustomerToolbar } from "./customer-toolbar";
|
||
|
|
import { DeleteButton } from "./delete-button";
|
||
|
|
import { buildCustomerWhere } from "./query";
|
||
|
|
|
||
|
|
const PAGE_SIZE = 15;
|
||
|
|
|
||
|
|
type SearchParams = {
|
||
|
|
keyword?: string;
|
||
|
|
start?: string;
|
||
|
|
end?: string;
|
||
|
|
page?: string;
|
||
|
|
};
|
||
|
|
|
||
|
|
export default async function CustomersPage({
|
||
|
|
searchParams,
|
||
|
|
}: {
|
||
|
|
searchParams: Promise<SearchParams>;
|
||
|
|
}) {
|
||
|
|
const sp = await searchParams;
|
||
|
|
const keyword = (sp.keyword ?? "").trim();
|
||
|
|
const page = Math.max(1, Number(sp.page) || 1);
|
||
|
|
const where = buildCustomerWhere(sp);
|
||
|
|
|
||
|
|
const [rows, total] = await Promise.all([
|
||
|
|
prisma.customer.findMany({
|
||
|
|
where,
|
||
|
|
orderBy: { reportDate: "desc" },
|
||
|
|
skip: (page - 1) * PAGE_SIZE,
|
||
|
|
take: PAGE_SIZE,
|
||
|
|
}),
|
||
|
|
prisma.customer.count({ where }),
|
||
|
|
]);
|
||
|
|
|
||
|
|
const totalPages = Math.max(1, Math.ceil(total / PAGE_SIZE));
|
||
|
|
const exportQs = new URLSearchParams();
|
||
|
|
if (sp.keyword) exportQs.set("keyword", sp.keyword);
|
||
|
|
if (sp.start) exportQs.set("start", sp.start);
|
||
|
|
if (sp.end) exportQs.set("end", sp.end);
|
||
|
|
|
||
|
|
return (
|
||
|
|
<div className="space-y-4">
|
||
|
|
<CustomerToolbar
|
||
|
|
defaultKeyword={keyword}
|
||
|
|
defaultStart={sp.start}
|
||
|
|
defaultEnd={sp.end}
|
||
|
|
exportHref={`/customers/export?${exportQs.toString()}`}
|
||
|
|
/>
|
||
|
|
|
||
|
|
<Card className="overflow-hidden">
|
||
|
|
<Table>
|
||
|
|
<THead>
|
||
|
|
<tr>
|
||
|
|
<TH>客户名称</TH>
|
||
|
|
<TH>联系人</TH>
|
||
|
|
<TH>电话</TH>
|
||
|
|
<TH>业务员</TH>
|
||
|
|
<TH>报单时间</TH>
|
||
|
|
<TH>生日</TH>
|
||
|
|
<TH className="text-right">操作</TH>
|
||
|
|
</tr>
|
||
|
|
</THead>
|
||
|
|
<tbody>
|
||
|
|
{rows.length === 0 && (
|
||
|
|
<tr>
|
||
|
|
<TD colSpan={7} className="py-14 text-center text-[var(--color-muted-foreground)]">
|
||
|
|
暂无数据
|
||
|
|
</TD>
|
||
|
|
</tr>
|
||
|
|
)}
|
||
|
|
{rows.map((c) => (
|
||
|
|
<TR key={c.id}>
|
||
|
|
<TD className="font-medium">{c.customerName}</TD>
|
||
|
|
<TD>{c.contacts}</TD>
|
||
|
|
<TD className="tabular-nums">{c.phone}</TD>
|
||
|
|
<TD>{c.salesman}</TD>
|
||
|
|
<TD className="tabular-nums">{fmtDate(c.reportDate)}</TD>
|
||
|
|
<TD>
|
||
|
|
<span className="tabular-nums">{fmtDate(c.birthday)}</span>
|
||
|
|
{c.birthday && (
|
||
|
|
<Badge tone={c.birthdayType === 2 ? "warning" : "info"} className="ml-2">
|
||
|
|
{c.birthdayType === 2 ? "农历" : "阳历"}
|
||
|
|
</Badge>
|
||
|
|
)}
|
||
|
|
</TD>
|
||
|
|
<TD>
|
||
|
|
<div className="flex justify-end gap-1.5">
|
||
|
|
<Link href={`/customers/${c.id}/edit`}>
|
||
|
|
<Button variant="outline" size="sm">编辑</Button>
|
||
|
|
</Link>
|
||
|
|
<DeleteButton id={c.id} name={c.customerName ?? ""} />
|
||
|
|
</div>
|
||
|
|
</TD>
|
||
|
|
</TR>
|
||
|
|
))}
|
||
|
|
</tbody>
|
||
|
|
</Table>
|
||
|
|
</Card>
|
||
|
|
|
||
|
|
{/* 分页 */}
|
||
|
|
<div className="flex items-center justify-between text-sm text-[var(--color-muted-foreground)]">
|
||
|
|
<span>共 <span className="font-medium text-[var(--color-foreground)]">{total.toLocaleString()}</span> 条</span>
|
||
|
|
<div className="flex items-center gap-2">
|
||
|
|
<PageLink page={page - 1} disabled={page <= 1} sp={sp} label="上一页" />
|
||
|
|
<span className="px-1 tabular-nums">{page} / {totalPages}</span>
|
||
|
|
<PageLink page={page + 1} disabled={page >= totalPages} sp={sp} label="下一页" />
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
function PageLink({
|
||
|
|
page,
|
||
|
|
disabled,
|
||
|
|
sp,
|
||
|
|
label,
|
||
|
|
}: {
|
||
|
|
page: number;
|
||
|
|
disabled: boolean;
|
||
|
|
sp: SearchParams;
|
||
|
|
label: string;
|
||
|
|
}) {
|
||
|
|
if (disabled) {
|
||
|
|
return (
|
||
|
|
<span className="rounded-md border px-3 py-1.5 opacity-40">{label}</span>
|
||
|
|
);
|
||
|
|
}
|
||
|
|
const params = new URLSearchParams();
|
||
|
|
if (sp.keyword) params.set("keyword", sp.keyword);
|
||
|
|
if (sp.start) params.set("start", sp.start);
|
||
|
|
if (sp.end) params.set("end", sp.end);
|
||
|
|
params.set("page", String(page));
|
||
|
|
return (
|
||
|
|
<Link
|
||
|
|
href={`/customers?${params.toString()}`}
|
||
|
|
className="rounded-md border bg-[var(--color-surface)] px-3 py-1.5 hover:bg-[var(--color-muted)]"
|
||
|
|
>
|
||
|
|
{label}
|
||
|
|
</Link>
|
||
|
|
);
|
||
|
|
}
|