"use client" import { zodResolver } from "@hookform/resolvers/zod" import { format } from "date-fns" import { Suspense, useCallback, useRef, useState } from "react" import { Controller, useForm } from "react-hook-form" import { toast } from "sonner" import { z } from "zod" import { getPageCusts, updateCust } from "@/actions/cust" import { DataTable, useDataTable } from "@/components/data-table" import { Badge } from "@/components/ui/badge" import { Button } from "@/components/ui/button" import { Field, FieldError, FieldGroup, FieldLabel, } from "@/components/ui/field" import { Input } from "@/components/ui/input" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select" import type { Cust } from "@/models/cust" import { AddUserDialog } from "./create" type FilterValues = { account?: string name?: string identified?: boolean enabled?: boolean created_at_start?: Date created_at_end?: Date } const filterSchema = z .object({ account: z.string().optional(), name: z.string().optional(), identified: z.string().optional(), enabled: z.string().optional(), created_at_start: z.string().optional(), created_at_end: z.string().optional(), }) .superRefine((data, ctx) => { if (data.created_at_start && data.created_at_end) { const start = new Date(data.created_at_start) const end = new Date(data.created_at_end) if (end < start) { ctx.addIssue({ code: z.ZodIssueCode.custom, message: "结束时间不能早于开始时间", path: ["created_at_end"], }) } } }) type FormValues = z.infer export default function UserPage() { const [filters, setFilters] = useState({}) const [editingRowId, setEditingRowId] = useState(null) const [editPhone, setEditPhone] = useState("") const [editEmail, setEditEmail] = useState("") const [isSaving, setIsSaving] = useState(false) const [isAddDialogOpen, setIsAddDialogOpen] = useState(false) const editingRowRef = useRef(null) const { control, handleSubmit, reset } = useForm({ resolver: zodResolver(filterSchema), defaultValues: { account: "", name: "", identified: "all", enabled: "all", created_at_start: "", created_at_end: "", }, }) const fetchUsers = useCallback( (page: number, size: number) => getPageCusts({ page, size, ...filters }), [filters], ) const table = useDataTable(fetchUsers) console.log(table, "table") const onFilter = handleSubmit(data => { const result: FilterValues = {} if (data.account) result.account = data.account if (data.name) result.name = data.name if (data.identified && data.identified !== "all") result.identified = data.identified === "1" if (data.enabled && data.enabled !== "all") result.enabled = data.enabled === "1" setFilters(result) table.pagination.onPageChange(1) }) const refreshTable = useCallback(() => { table.refresh() }, [table]) const startEdit = (row: Cust) => { setEditingRowId(row.id) setEditPhone(row.phone || "") setEditEmail(row.email || "") editingRowRef.current = row } const cancelEdit = () => { setEditingRowId(null) setEditPhone("") setEditEmail("") editingRowRef.current = null } const saveEdit = async (row: Cust) => { const phoneRegex = /^1[3-9]\d{9}$/ if (editPhone && !phoneRegex.test(editPhone)) { toast.error("请输入正确的手机号格式") return false } const emailRegex = /^[^\s@]+@([^\s@]+\.)+[^\s@]+$/ if (editEmail && !emailRegex.test(editEmail)) { toast.error("请输入正确的邮箱格式") return false } setIsSaving(true) try { const result = await updateCust({ id: row.id, phone: editPhone, email: editEmail, }) if (result.success) { toast.success("更新成功") refreshTable() cancelEdit() return true } else { toast.error(result.message || "更新失败") return false } } catch (error) { toast.error("更新失败,请稍后重试") console.error(error) return false } finally { setIsSaving(false) } } const handleAddUserSuccess = () => { refreshTable() } return (
( 账号/手机号/邮箱 {fieldState.error?.message} )} /> ( 姓名 {fieldState.error?.message} )} /> ( 实名状态 {fieldState.error?.message} )} /> ( 账号状态 {fieldState.error?.message} )} /> ( 开始时间 {fieldState.error?.message} )} /> ( 结束时间 {fieldState.error?.message} )} />
{...table} columns={[ { header: "ID", accessorKey: "id" }, { header: "账号", accessorKey: "username" }, { header: "手机", accessorKey: "phone", cell: ({ row }) => { const isEditing = editingRowId === row.original.id if (isEditing) { return ( setEditPhone(e.target.value)} onBlur={() => { if (editingRowRef.current) { saveEdit(editingRowRef.current) } }} onKeyDown={e => { if (e.key === "Enter") { e.preventDefault() if (editingRowRef.current) { saveEdit(editingRowRef.current) } } if (e.key === "Escape") { e.preventDefault() cancelEdit() } }} placeholder="手机号" className="w-32" autoFocus /> ) } return row.original.phone || "-" }, }, { header: "邮箱", accessorKey: "email", cell: ({ row }) => { const isEditing = editingRowId === row.original.id if (isEditing) { return ( setEditEmail(e.target.value)} onBlur={() => { if (editingRowRef.current) { saveEdit(editingRowRef.current) } }} onKeyDown={e => { if (e.key === "Enter") { e.preventDefault() if (editingRowRef.current) { saveEdit(editingRowRef.current) } } if (e.key === "Escape") { e.preventDefault() cancelEdit() } }} placeholder="邮箱" className="w-40" /> ) } return row.original.email || "-" }, }, { header: "姓名", accessorKey: "name" }, { header: "客户来源", accessorKey: "source", cell: ({ row }) => { const sourceMap: Record = { 0: "官网注册", 1: "管理员添加", 2: "代理商注册", 3: "代理商添加", } return sourceMap[row.original.source] ?? "未知" }, }, { header: "余额", accessorKey: "balance", cell: ({ row }) => { const balance = Number(row.original.balance) || 0 return ( 0 ? "text-green-500" : "text-orange-500" } > ¥{balance.toFixed(2)} ) }, }, { header: "折扣", accessorKey: "discount.name" }, { header: "实名状态", accessorKey: "id_type", cell: ({ row }) => ( {row.original.id_type === 1 ? "已认证" : "未认证"} ), }, { header: "身份证号", accessorKey: "id_no", cell: ({ row }) => { const idNo = row.original.id_no return idNo ? `${idNo.slice(0, 6)}****${idNo.slice(-4)}` : "-" }, }, { header: "账号状态", accessorKey: "status", cell: ({ row }) => (row.original.status === 1 ? "正常" : "禁用"), }, { header: "联系方式", accessorKey: "contact_wechat" }, { header: "客户经理", accessorKey: "admin.name" }, { header: "最后登录时间", accessorKey: "last_login", cell: ({ row }) => row.original.last_login ? format( new Date(row.original.last_login), "yyyy-MM-dd HH:mm", ) : "-", }, { header: "最后登录IP", accessorKey: "last_login_ip", cell: ({ row }) => row.original.last_login_ip || "-", }, { header: "创建时间", accessorKey: "created_at", cell: ({ row }) => format(new Date(row.original.created_at), "yyyy-MM-dd HH:mm"), }, { header: "操作", cell: ({ row }) => { const isEditing = editingRowId === row.original.id if (isEditing) { return (
) } return ( ) }, }, ]} />
) }