lty/qy-lty-admin/components/songs/add-print-batch-dialog.tsx
2026-03-17 13:17:02 +08:00

140 lines
4.5 KiB
TypeScript

"use client"
import { useState } from "react"
import { Button } from "@/components/ui/button"
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { Plus, Loader2 } from "lucide-react"
import { createSongBatch } from "@/lib/api/songs"
import { useToast } from "@/components/ui/use-toast"
interface AddPrintBatchDialogProps {
songId: string
isPublished: boolean
onBatchCreated?: () => Promise<void>
}
export function AddPrintBatchDialog({ songId, isPublished, onBatchCreated }: AddPrintBatchDialogProps) {
const [open, setOpen] = useState(false)
const [quantity, setQuantity] = useState(1000)
const [isSubmitting, setIsSubmitting] = useState(false)
const { toast } = useToast()
const handleSubmit = async () => {
setIsSubmitting(true)
try {
const response = await createSongBatch({
template: Number(songId),
quantity: quantity
})
toast({
title: "批次创建成功",
description: `已成功创建包含 ${quantity} 个卡片的新批次`,
})
// 关闭对话框
setOpen(false)
// 调用回调函数刷新批次列表
if (onBatchCreated) {
await onBatchCreated()
}
} catch (error) {
console.error("创建批次失败:", error)
toast({
title: "创建批次失败",
description: error instanceof Error ? error.message : "无法创建批次,请稍后重试",
variant: "destructive",
})
} finally {
setIsSubmitting(false)
}
}
return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button className="bg-gradient-to-r from-purple-500 to-pink-500 hover:from-purple-600 hover:to-pink-600 transition-all duration-300 shadow-md hover:shadow-lg">
<Plus className="mr-2 h-4 w-4" />
</Button>
</DialogTrigger>
<DialogContent className="sm:max-w-[500px]">
<DialogHeader>
<DialogTitle className="text-xl font-bold bg-clip-text text-transparent bg-gradient-to-r from-purple-600 to-pink-600">
</DialogTitle>
<DialogDescription>
<span className="font-medium">{songId}</span> ID
</DialogDescription>
</DialogHeader>
<div className="grid gap-4 py-4">
<div className="space-y-2">
<Label htmlFor="quantity" className="text-right">
</Label>
<Input
id="quantity"
type="number"
min="1"
value={quantity}
onChange={(e) => setQuantity(Number.parseInt(e.target.value))}
className="border-gray-300 focus-visible:ring-purple-500"
/>
<p className="text-sm text-gray-500"> {quantity} ID</p>
</div>
<div className="space-y-2">
<Label className="text-right"></Label>
<div className="p-3 bg-gray-50 rounded-md">
<p className="text-sm text-gray-700">
ID:{" "}
<span className="font-mono">
</span>
</p>
<p className="text-sm text-gray-700">
ID: <span className="font-mono"></span>
</p>
<p className="text-sm text-gray-700">
ID: <span className="font-mono"></span>
</p>
</div>
<p className="text-xs text-gray-500">ID将在创建批次时生成</p>
</div>
</div>
<DialogFooter>
<Button variant="outline" onClick={() => setOpen(false)} disabled={isSubmitting}>
</Button>
<Button
className="bg-gradient-to-r from-purple-500 to-pink-500 hover:from-purple-600 hover:to-pink-600"
onClick={handleSubmit}
disabled={isSubmitting}
>
{isSubmitting ? (
<>
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
...
</>
) : (
"创建批次"
)}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
)
}