import React, { useEffect, useState } from 'react' import { Row, Col, Table, Button, Divider, Switch, Modal, Input, Popconfirm, message } from 'antd' import BaseTmp from '@/components/BaseTmp' import api from '@/services/api/index' import { PlusOutlined } from '@ant-design/icons' import { descText } from '@/common/MessageManage' const { getQuestionBank, addQuestionBank, delQuestionBank, putQuestionBank } = api.question const QuestionBank: React.FC = () => { const [ dataSource, setDataSource ] = useState([]) const [ isModalVisible, setIsModalVisible ] = useState(false) /**modal loading */ const [ confirmLoading, setConfirmLoading ] = useState(false) /**table loading */ const [ loading, setLoading ] = useState(false) /**编辑还是新增 */ const [ type, setType ] = useState<'edit' | 'add'>('add') const [ currentDataSource, setCurrentDataSource ] = useState<{ id: string, label: string }>({id: '', label: ''}) useEffect( () => { _getQuestionBank() }, []) const columns = [ { title: '学段名', dataIndex: 'label', key: 'label', }, { title: '教材数', dataIndex: 'textBookNum', key: 'textBookNum', }, { title: '卡片数', dataIndex: 'cardNum', key: 'cardNum', }, { title: '状态', dataIndex: 'state', key: 'state', render: () => }, { title: '操作', dataIndex: 'action', key: 'action', render: (action: any, record: any) => ( <> openModel('edit', record)} >编辑 onConfirm(action, record)} okText="是" cancelText="我点错了" > 删除 ) } ] const modelTitle = type === 'add' ? '新增学段' : '编辑学段' const handleOk = () => { setConfirmLoading(true) if (type === 'add') { _addQuestionBank() } else { _putQuestionBank() } } const onInput = (e: InputEvent) => { setCurrentDataSource({ ...currentDataSource, label: e.target.value }) } /**删除学段 */ const onConfirm = (action: any, records: any) => { _delQuestionBank(records.id) } /**修改题库 */ const _putQuestionBank = async () => { const { status, result } = await putQuestionBank(currentDataSource) setIsModalVisible(false) setConfirmLoading(false) if (status === 200) { console.log(result); setCurrentDataSource({id: '', label: ''}) _getQuestionBank() } } /**删除题库 */ const _delQuestionBank = async (id: string | number) => { const { status, result } = await delQuestionBank(id) if ( status === 200 ) { console.log(result); message.success(descText.delTextSuccess) _getQuestionBank() } } /**增加题库 */ const _addQuestionBank = async () => { setCurrentDataSource({id: '', label: ''}) const { status, result} = await addQuestionBank({ bankName: currentDataSource.label }) setConfirmLoading(false) if (status === 200) { console.log(result, 'result'); message.success(descText.addTextSuccess) setIsModalVisible(false) _getQuestionBank() } } /**打开弹窗 */ const openModel = (type: 'edit' | 'add', record?: any) => { setType(type) setIsModalVisible(true) if (type === 'edit' ) { setCurrentDataSource(record) } } /**关闭弹窗 */ const closeModel = () => { setIsModalVisible(false) } /** 查询学段 */ const _getQuestionBank = async () => { setLoading(true) const { status, result } = await getQuestionBank() setLoading(false) if ( status === 200 ) { console.log("result:", result); setDataSource(result) } } return ( ) } export default QuestionBank