import { PlusOutlined } from '@ant-design/icons'; import { Button, message, Image, Modal, } from 'antd'; import React, { useState, useRef } from 'react'; import { history } from 'umi'; import { PageContainer, FooterToolbar } from '@ant-design/pro-layout'; import type { ProColumns, ActionType } from '@ant-design/pro-table'; import ProTable from '@ant-design/pro-table'; import { ModalForm, ProFormText, ProFormTextArea } from '@ant-design/pro-form'; import type { ProDescriptionsItemProps } from '@ant-design/pro-descriptions'; import ProDescriptions from '@ant-design/pro-descriptions'; import type { FormValueType } from './components/UpdateForm'; import UpdateForm from './components/UpdateForm'; import { copySubject, addSubject, updateSubject, removeSubject, querySubjectList, queryListChapterBySubject } from './service'; import type { TableListItem, TableListPagination } from './data'; import { queryCourseListByTag } from '../option/service'; const { confirm } = Modal; /** 章节列表项定义 */ const chapterColumns: ProColumns[] = [ { title: '序号', key: 'index', valueType: 'indexBorder', width: 48, }, { title: '章节名称', dataIndex: 'chapter_name', valueType: 'text', hideInTable: false, hideInDescriptions: false, hideInForm: false, hideInSearch: true, }, { title: '简介', dataIndex: 'chapter_describe', valueType: 'textarea', sorter: false, hideInTable: false, hideInForm: false, hideInSearch: true, renderText: (val: string) => (
), }, { title: '课程', valueType: 'select', dataIndex: 'course_names', sorter: false, hideInTable: false, hideInForm: false, hideInSearch: true, fieldProps: { mode: "multiple" }, renderText: (val: string) => `${val}`, request: async (params) => { const { tags } = params; const { data: Items } = await queryCourseListByTag({ tag_ids: tags?.toString() }); console.log('queryCourseListByTag...') const courses = [] for (let i = 0; i < Items?.length; i++) { courses.push({ label: Items[i]?.course_name, value: Items[i]?.course_id }) } console.log(courses, 'courses:::'); return courses; }, }, { title: '学时', dataIndex: 'course_minutes', valueType: 'text', sorter: false, hideInTable: false, hideInForm: true, hideInSearch: true, renderText: (val: string) => `${val}学时`, }, ]; /** * 添加节点 * * @param fields */ const handleAdd = async (fields: TableListItem) => { const hide = message.loading('正在添加'); try { await addSubject({ ...fields }); hide(); message.success('添加成功'); return true; } catch (error) { hide(); message.error('添加失败请重试!'); return false; } }; /** * 更新节点 * * @param fields */ const handleUpdate = async (fields: FormValueType, currentRow?: TableListItem) => { const hide = message.loading('正在更新'); try { await updateSubject({ ...currentRow, ...fields, }); hide(); message.success('操作成功'); return true; } catch (error) { hide(); message.error('操作失败请重试!'); return false; } }; /** * 删除主题 * * @param selectedRows */ const handleRemove = async (selectedRows: TableListItem[]) => { const hide = message.loading('正在删除'); if (!selectedRows) return true; try { const { code, msg } = await removeSubject({ key: selectedRows.map((row) => row.key), }); hide(); if (code === 2000) { message.success('删除成功,即将刷新'); } else { message.warning(msg); } return true; } catch (error) { hide(); message.error('删除失败,请重试'); return false; } }; const TableList: React.FC = () => { /** 新建窗口的弹窗 */ const [createModalVisible, handleModalVisible] = useState(false); const [updateModalVisible, handleUpdateModalVisible] = useState(false); const [detailModalVisible, handleDetailModalVisible] = useState(false); const actionRef = useRef(); const actionChapterRef = useRef(); const [currentRow, setCurrentRow] = useState(); const [selectedRowsState, setSelectedRows] = useState([]); const columns: ProColumns[] = [ { title: '主题名称', dataIndex: 'subject_name', valueType: 'text', hideInTable: true, hideInDescriptions: true, hideInForm: true, hideInSearch: false, }, { title: '图片', tip: '主题封面', hideInSearch: true, render: (dom, entity) => { // console.log(entity, 'entity') return ( ); }, }, { title: '创建日期', dataIndex: 'create_time', valueType: 'dateRange', hideInTable: true, hideInForm: true, hideInSearch: false, }, { title: '主题', dataIndex: 'subject_describe', valueType: 'textarea', hideInSearch: true, width: "*", render: (dom, entity) => { return (

{entity.subject_name}

); }, }, { title: '信息', dataIndex: 'total_course_minutes', sorter: false, hideInSearch: true, width: 200, render: (dom, entity) => { if (entity.b_use === 0) { return '待发布' } return (
章节数:{entity.total_chapter_number} 个
课程数:{entity.total_course_number} 个
考核学时:{entity.total_course_minutes} 分钟
); }, }, // { // title: '课程数', // sorter: true, // dataIndex: 'total_chapter_number', // }, { title: '操作', dataIndex: 'option', valueType: 'option', width: 200, render: (_, record) => [ { setCurrentRow(record); if (actionChapterRef.current) { actionChapterRef.current.reload(); } handleDetailModalVisible(true); }} > 查看 , { history.push(`/course/subject/step/update/${record.subject_id}`) }} > 编辑 , { const hide = message.loading('正在复制'); // console.log(e, record, 'copy') const success = await copySubject({ subject_id: record.subject_id }); if (success) { hide(); // handleModalVisible(false); if (actionRef.current) { actionRef.current.reload(); } } }} > 复制 , { showConfirm(record) }}> 删除 , ], }, ]; const showConfirm = async (record) => { confirm({ title: '确认删除主题吗?', centered: true, onOk() { handleRemove([{ key: record?.subject_id }]); // 调用批量删除函数(如果接口不支持批量需要在service中处理) setSelectedRows([]); actionRef.current?.reloadAndRest?.(); }, onCancel() { }, }); }; return ( headerTitle="主题设置" actionRef={actionRef} rowKey="subject_id" options={false} search={{ labelWidth: 120, }} toolBarRender={() => [ , ]} request={async (value) => { const { create_time } = value; if (create_time) { value.begin_time = create_time[0] value.end_time = create_time[1] } const { data } = await querySubjectList({ ...value, page_number: value.current, page_size: value.pageSize }); // 解构后不用套data // console.log('datalist', dataist) return { current: data?.page_number, data: data?.list, pageSize: data?.page_size, success: true, total: data?.total_row || 0, }; }} columns={columns} rowSelection={false} /> {selectedRowsState?.length > 0 && ( 已选择{' '} {selectedRowsState.length} {' '} 项    服务调用次数总计 {selectedRowsState.reduce((pre, item) => pre + item.callNo!, 0)} 万
} > )} { const success = await handleAdd(value as TableListItem); if (success) { handleModalVisible(false); if (actionRef.current) { actionRef.current.reload(); } } }} > { setCurrentRow(undefined); // 设置当前行 handleDetailModalVisible(false); }} footer={null} centered > {currentRow?.subject_id && ( <> { console.log('step2 主题信息') return Promise.resolve({ success: true, data: { id: '这是一段文本', object: '', date: '2020-07-30 08:00', duration: '', grade: 100, through: '>60', learn: '>20 min', times: 2 }, }); }}*/ extra={false} > {currentRow?.subject_name}
headerTitle={false} actionRef={actionChapterRef} rowKey="chapter_id" options={false} search={false} toolBarRender={false} request={async (value) => { const { data } = await queryListChapterBySubject({ subject_id: currentRow?.subject_id, page_number: value?.current || 1, page_size: value?.pageSize, }); return { current: data?.page_number, data: data?.list, pageSize: data?.page_size, success: true, total: data?.total_row || 0, }; }} // dataSource={list} columns={chapterColumns} rowSelection={false} /> )} { const success = await handleUpdate(value, currentRow); if (success) { handleUpdateModalVisible(false); setCurrentRow(undefined); if (actionRef.current) { actionRef.current.reload(); } } }} onCancel={() => { handleUpdateModalVisible(false); setCurrentRow(undefined); }} updateModalVisible={updateModalVisible} values={currentRow || {}} /> ); }; export default TableList;