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

168 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 19:38:39 +08:00
import DepartmentSelect from "@web/src/components/models/department/department-select";
2025-03-12 20:48:02 +08:00
import { useEffect } from "react";
2025-03-12 11:45:18 +08:00
export default function StaffModal() {
2025-03-12 19:54:33 +08:00
const { data: traincontents} = api.trainSituation.findMany.useQuery({
2025-03-12 20:48:02 +08:00
2025-03-12 19:54:33 +08:00
select:{
id: true,
2025-03-12 20:48:02 +08:00
trainContent:{
select:{
id: true,
title: true,
type: true,
}
},
2025-03-12 19:39:24 +08:00
}
});
2025-03-12 20:48:02 +08:00
useEffect(() => {
traincontents?.forEach((situation)=>{
console.log(situation.trainContent.title);
});
}, [traincontents]);
2025-03-12 11:45:18 +08:00
const { form, formValue, setVisible, visible, editingRecord } = useMainContext()
const { create, update } = useStaff();
const handleOk = async () => {
const values = await form.getFieldsValue();
console.log(values.username);
try {
if (editingRecord && editingRecord.id) {
const result = await update.mutateAsync(
{
where: {
id: editingRecord.id,
},
data: {
username: values.username,
deptId: values.deptId,
position: values.position,
absent: values.absent,
trainSituations: values.trainSituations ? {
upsert: values.trainSituations.map((situation) => ({
where: { id: situation.id || "" },
update: {
mustTrainTime: situation.mustTrainTime,
trainContent: { connect: { id: situation.trainContentId } },
// 其他字段...
},
}))
} : undefined,
updatedAt: new Date()
} as any
}
);
// console.log(result);
} else {
await create.mutateAsync(
{
data: {
username: values.username,
deptId: values.deptId,
createdAt: new Date(),
showname: values.username,
absent: values.absent,
trainSituations: {
create: values.trainSituations.map((situation) => ({
trainContent: { connect: { id: situation.trainContentId } },
mustTrainTime: situation.mustTrainTime,
// 其他必填字段...
}))
}
}
}
);
}
toast.success("保存成功");
setVisible(false);
} catch (error) {
toast.error("保存失败");
throw error;
}
};
const handleCancel = () => {
setVisible(false);
};
return (
<>
{/* 模态框样式更新 */}
<Modal
title="编辑员工信息"
visible={visible}
onOk={handleOk}
onCancel={handleCancel}
>
<Form
form={form}
initialValues={formValue}
>
<Form.Item
name={"username"}
label="姓名"
// labelClassName="text-gray-300"
>
<Input className="rounded-lg" />
</Form.Item>
<Form.Item
name={"deptId"}
label="部门"
// labelClassName="text-gray-300"
>
2025-03-12 19:38:39 +08:00
<DepartmentSelect></DepartmentSelect>
</Form.Item>
<Form.Item
name={"positionId"}
label="职务"
// labelClassName="text-gray-300"
>
<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-03-12 19:38:39 +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-12 20:48:02 +08:00
name={[name, 'trainContent']}
2025-03-12 11:45:18 +08:00
label="培训内容"
className="flex-1"
>
2025-03-12 20:49:28 +08:00
<TrainContentTreeSelect></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-12 20:48:02 +08:00
<Button onClick={() =>{console.log(traincontents);}}>TEST</Button>
2025-03-12 11:45:18 +08:00
</>
)
}