2026-03-23 17:49:47 +08:00
|
|
|
|
"use client"
|
|
|
|
|
|
|
|
|
|
|
|
import { format } from "date-fns"
|
|
|
|
|
|
import { Suspense, useCallback, useEffect, useMemo, useState } from "react"
|
2026-03-24 17:14:50 +08:00
|
|
|
|
import { toast } from "sonner"
|
|
|
|
|
|
import {
|
|
|
|
|
|
deleteProductSku,
|
|
|
|
|
|
getAllProduct,
|
|
|
|
|
|
getPageProductSku,
|
|
|
|
|
|
} from "@/actions/product"
|
2026-03-23 17:49:47 +08:00
|
|
|
|
import { DataTable, useDataTable } from "@/components/data-table"
|
2026-04-08 15:41:32 +08:00
|
|
|
|
import { SkuCodeBadge } from "@/components/products/format"
|
2026-03-24 17:14:50 +08:00
|
|
|
|
import {
|
|
|
|
|
|
AlertDialog,
|
|
|
|
|
|
AlertDialogAction,
|
|
|
|
|
|
AlertDialogCancel,
|
|
|
|
|
|
AlertDialogContent,
|
|
|
|
|
|
AlertDialogDescription,
|
|
|
|
|
|
AlertDialogFooter,
|
|
|
|
|
|
AlertDialogHeader,
|
|
|
|
|
|
AlertDialogTitle,
|
|
|
|
|
|
AlertDialogTrigger,
|
|
|
|
|
|
} from "@/components/ui/alert-dialog"
|
2026-03-23 17:49:47 +08:00
|
|
|
|
import { Badge } from "@/components/ui/badge"
|
|
|
|
|
|
import { Button } from "@/components/ui/button"
|
2026-04-08 15:41:32 +08:00
|
|
|
|
import type { ProductCode } from "@/lib/base"
|
2026-03-23 17:49:47 +08:00
|
|
|
|
import { cn } from "@/lib/utils"
|
|
|
|
|
|
import type { Product } from "@/models/product"
|
2026-03-24 17:14:50 +08:00
|
|
|
|
import type { ProductSku } from "@/models/product_sku"
|
|
|
|
|
|
import { BatchUpdateDiscount } from "./batch-discount"
|
|
|
|
|
|
import { CreateProductSku } from "./create"
|
2026-04-08 15:41:32 +08:00
|
|
|
|
import type { SelectedProduct } from "./type"
|
2026-03-24 17:14:50 +08:00
|
|
|
|
import { UpdateProductSku } from "./update"
|
2026-03-23 17:49:47 +08:00
|
|
|
|
|
|
|
|
|
|
export default function ProductPage() {
|
2026-04-08 15:41:32 +08:00
|
|
|
|
const [selected, setSelected] = useState<SelectedProduct | undefined>(
|
|
|
|
|
|
undefined,
|
|
|
|
|
|
)
|
2026-03-23 17:49:47 +08:00
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div className="size-full flex gap-6 items-stretch">
|
|
|
|
|
|
<Products selected={selected} onSelect={setSelected} />
|
|
|
|
|
|
<ProductSkus selected={selected} />
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function Products(props: {
|
2026-04-08 15:41:32 +08:00
|
|
|
|
selected?: SelectedProduct
|
|
|
|
|
|
onSelect?: (id: SelectedProduct) => void
|
2026-03-23 17:49:47 +08:00
|
|
|
|
}) {
|
|
|
|
|
|
const [list, setList] = useState<Product[]>([])
|
|
|
|
|
|
|
|
|
|
|
|
const refresh = useCallback(async () => {
|
|
|
|
|
|
const resp = await getAllProduct()
|
|
|
|
|
|
if (resp.success) {
|
|
|
|
|
|
setList(resp.data)
|
|
|
|
|
|
}
|
|
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
|
|
const selected = useMemo(() => {
|
2026-04-08 15:41:32 +08:00
|
|
|
|
return list.find(item => item.id === props.selected?.id)
|
2026-03-23 17:49:47 +08:00
|
|
|
|
}, [list, props.selected])
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
refresh()
|
|
|
|
|
|
}, [refresh])
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<section className="flex-none basis-64 bg-background rounded-lg">
|
|
|
|
|
|
<header className="pl-3 pr-1 h-10 border-b flex items-center justify-between">
|
|
|
|
|
|
<h3 className="text-sm">产品列表</h3>
|
|
|
|
|
|
</header>
|
2026-03-24 17:14:50 +08:00
|
|
|
|
<ul className="flex flex-col gap-1 py-1">
|
2026-03-23 17:49:47 +08:00
|
|
|
|
{list.map(item => (
|
2026-03-24 17:14:50 +08:00
|
|
|
|
<li key={item.id} className="px-1">
|
2026-03-23 17:49:47 +08:00
|
|
|
|
<Button
|
|
|
|
|
|
variant="ghost"
|
|
|
|
|
|
className={cn(
|
|
|
|
|
|
"size-full box-border p-2 rounded-md flex justify-between items-center select-none",
|
|
|
|
|
|
selected?.id === item.id && "bg-primary/20",
|
|
|
|
|
|
)}
|
2026-04-08 15:41:32 +08:00
|
|
|
|
onClick={() => props.onSelect?.({ id: item.id, code: item.code })}
|
2026-03-23 17:49:47 +08:00
|
|
|
|
>
|
|
|
|
|
|
<div>
|
|
|
|
|
|
<p>{item.name}</p>
|
|
|
|
|
|
<p className="text-sm text-gray-500">{item.description}</p>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<Badge className="bg-green-600/60">已启用</Badge>
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</li>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</ul>
|
|
|
|
|
|
</section>
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-08 15:41:32 +08:00
|
|
|
|
function ProductSkus(props: {
|
|
|
|
|
|
selected?: {
|
|
|
|
|
|
id: number
|
|
|
|
|
|
code: ProductCode
|
|
|
|
|
|
}
|
|
|
|
|
|
}) {
|
2026-03-23 17:49:47 +08:00
|
|
|
|
const action = useCallback(
|
2026-04-02 13:13:59 +08:00
|
|
|
|
(page: number, size: number) =>
|
2026-04-08 15:41:32 +08:00
|
|
|
|
getPageProductSku({ page, size, product_id: props.selected?.id }),
|
2026-03-23 17:49:47 +08:00
|
|
|
|
[props.selected],
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const table = useDataTable(action)
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div className="flex-auto overflow-hidden flex flex-col items-stretch gap-3">
|
2026-03-24 17:14:50 +08:00
|
|
|
|
<div className="flex gap-3">
|
2026-04-08 15:41:32 +08:00
|
|
|
|
<CreateProductSku product={props.selected} onSuccess={table.refresh} />
|
2026-03-24 17:14:50 +08:00
|
|
|
|
<BatchUpdateDiscount
|
2026-04-08 15:41:32 +08:00
|
|
|
|
productId={props.selected?.id ?? 0}
|
2026-03-24 17:14:50 +08:00
|
|
|
|
onSuccess={table.refresh}
|
|
|
|
|
|
/>
|
2026-03-23 17:49:47 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
<Suspense>
|
2026-03-24 17:14:50 +08:00
|
|
|
|
<DataTable<ProductSku>
|
2026-03-23 17:49:47 +08:00
|
|
|
|
classNames={{
|
|
|
|
|
|
root: "overflow-auto",
|
|
|
|
|
|
}}
|
|
|
|
|
|
{...table}
|
|
|
|
|
|
columns={[
|
2026-04-08 15:41:32 +08:00
|
|
|
|
{
|
|
|
|
|
|
header: "套餐编码",
|
|
|
|
|
|
cell: ({ row }) =>
|
|
|
|
|
|
row.original.product ? (
|
|
|
|
|
|
<SkuCodeBadge
|
|
|
|
|
|
productCode={row.original.product.code}
|
|
|
|
|
|
skuCode={row.original.code}
|
|
|
|
|
|
/>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
row.original.code
|
|
|
|
|
|
),
|
|
|
|
|
|
},
|
2026-03-23 17:49:47 +08:00
|
|
|
|
{ header: "套餐名称", accessorKey: "name" },
|
2026-03-30 16:14:25 +08:00
|
|
|
|
{ header: "单价", accessorFn: row => Number(row.price).toFixed(2) },
|
2026-03-24 17:14:50 +08:00
|
|
|
|
{ header: "折扣", accessorFn: row => row.discount?.name ?? "—" },
|
2026-03-23 17:49:47 +08:00
|
|
|
|
{
|
|
|
|
|
|
header: "最终价格",
|
2026-03-30 16:14:25 +08:00
|
|
|
|
accessorFn: row => {
|
|
|
|
|
|
const value = row.discount
|
2026-03-24 17:14:50 +08:00
|
|
|
|
? (Number(row.price) * Number(row.discount.discount)) / 100
|
2026-03-30 16:14:25 +08:00
|
|
|
|
: Number(row.price)
|
|
|
|
|
|
return Number(value.toFixed(2))
|
|
|
|
|
|
},
|
2026-03-23 17:49:47 +08:00
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
header: "创建时间",
|
|
|
|
|
|
accessorFn: row => format(row.created_at, "yyyy-MM-dd HH:mm"),
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
header: "更新时间",
|
|
|
|
|
|
accessorFn: row => format(row.updated_at, "yyyy-MM-dd HH:mm"),
|
|
|
|
|
|
},
|
2026-03-24 17:14:50 +08:00
|
|
|
|
{
|
2026-03-28 15:36:08 +08:00
|
|
|
|
id: "action",
|
|
|
|
|
|
meta: { pin: "right" },
|
2026-03-24 17:14:50 +08:00
|
|
|
|
header: "操作",
|
|
|
|
|
|
cell: ({ row }) => (
|
|
|
|
|
|
<div className="flex gap-1">
|
|
|
|
|
|
<UpdateProductSku
|
|
|
|
|
|
sku={row.original}
|
|
|
|
|
|
onSuccess={table.refresh}
|
|
|
|
|
|
/>
|
|
|
|
|
|
<DeleteButton sku={row.original} onSuccess={table.refresh} />
|
|
|
|
|
|
</div>
|
|
|
|
|
|
),
|
|
|
|
|
|
},
|
2026-03-23 17:49:47 +08:00
|
|
|
|
]}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</Suspense>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
2026-03-24 17:14:50 +08:00
|
|
|
|
|
|
|
|
|
|
function DeleteButton(props: { sku: ProductSku; onSuccess?: () => void }) {
|
|
|
|
|
|
const [loading, setLoading] = useState(false)
|
|
|
|
|
|
|
|
|
|
|
|
const handleConfirm = async () => {
|
|
|
|
|
|
setLoading(true)
|
|
|
|
|
|
try {
|
|
|
|
|
|
const resp = await deleteProductSku(props.sku.id)
|
|
|
|
|
|
if (resp.success) {
|
|
|
|
|
|
toast.success("删除成功")
|
|
|
|
|
|
props.onSuccess?.()
|
|
|
|
|
|
} else {
|
|
|
|
|
|
toast.error(resp.message ?? "删除失败")
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
const message = error instanceof Error ? error.message : error
|
|
|
|
|
|
toast.error(`接口请求错误: ${message}`)
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
setLoading(false)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<AlertDialog>
|
|
|
|
|
|
<AlertDialogTrigger asChild>
|
|
|
|
|
|
<Button size="sm" variant="destructive" disabled={loading}>
|
|
|
|
|
|
删除
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</AlertDialogTrigger>
|
|
|
|
|
|
<AlertDialogContent size="sm">
|
|
|
|
|
|
<AlertDialogHeader>
|
|
|
|
|
|
<AlertDialogTitle>确认删除</AlertDialogTitle>
|
|
|
|
|
|
<AlertDialogDescription>
|
|
|
|
|
|
确定要删除套餐「{props.sku.name}」吗?此操作不可撤销。
|
|
|
|
|
|
</AlertDialogDescription>
|
|
|
|
|
|
</AlertDialogHeader>
|
|
|
|
|
|
<AlertDialogFooter>
|
|
|
|
|
|
<AlertDialogCancel>取消</AlertDialogCancel>
|
|
|
|
|
|
<AlertDialogAction variant="destructive" onClick={handleConfirm}>
|
|
|
|
|
|
删除
|
|
|
|
|
|
</AlertDialogAction>
|
|
|
|
|
|
</AlertDialogFooter>
|
|
|
|
|
|
</AlertDialogContent>
|
|
|
|
|
|
</AlertDialog>
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|