import React, { useState, useEffect } from "react"; import { Button, Drawer, Form, Modal, message } from "antd"; import styles from "./hour-update.module.less"; import { course, courseAttachment } from "../../../api/index"; import { SelectAttachment } from "../../../compenents"; import { ExclamationCircleFilled } from "@ant-design/icons"; import { TreeAttachments } from "./attachments"; const { confirm } = Modal; interface PropInterface { id: number; open: boolean; onCancel: () => void; } export const CourseAttachmentUpdate: React.FC = ({ id, open, onCancel, }) => { const [form] = Form.useForm(); const [attachmentVisible, setAttachmentVisible] = useState(false); const [attachmentData, setAttachmentData] = useState([]); const [attachments, setAttachments] = useState([]); useEffect(() => { if (id === 0) { return; } getDetail(); }, [id, open]); const getDetail = () => { course.course(id).then((res: any) => { let treeData = res.data.attachments; if (treeData.length > 0) { const arr: any = resetAttachments(treeData).arr; const keys: any = resetAttachments(treeData).keys; setAttachmentData(arr); setAttachments(keys); } }); }; const resetAttachments = (data: any) => { const arr: any = []; const keys: any = []; if (data) { for (let i = 0; i < data.length; i++) { arr.push({ type: data[i].type, name: data[i].title, rid: data[i].rid, id: data[i].id, }); keys.push(data[i].rid); } } return { arr, keys }; }; const onFinish = (values: any) => {}; const onFinishFailed = (errorInfo: any) => { console.log("Failed:", errorInfo); }; const selectAttachmentData = (arr: any, videos: any) => { const hours: any = []; for (let i = 0; i < videos.length; i++) { hours.push({ sort: attachmentData.length + i, title: videos[i].name, type: videos[i].type, rid: videos[i].rid, }); } if (hours.length === 0) { message.error("请选择课件"); return; } console.log(hours); courseAttachment .storeCourseAttachmentMulti(id, hours) .then((res: any) => { console.log("ok"); setAttachmentVisible(false); getDetail(); }) .catch((err) => { message.error(err.message); }); }; const delAttachments = (hid: number) => { const data = [...attachmentData]; confirm({ title: "操作确认", icon: , content: "确认删除此课件?", centered: true, okText: "确认", cancelText: "取消", onOk() { const index = data.findIndex((i: any) => i.rid === hid); let delId = data[index].id; if (index >= 0) { data.splice(index, 1); } if (data.length > 0) { setAttachmentData(data); const keys = data.map((item: any) => item.rid); setAttachments(keys); } else { setAttachmentData([]); setAttachments([]); } if (delId) { courseAttachment.destroyAttachment(id, delId).then((res: any) => { console.log("ok"); }); } }, onCancel() { console.log("Cancel"); }, }); }; const transAttachments = (arr: any) => { setAttachments(arr); const data = [...attachmentData]; const newArr: any = []; const hourIds: any = []; for (let i = 0; i < arr.length; i++) { data.map((item: any) => { if (item.rid === arr[i]) { newArr.push(item); hourIds.push(item.id); } }); } setAttachmentData(newArr); courseAttachment.transCourseAttachment(id, hourIds).then((res: any) => { console.log("ok"); }); }; return ( <>

1.线上课课件调整及时生效,操作不可逆,请谨慎操作。

{ setAttachmentVisible(false); }} onSelected={(arr: any, videos: any) => { selectAttachmentData(arr, videos); }} >
{attachmentData.length === 0 && ( 请点击上方按钮添加课件 )} {attachmentData.length > 0 && ( { delAttachments(id); }} onUpdate={(arr: any[]) => { transAttachments(arr); }} /> )}
); };