2026-04-09 10:01:23 +08:00
|
|
|
|
"use client"
|
|
|
|
|
|
|
|
|
|
|
|
import { zodResolver } from "@hookform/resolvers/zod"
|
2026-05-02 19:40:42 +08:00
|
|
|
|
import { Controller, type Resolver, useForm } from "react-hook-form"
|
2026-04-09 10:01:23 +08:00
|
|
|
|
import { toast } from "sonner"
|
|
|
|
|
|
import { z } from "zod/v4"
|
|
|
|
|
|
|
|
|
|
|
|
import { OptionCombobox } from "@/components/option-combobox"
|
|
|
|
|
|
import { Button } from "@/components/ui/button"
|
|
|
|
|
|
import {
|
|
|
|
|
|
Dialog,
|
|
|
|
|
|
DialogContent,
|
|
|
|
|
|
DialogFooter,
|
|
|
|
|
|
DialogHeader,
|
|
|
|
|
|
DialogTitle,
|
|
|
|
|
|
} from "@/components/ui/dialog"
|
|
|
|
|
|
import { Field, FieldContent, FieldError, FieldLabel } from "@/components/ui/field"
|
2026-05-02 19:40:42 +08:00
|
|
|
|
import { changeTicketStatus, type TicketStatus } from "@/lib/api/ticket"
|
|
|
|
|
|
|
|
|
|
|
|
const ticketStatuses = [
|
|
|
|
|
|
{ value: "pending", label: "待处理" },
|
|
|
|
|
|
{ value: "in_progress", label: "处理中" },
|
|
|
|
|
|
{ value: "done", label: "已处理" },
|
|
|
|
|
|
] satisfies Array<{ value: TicketStatus; label: string }>
|
2026-04-09 10:01:23 +08:00
|
|
|
|
|
|
|
|
|
|
const schema = z.object({
|
2026-05-02 19:40:42 +08:00
|
|
|
|
status: z.enum(["pending", "in_progress", "done"], { message: "请选择状态" }),
|
2026-04-09 10:01:23 +08:00
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
type FormValues = z.infer<typeof schema>
|
|
|
|
|
|
|
|
|
|
|
|
const resolver = zodResolver(schema as never) as Resolver<
|
|
|
|
|
|
z.input<typeof schema>,
|
|
|
|
|
|
undefined,
|
|
|
|
|
|
z.output<typeof schema>
|
|
|
|
|
|
>
|
|
|
|
|
|
|
|
|
|
|
|
type TicketStatusDialogProps = {
|
|
|
|
|
|
open: boolean
|
|
|
|
|
|
ticketId: number | null
|
|
|
|
|
|
ticketIds?: number[]
|
|
|
|
|
|
currentStatus?: string
|
|
|
|
|
|
onOpenChange: (open: boolean) => void
|
|
|
|
|
|
onSuccess?: () => Promise<void> | void
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function TicketStatusDialog({
|
|
|
|
|
|
open,
|
|
|
|
|
|
ticketId,
|
|
|
|
|
|
ticketIds,
|
|
|
|
|
|
currentStatus,
|
|
|
|
|
|
onOpenChange,
|
|
|
|
|
|
onSuccess,
|
|
|
|
|
|
}: TicketStatusDialogProps) {
|
|
|
|
|
|
const form = useForm<
|
|
|
|
|
|
z.input<typeof schema>,
|
|
|
|
|
|
undefined,
|
|
|
|
|
|
z.output<typeof schema>
|
|
|
|
|
|
>({
|
|
|
|
|
|
resolver,
|
|
|
|
|
|
defaultValues: {
|
2026-05-02 19:40:42 +08:00
|
|
|
|
status: isTicketStatus(currentStatus) ? currentStatus : "pending",
|
2026-04-09 10:01:23 +08:00
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
|
control,
|
|
|
|
|
|
reset,
|
|
|
|
|
|
handleSubmit,
|
|
|
|
|
|
formState: { errors, isSubmitting },
|
|
|
|
|
|
} = form
|
|
|
|
|
|
|
2026-05-02 19:40:42 +08:00
|
|
|
|
function handleOpenChange(nextOpen: boolean) {
|
|
|
|
|
|
if (nextOpen) {
|
|
|
|
|
|
reset({ status: isTicketStatus(currentStatus) ? currentStatus : "pending" })
|
2026-04-09 10:01:23 +08:00
|
|
|
|
}
|
2026-05-02 19:40:42 +08:00
|
|
|
|
onOpenChange(nextOpen)
|
|
|
|
|
|
}
|
2026-04-09 10:01:23 +08:00
|
|
|
|
|
|
|
|
|
|
async function onFormSubmit(values: FormValues) {
|
|
|
|
|
|
const validTicketIds = (ticketIds ?? []).filter((item) => item > 0)
|
|
|
|
|
|
if (!ticketId && validTicketIds.length === 0) {
|
|
|
|
|
|
toast.error("请选择工单")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (validTicketIds.length > 0) {
|
2026-05-02 19:40:42 +08:00
|
|
|
|
await Promise.all(
|
|
|
|
|
|
validTicketIds.map((id) => changeTicketStatus({ ticketId: id, status: values.status })),
|
|
|
|
|
|
)
|
2026-04-09 10:01:23 +08:00
|
|
|
|
toast.success(`已批量更新 ${validTicketIds.length} 张工单`)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
await changeTicketStatus({
|
|
|
|
|
|
ticketId: ticketId!,
|
|
|
|
|
|
status: values.status,
|
|
|
|
|
|
})
|
|
|
|
|
|
toast.success("状态已更新")
|
|
|
|
|
|
}
|
|
|
|
|
|
onOpenChange(false)
|
|
|
|
|
|
await onSuccess?.()
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
toast.error(error instanceof Error ? error.message : "更新状态失败")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
2026-05-02 19:40:42 +08:00
|
|
|
|
<Dialog open={open} onOpenChange={handleOpenChange}>
|
2026-04-09 10:01:23 +08:00
|
|
|
|
<DialogContent className="max-w-lg gap-0 p-0 sm:max-w-lg">
|
|
|
|
|
|
<DialogHeader className="px-6 pt-6">
|
|
|
|
|
|
<DialogTitle>{ticketIds?.length ? `批量变更状态(${ticketIds.length})` : "变更工单状态"}</DialogTitle>
|
|
|
|
|
|
</DialogHeader>
|
|
|
|
|
|
<form onSubmit={handleSubmit(onFormSubmit)}>
|
|
|
|
|
|
<div className="space-y-4 p-6">
|
|
|
|
|
|
<Field data-invalid={!!errors.status}>
|
|
|
|
|
|
<FieldLabel>目标状态</FieldLabel>
|
|
|
|
|
|
<FieldContent>
|
|
|
|
|
|
<Controller
|
|
|
|
|
|
control={control}
|
|
|
|
|
|
name="status"
|
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
|
<OptionCombobox
|
|
|
|
|
|
value={field.value}
|
|
|
|
|
|
onChange={field.onChange}
|
|
|
|
|
|
placeholder="请选择状态"
|
2026-05-02 19:40:42 +08:00
|
|
|
|
options={ticketStatuses}
|
2026-04-09 10:01:23 +08:00
|
|
|
|
/>
|
|
|
|
|
|
)}
|
|
|
|
|
|
/>
|
|
|
|
|
|
<FieldError errors={[errors.status]} />
|
|
|
|
|
|
</FieldContent>
|
|
|
|
|
|
</Field>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
<DialogFooter className="mx-0 mb-0 px-6 py-4">
|
|
|
|
|
|
<Button type="button" variant="outline" onClick={() => onOpenChange(false)}>
|
|
|
|
|
|
取消
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
<Button type="submit" disabled={isSubmitting}>
|
|
|
|
|
|
{isSubmitting ? "提交中..." : "确认变更"}
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</DialogFooter>
|
|
|
|
|
|
</form>
|
|
|
|
|
|
</DialogContent>
|
|
|
|
|
|
</Dialog>
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
2026-05-02 19:40:42 +08:00
|
|
|
|
|
|
|
|
|
|
function isTicketStatus(status: string | undefined): status is TicketStatus {
|
|
|
|
|
|
return status === "pending" || status === "in_progress" || status === "done"
|
|
|
|
|
|
}
|