training_data/apps/web/src/app/main/staffpage/staffmodal/page.tsx

173 lines
6.8 KiB
TypeScript
Raw Normal View History

2025-03-12 19:38:39 +08:00
import { api, useStaff } from "@nice/client";
2025-03-12 11:45:18 +08:00
import { useMainContext } from "../../layout/MainProvider";
import toast from "react-hot-toast";
import { Button, Form, Input, Modal, Select } from "antd";
2025-03-12 20:48:02 +08:00
import { useEffect } from "react";
2025-03-24 20:16:37 +08:00
import TrainContentTreeSelect from "@web/src/components/models/trainContent/train-content-tree-select";
import DepartmentChildrenSelect from "@web/src/components/models/department/department-children-select";
2025-03-12 11:45:18 +08:00
export default function StaffModal() {
2025-04-06 19:44:22 +08:00
const { data: traincontents } = api.trainSituation.findMany.useQuery({
select: {
2025-03-12 19:54:33 +08:00
id: true,
2025-04-06 19:44:22 +08:00
trainContent: {
select: {
2025-03-12 20:48:02 +08:00
id: true,
title: true,
type: true,
}
},
2025-03-12 19:39:24 +08:00
}
});
2025-03-24 20:16:37 +08:00
// useEffect(() => {
// traincontents?.forEach((situation)=>{
// console.log(situation.id);
// });
// }, [traincontents]);
2025-03-12 20:48:02 +08:00
2025-03-24 20:16:37 +08:00
const { form, formValue, setVisible, visible, editingRecord, setEditingRecord } = useMainContext()
2025-03-12 11:45:18 +08:00
const { create, update } = useStaff();
const handleOk = async () => {
const values = await form.getFieldsValue();
try {
2025-03-24 20:16:37 +08:00
const staffData = {
username: values.username,
showname: values.username,
department: values.deptId ? {
connect: { id: values.deptId }
} : undefined,
position: values.positionId ? {
connect: { id: values.positionId }
} : undefined,
absent: values.absent,
trainSituations: values.trainSituations?.length > 0 ? {
create: values.trainSituations.map((situation) => ({
trainContent: {
connect: { id: situation.trainContentId }
2025-03-12 11:45:18 +08:00
},
2025-03-24 20:16:37 +08:00
mustTrainTime: parseFloat(situation.mustTrainTime) || 0,
alreadyTrainTime: 0,
score: 0
}))
} : undefined
};
if (editingRecord?.id) {
await update.mutateAsync({
where: { id: editingRecord.id },
data: {
...staffData,
trainSituations: {
deleteMany: {},
create: values.trainSituations?.map((situation) => ({
trainContent: {
connect: { id: situation.trainContentId }
},
mustTrainTime: parseFloat(situation.mustTrainTime) || 0,
alreadyTrainTime: situation.alreadyTrainTime || 0,
// score: situation.score || 0
})) || []
2025-03-12 11:45:18 +08:00
}
}
2025-03-24 20:16:37 +08:00
});
} else {
await create.mutateAsync({
data: staffData
});
2025-03-12 11:45:18 +08:00
}
2025-04-06 19:44:22 +08:00
2025-03-12 11:45:18 +08:00
toast.success("保存成功");
setVisible(false);
2025-03-24 20:16:37 +08:00
setEditingRecord(null);
form.resetFields();
2025-03-12 11:45:18 +08:00
} catch (error) {
2025-03-24 20:16:37 +08:00
console.error("保存失败:", error);
2025-03-12 11:45:18 +08:00
toast.error("保存失败");
}
};
const handleCancel = () => {
setVisible(false);
2025-03-24 20:16:37 +08:00
setEditingRecord(null);
form.resetFields();
2025-03-12 11:45:18 +08:00
};
2025-03-24 20:16:37 +08:00
useEffect(() => {
2025-04-06 19:44:22 +08:00
if (visible && editingRecord) {
2025-03-24 20:16:37 +08:00
form.setFieldsValue(editingRecord);
}
2025-04-06 19:44:22 +08:00
}, [visible, editingRecord]);
2025-03-12 11:45:18 +08:00
return (
<>
<Modal
title="编辑员工信息"
2025-04-06 19:44:22 +08:00
open={visible}
2025-03-12 11:45:18 +08:00
onOk={handleOk}
onCancel={handleCancel}
>
<Form
form={form}
initialValues={formValue}
>
<Form.Item
name={"username"}
label="姓名"
>
<Input className="rounded-lg" />
</Form.Item>
<Form.Item
name={"deptId"}
label="部门"
>
2025-03-24 20:16:37 +08:00
<DepartmentChildrenSelect></DepartmentChildrenSelect>
2025-03-12 19:38:39 +08:00
</Form.Item>
<Form.Item
2025-03-24 20:16:37 +08:00
name={["position", "type"]}
2025-03-12 19:38:39 +08:00
label="职务"
>
<Input className="rounded-lg" />
2025-03-12 11:45:18 +08:00
</Form.Item>
<Form.Item
name={"absent"}
label="在位"
>
<Select className="rounded-lg" >
<Select.Option value={true}></Select.Option>
<Select.Option value={false}></Select.Option>
</Select>
</Form.Item>
2025-04-06 19:44:22 +08:00
2025-03-12 11:45:18 +08:00
<Form.List name="trainSituations">
{(fields, { add, remove }) => (
<>
{fields.map(({ key, name, ...restField }) => (
<div key={key} className="flex space-x-2">
<Form.Item
{...restField}
2025-03-24 20:16:37 +08:00
name={[name, 'trainContentId']} // 从 trainContent 改为 trainContentId
2025-03-12 11:45:18 +08:00
label="培训内容"
className="flex-1"
>
2025-04-06 19:44:22 +08:00
<TrainContentTreeSelect />
2025-03-12 11:45:18 +08:00
</Form.Item>
<Form.Item
{...restField}
name={[name, 'mustTrainTime']}
label="需训时长"
className="flex-1"
>
<Input type="number" />
</Form.Item>
<Button onClick={() => remove(name)}></Button>
</div>
))}
<Button onClick={() => add()}></Button>
</>
)}
</Form.List>
</Form>
</Modal>
2025-03-24 20:16:37 +08:00
{/* <Button onClick={() =>{console.log(traincontents);}}>TEST</Button> */}
2025-03-12 11:45:18 +08:00
</>
)
}