|
|
@@ -1,409 +0,0 @@
|
|
|
-import React, { useState, useEffect } from 'react';
|
|
|
-import { useMutation, useQueryClient } from '@tanstack/react-query';
|
|
|
-import { Button } from '@d8d/shared-ui-components/components/ui/button';
|
|
|
-import {
|
|
|
- Dialog,
|
|
|
- DialogContent,
|
|
|
- DialogDescription,
|
|
|
- DialogFooter,
|
|
|
- DialogHeader,
|
|
|
- DialogTitle,
|
|
|
-} from '@d8d/shared-ui-components/components/ui/dialog';
|
|
|
-import {
|
|
|
- Form,
|
|
|
- FormLabel,
|
|
|
- FormMessage,
|
|
|
-} from '@d8d/shared-ui-components/components/ui/form';
|
|
|
-import { Input } from '@d8d/shared-ui-components/components/ui/input';
|
|
|
-import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@d8d/shared-ui-components/components/ui/table';
|
|
|
-import { Badge } from '@d8d/shared-ui-components/components/ui/badge';
|
|
|
-import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@d8d/shared-ui-components/components/ui/card';
|
|
|
-import { Separator } from '@d8d/shared-ui-components/components/ui/separator';
|
|
|
-import { useForm } from 'react-hook-form';
|
|
|
-import { zodResolver } from '@hookform/resolvers/zod';
|
|
|
-import { z } from 'zod';
|
|
|
-import { toast } from 'sonner';
|
|
|
-import { User, Users, X } from 'lucide-react';
|
|
|
-import { DisabledPersonSelector } from '@d8d/allin-disability-person-management-ui';
|
|
|
-import { WorkStatus } from '@d8d/allin-enums';
|
|
|
-import { orderClient, orderClientManager } from '../api/orderClient';
|
|
|
-import type { DisabledPersonData } from '@d8d/allin-disability-person-management-ui';
|
|
|
-
|
|
|
-// 批量添加人员表单Schema
|
|
|
-// 根据后端API,需要包含joinDate、salaryDetail等字段
|
|
|
-const batchAddPersonsSchema = z.object({
|
|
|
- persons: z.array(
|
|
|
- z.object({
|
|
|
- personId: z.number().int().positive('请选择人员'),
|
|
|
- joinDate: z.string().datetime('请选择有效的入职日期'),
|
|
|
- salaryDetail: z.string().min(1, '薪资详情不能为空'),
|
|
|
- leaveDate: z.string().datetime().optional(),
|
|
|
- workStatus: z.nativeEnum(WorkStatus).optional(),
|
|
|
- role: z.string().max(50, '角色不能超过50个字符').optional(),
|
|
|
- remark: z.string().max(200, '备注不能超过200个字符').optional(),
|
|
|
- })
|
|
|
- ).min(1, '至少选择一名人员'),
|
|
|
-});
|
|
|
-
|
|
|
-type BatchAddPersonsFormValues = z.infer<typeof batchAddPersonsSchema>;
|
|
|
-
|
|
|
-interface PersonSelectorProps {
|
|
|
- orderId: number;
|
|
|
- open: boolean;
|
|
|
- onOpenChange: (open: boolean) => void;
|
|
|
- onSuccess?: () => void;
|
|
|
-}
|
|
|
-
|
|
|
-export const PersonSelector: React.FC<PersonSelectorProps> = ({
|
|
|
- orderId,
|
|
|
- open,
|
|
|
- onOpenChange,
|
|
|
- onSuccess,
|
|
|
-}) => {
|
|
|
- console.debug('PersonSelector: render with open=', open);
|
|
|
- const queryClient = useQueryClient();
|
|
|
- const [isSubmitting, setIsSubmitting] = useState(false);
|
|
|
- const [selectedPersons, setSelectedPersons] = useState<DisabledPersonData[]>([]);
|
|
|
- const [isSelectorOpen, setIsSelectorOpen] = useState(false);
|
|
|
-
|
|
|
- // 初始化表单
|
|
|
- const form = useForm<BatchAddPersonsFormValues>({
|
|
|
- resolver: zodResolver(batchAddPersonsSchema),
|
|
|
- defaultValues: {
|
|
|
- persons: [],
|
|
|
- },
|
|
|
- });
|
|
|
-
|
|
|
- // 批量添加人员Mutation
|
|
|
- const batchAddMutation = useMutation({
|
|
|
- mutationFn: async (data: BatchAddPersonsFormValues) => {
|
|
|
- const response = await orderClientManager.get()[':orderId'].persons.batch.$post({
|
|
|
- param: { orderId },
|
|
|
- json: data,
|
|
|
- });
|
|
|
- if (!response.ok) {
|
|
|
- const error = await response.json();
|
|
|
- throw new Error(error.message || '批量添加人员失败');
|
|
|
- }
|
|
|
- return response.json();
|
|
|
- },
|
|
|
- onSuccess: () => {
|
|
|
- toast.success('批量添加人员成功');
|
|
|
- queryClient.invalidateQueries({ queryKey: ['order-persons', orderId] });
|
|
|
- onOpenChange(false);
|
|
|
- setSelectedPersons([]);
|
|
|
- form.reset();
|
|
|
- onSuccess?.();
|
|
|
- },
|
|
|
- onError: (error: Error) => {
|
|
|
- toast.error(`批量添加人员失败: ${error.message}`);
|
|
|
- },
|
|
|
- });
|
|
|
-
|
|
|
- // 处理表单提交
|
|
|
- const onSubmit = async (data: BatchAddPersonsFormValues) => {
|
|
|
- setIsSubmitting(true);
|
|
|
- try {
|
|
|
- await batchAddMutation.mutateAsync(data);
|
|
|
- } finally {
|
|
|
- setIsSubmitting(false);
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
- // 处理残疾人选择
|
|
|
- const handlePersonSelect = (persons: DisabledPersonData | DisabledPersonData[]) => {
|
|
|
- if (Array.isArray(persons)) {
|
|
|
- // 多选模式
|
|
|
- const newPersons = persons.filter(
|
|
|
- person => !selectedPersons.some(p => p.id === person.id)
|
|
|
- );
|
|
|
- setSelectedPersons(prev => [...prev, ...newPersons]);
|
|
|
-
|
|
|
- // 更新表单值 - 根据后端API要求包含必需字段
|
|
|
- const currentPersons = form.getValues('persons') || [];
|
|
|
- const newFormPersons = newPersons.map(person => ({
|
|
|
- personId: person.id,
|
|
|
- joinDate: new Date().toISOString(), // 默认当前时间
|
|
|
- salaryDetail: '待定', // 默认值
|
|
|
- leaveDate: undefined,
|
|
|
- workStatus: WorkStatus.WORKING,
|
|
|
- role: '',
|
|
|
- remark: ''
|
|
|
- }));
|
|
|
- form.setValue('persons', [...currentPersons, ...newFormPersons]);
|
|
|
- } else {
|
|
|
- // 单选模式
|
|
|
- const person = persons;
|
|
|
- if (!selectedPersons.some(p => p.id === person.id)) {
|
|
|
- setSelectedPersons(prev => [...prev, person]);
|
|
|
-
|
|
|
- // 更新表单值 - 根据后端API要求包含必需字段
|
|
|
- const currentPersons = form.getValues('persons') || [];
|
|
|
- form.setValue('persons', [
|
|
|
- ...currentPersons,
|
|
|
- {
|
|
|
- personId: person.id,
|
|
|
- joinDate: new Date().toISOString(), // 默认当前时间
|
|
|
- salaryDetail: '待定', // 默认值
|
|
|
- leaveDate: undefined,
|
|
|
- workStatus: WorkStatus.WORKING,
|
|
|
- role: '',
|
|
|
- remark: ''
|
|
|
- }
|
|
|
- ]);
|
|
|
- }
|
|
|
- }
|
|
|
- setIsSelectorOpen(false);
|
|
|
- };
|
|
|
-
|
|
|
- // 处理移除人员
|
|
|
- const handleRemovePerson = (personId: number) => {
|
|
|
- setSelectedPersons(prev => prev.filter(p => p.id !== personId));
|
|
|
-
|
|
|
- // 更新表单值
|
|
|
- const currentPersons = form.getValues('persons') || [];
|
|
|
- form.setValue('persons', currentPersons.filter(p => p.personId !== personId));
|
|
|
- };
|
|
|
-
|
|
|
- // 处理人员详情更新
|
|
|
- const handlePersonDetailChange = (personId: number, field: keyof BatchAddPersonsFormValues['persons'][0], value: string) => {
|
|
|
- const currentPersons = form.getValues('persons') || [];
|
|
|
- const updatedPersons = currentPersons.map(person =>
|
|
|
- person.personId === personId ? { ...person, [field]: value } : person
|
|
|
- );
|
|
|
- form.setValue('persons', updatedPersons);
|
|
|
- };
|
|
|
-
|
|
|
- // 重置状态
|
|
|
- const resetState = () => {
|
|
|
- setSelectedPersons([]);
|
|
|
- setIsSelectorOpen(false);
|
|
|
- form.reset({
|
|
|
- persons: [],
|
|
|
- });
|
|
|
- };
|
|
|
-
|
|
|
- // 当对话框关闭时重置状态
|
|
|
- useEffect(() => {
|
|
|
- if (!open) {
|
|
|
- resetState();
|
|
|
- }
|
|
|
- }, [open]);
|
|
|
-
|
|
|
- // 处理Dialog的onOpenChange,防止在残疾人选择器打开时关闭人员选择器
|
|
|
- const handleDialogOpenChange = (newOpen: boolean) => {
|
|
|
- console.debug('PersonSelector: handleDialogOpenChange called with', newOpen, 'isSelectorOpen:', isSelectorOpen);
|
|
|
- if (isSelectorOpen && !newOpen) {
|
|
|
- console.debug('PersonSelector: 阻止人员选择器关闭,因为残疾人选择器正在打开');
|
|
|
- return; // 如果残疾人选择器正在打开,阻止人员选择器关闭
|
|
|
- }
|
|
|
- onOpenChange(newOpen);
|
|
|
- };
|
|
|
-
|
|
|
- return (
|
|
|
- <>
|
|
|
- <Dialog open={open} onOpenChange={handleDialogOpenChange}>
|
|
|
- <DialogContent className="sm:max-w-[800px] max-h-[90vh] overflow-y-auto">
|
|
|
- <DialogHeader>
|
|
|
- <DialogTitle data-testid="batch-add-persons-dialog-title">批量添加残疾人到订单</DialogTitle>
|
|
|
- <DialogDescription>
|
|
|
- 选择要添加到订单的残疾人,并设置角色和备注信息
|
|
|
- </DialogDescription>
|
|
|
- </DialogHeader>
|
|
|
-
|
|
|
- <Form {...form}>
|
|
|
- <form onSubmit={form.handleSubmit(onSubmit, (errors) => console.debug('表单验证错误:', errors))} className="space-y-6">
|
|
|
- {/* 选择残疾人区域 */}
|
|
|
- <Card>
|
|
|
- <CardHeader>
|
|
|
- <CardTitle className="text-lg flex items-center justify-between">
|
|
|
- <div className="flex items-center">
|
|
|
- <Users className="mr-2 h-5 w-5" />
|
|
|
- 选择残疾人
|
|
|
- </div>
|
|
|
- <Button
|
|
|
- type="button"
|
|
|
- variant="outline"
|
|
|
- size="sm"
|
|
|
- onClick={() => setIsSelectorOpen(true)}
|
|
|
- data-testid="select-persons-button"
|
|
|
- >
|
|
|
- <User className="mr-2 h-4 w-4" />
|
|
|
- 选择残疾人
|
|
|
- </Button>
|
|
|
- </CardTitle>
|
|
|
- <CardDescription>
|
|
|
- 使用残疾人选择器选择要添加到订单的残疾人
|
|
|
- </CardDescription>
|
|
|
- </CardHeader>
|
|
|
- <CardContent>
|
|
|
- {selectedPersons.length === 0 ? (
|
|
|
- <div className="text-center py-8 border rounded-md">
|
|
|
- <Users className="h-12 w-12 mx-auto text-muted-foreground mb-2" />
|
|
|
- <div className="text-muted-foreground">尚未选择残疾人</div>
|
|
|
- <div className="text-sm text-muted-foreground mt-1">
|
|
|
- 点击"选择残疾人"按钮打开选择器
|
|
|
- </div>
|
|
|
- </div>
|
|
|
- ) : (
|
|
|
- <div className="border rounded-md">
|
|
|
- <Table>
|
|
|
- <TableHeader>
|
|
|
- <TableRow>
|
|
|
- <TableHead>姓名</TableHead>
|
|
|
- <TableHead>性别</TableHead>
|
|
|
- <TableHead>残疾证号</TableHead>
|
|
|
- <TableHead>残疾类型</TableHead>
|
|
|
- <TableHead>残疾等级</TableHead>
|
|
|
- <TableHead>操作</TableHead>
|
|
|
- </TableRow>
|
|
|
- </TableHeader>
|
|
|
- <TableBody>
|
|
|
- {selectedPersons.map((person) => (
|
|
|
- <TableRow key={person.id}>
|
|
|
- <TableCell className="font-medium">{person.name}</TableCell>
|
|
|
- <TableCell>{person.gender}</TableCell>
|
|
|
- <TableCell>{person.disabilityId}</TableCell>
|
|
|
- <TableCell>{person.disabilityType}</TableCell>
|
|
|
- <TableCell>{person.disabilityLevel}</TableCell>
|
|
|
- <TableCell>
|
|
|
- <Button
|
|
|
- variant="ghost"
|
|
|
- size="sm"
|
|
|
- onClick={() => handleRemovePerson(person.id)}
|
|
|
- >
|
|
|
- <X className="h-4 w-4" />
|
|
|
- </Button>
|
|
|
- </TableCell>
|
|
|
- </TableRow>
|
|
|
- ))}
|
|
|
- </TableBody>
|
|
|
- </Table>
|
|
|
- </div>
|
|
|
- )}
|
|
|
- <FormMessage>
|
|
|
- {form.formState.errors.persons?.message}
|
|
|
- </FormMessage>
|
|
|
- </CardContent>
|
|
|
- </Card>
|
|
|
-
|
|
|
- {/* 已选择人员详情 */}
|
|
|
- {selectedPersons.length > 0 && (
|
|
|
- <Card>
|
|
|
- <CardHeader>
|
|
|
- <CardTitle className="text-lg">设置人员信息</CardTitle>
|
|
|
- <CardDescription>
|
|
|
- 为每个残疾人设置角色和备注信息
|
|
|
- </CardDescription>
|
|
|
- </CardHeader>
|
|
|
- <CardContent>
|
|
|
- <div className="space-y-4">
|
|
|
- {selectedPersons.map((person) => (
|
|
|
- <Card key={person.id} className="border">
|
|
|
- <CardHeader className="py-3">
|
|
|
- <CardTitle className="text-base flex items-center justify-between">
|
|
|
- <div className="flex items-center">
|
|
|
- <User className="mr-2 h-4 w-4" />
|
|
|
- {person.name}
|
|
|
- </div>
|
|
|
- <Badge variant="outline">
|
|
|
- {person.disabilityType} {person.disabilityLevel}
|
|
|
- </Badge>
|
|
|
- </CardTitle>
|
|
|
- <CardDescription>
|
|
|
- 残疾证号: {person.disabilityId} | 联系电话: {person.phone}
|
|
|
- </CardDescription>
|
|
|
- </CardHeader>
|
|
|
- <CardContent className="py-3">
|
|
|
- <div className="grid grid-cols-3 gap-4">
|
|
|
- <div>
|
|
|
- <FormLabel className="text-sm">入职日期</FormLabel>
|
|
|
- <Input
|
|
|
- type="datetime-local"
|
|
|
- defaultValue={new Date().toISOString().slice(0, 16)}
|
|
|
- onChange={(e) => handlePersonDetailChange(person.id, 'joinDate', e.target.value + ':00.000Z')}
|
|
|
- data-testid={`join-date-input-${person.id}`}
|
|
|
- />
|
|
|
- </div>
|
|
|
- <div>
|
|
|
- <FormLabel className="text-sm">薪资详情</FormLabel>
|
|
|
- <Input
|
|
|
- placeholder="请输入薪资详情"
|
|
|
- defaultValue="待定"
|
|
|
- onChange={(e) => handlePersonDetailChange(person.id, 'salaryDetail', e.target.value)}
|
|
|
- data-testid={`salary-detail-input-${person.id}`}
|
|
|
- />
|
|
|
- </div>
|
|
|
- <div>
|
|
|
- <FormLabel className="text-sm">工作状态</FormLabel>
|
|
|
- <Input
|
|
|
- placeholder="在职"
|
|
|
- defaultValue="在职"
|
|
|
- onChange={(e) => handlePersonDetailChange(person.id, 'workStatus', e.target.value)}
|
|
|
- data-testid={`work-status-input-${person.id}`}
|
|
|
- />
|
|
|
- </div>
|
|
|
- <div>
|
|
|
- <FormLabel className="text-sm">角色</FormLabel>
|
|
|
- <Input
|
|
|
- placeholder="请输入角色(如:操作员、质检员等)"
|
|
|
- onChange={(e) => handlePersonDetailChange(person.id, 'role', e.target.value)}
|
|
|
- data-testid={`role-input-${person.id}`}
|
|
|
- />
|
|
|
- </div>
|
|
|
- <div>
|
|
|
- <FormLabel className="text-sm">备注</FormLabel>
|
|
|
- <Input
|
|
|
- placeholder="请输入备注信息"
|
|
|
- onChange={(e) => handlePersonDetailChange(person.id, 'remark', e.target.value)}
|
|
|
- data-testid={`remark-input-${person.id}`}
|
|
|
- />
|
|
|
- </div>
|
|
|
- </div>
|
|
|
- </CardContent>
|
|
|
- </Card>
|
|
|
- ))}
|
|
|
- </div>
|
|
|
- </CardContent>
|
|
|
- </Card>
|
|
|
- )}
|
|
|
-
|
|
|
- <Separator />
|
|
|
-
|
|
|
- <DialogFooter>
|
|
|
- <Button
|
|
|
- type="button"
|
|
|
- variant="outline"
|
|
|
- onClick={() => onOpenChange(false)}
|
|
|
- disabled={isSubmitting}
|
|
|
- >
|
|
|
- 取消
|
|
|
- </Button>
|
|
|
- <Button
|
|
|
- type="submit"
|
|
|
- disabled={isSubmitting || selectedPersons.length === 0}
|
|
|
- >
|
|
|
- {isSubmitting ? '提交中...' : `添加 ${selectedPersons.length} 名残疾人`}
|
|
|
- </Button>
|
|
|
- </DialogFooter>
|
|
|
- </form>
|
|
|
- </Form>
|
|
|
- </DialogContent>
|
|
|
- </Dialog>
|
|
|
-
|
|
|
- {/* 残疾人选择器 */}
|
|
|
- <DisabledPersonSelector
|
|
|
- open={isSelectorOpen}
|
|
|
- onOpenChange={(newOpen) => {
|
|
|
- console.debug('PersonSelector: DisabledPersonSelector onOpenChange called with', newOpen);
|
|
|
- setIsSelectorOpen(newOpen);
|
|
|
- }}
|
|
|
- onSelect={handlePersonSelect}
|
|
|
- mode="multiple"
|
|
|
- disabledIds={selectedPersons.map(p => p.id)}
|
|
|
- />
|
|
|
- </>
|
|
|
- );
|
|
|
-};
|
|
|
-
|
|
|
-export default PersonSelector;
|