import React from 'react'; import { Form, Input, Button, Row, Col } from 'antd'; import './index.less'; import Page from '@src/containers/Page'; import Block from '@src/components/Block'; import Select from '@src/components/Select'; import { formatFormError, bindSearch, formatDate, generateSearch } from '@src/services/Tools'; import { asyncSMessage, asyncForm } from '@src/services/AsyncTools'; import { QuestionType } from '../../../../Constant'; import Association from '../../../components/Association'; import { Preview } from '../../../stores/preview'; import { Course } from '../../../stores/course'; import { User } from '../../../stores/user'; export default class extends Page { initState() { return { questionType: QuestionType, }; } init() { this.onlineList = [{ key: 'courseTime', type: 'select', select: [], name: '时间段', required: true, }, { key: 'time', type: 'daterange', name: '完成时间', }]; this.vsList = [{ key: 'userId', type: 'select', select: [], name: '学生', required: true, }, { key: 'time', type: 'daterange', name: '完成时间', }, { key: 'title', type: 'input', name: '作业标题', required: true, }]; } initData() { const { id } = this.params; const { form } = this.props; const { module } = this.state.search; let handler; if (id) { handler = Preview.get({ id }); } else { handler = Promise.resolve({ questionNoIds: [], courseModule: module }); } handler .then(result => { const { questionNoIds } = result; generateSearch('courseId', {}, this, (search) => { return Course.list(Object.assign({ courseModule: result.courseModule }, search)); }, (row) => { return { title: row.title, value: row.id, }; }, result.courseId, null); form.getFieldDecorator('courseNo'); form.setFieldsValue(result); this.setState({ module: result.courseModule, data: result, questionNoIds }); this.refresh(); if (result.courseId) this.refreshCourse(result.courseId); if (result.questionType) this.onRefreshType(result.questionType); }); } refresh() { const { data } = this.state; switch (data.courseModule) { case 'video': this.refreshNo(data.courseId); break; case 'online': bindSearch(this.onlineList, 'courseTime', this, (search) => { return Course.listTime(Object.assign({ courseId: data.courseId }, search)); }, (row) => { return { title: `${formatDate(row.startTime, 'YYYY-MM-DD')}~${formatDate(row.endTime, 'YYYY-MM-DD')}`, value: row.id, }; }, null, null); break; case 'vs': break; default: } } refreshCourse(id) { if (!id) return; Course.get({ id }).then((info) => { const { data } = this.state; // 设置长难句paperModule信息 data.paperModule = info.extend === 'sentence' ? 'sentence' : 'exercise'; data.courseId = id; this.setState({ data }); if (data.courseModule === 'video') { this.refreshNo(id); } this.refreshType(info.extend); }); } refreshNo(id) { if (!id) return; Course.allNo({ courseId: id }).then(result => { this.setState({ courseNo: result.map((row) => { return { title: `${row.title}(${row.no})`, value: row.no, }; }), }); }); } refreshType(questionType) { if (questionType) { const { setFieldsValue } = this.props.form; setFieldsValue({ questionType }); this.setState({ questionType: QuestionType.filter(row => row.value === questionType) }); } else { this.setState({ questionType: QuestionType }); } } onRefreshType(questionType) { this.setState({ currentQuestionType: questionType }); } submit() { const { form } = this.props; form.validateFields((err) => { if (!err) { const data = form.getFieldsValue(); let handler; data.questionNoIds = this.state.questionNoIds; data.paperModule = this.state.data.paperModule; data.courseModule = this.state.data.courseModule; if (!data.id) { handler = Preview.add(data); } else { handler = Preview.edit(data); } handler.then((result) => { asyncSMessage('保存成功'); if (data.id) { linkTo(`/course/preview/detail/${data.id}`); } else { linkTo(`/course/preview/detail/${result.id}`); } }).catch((e) => { if (e.result) { form.setFields(formatFormError(data, e.result)); } else { asyncSMessage(e.message, 'error'); } }); } }); } addOnlineAction() { const { data } = this.state; asyncForm('添加', this.onlineList, {}, info => { if (info.time && info.time.length > 0) { info.time[0].local(); info.time[1].local(); info.startTime = info.time[0].format('YYYY-MM-DDT00:00:00Z'); info.endTime = info.time[1].format('YYYY-MM-DDT00:00:00Z'); } return Preview.addAssign(Object.assign({ paperModule: data.paperModule, paperId: data.id, courseModule: data.courseModule }, info)).then(() => { asyncSMessage('添加成功!'); this.refresh(); }); }).catch(err => { console.log(err); }); } addVsAction() { const { data } = this.state; asyncForm('添加', this.vsList, {}, info => { if (info.time && info.time.length > 0) { info.time[0].local(); info.time[1].local(); info.startTime = info.time[0].format('YYYY-MM-DDT00:00:00Z'); info.endTime = info.time[1].format('YYYY-MM-DDT00:00:00Z'); } return User.listCourseAppointment({ courseId: data.courseId, userId: info.userId, startTime: info.startTime, endTime: info.endTime }).then(result => { if (result.total === 0) { return Promise.reject(new Error('没有找到相关的预约记录')); } if (result.total > 1) { return Promise.reject(new Error('时间段内存在多条预约记录')); } return Preview.addAssign(Object.assign({ paperModule: data.paperModule, paperId: data.id, courseModule: data.courseModule, courseAppointment: result.list[0].id }, info)).then(() => { asyncSMessage('添加成功!'); this.refresh(); }); }); }).then((form) => { bindSearch(this.vsList, 'userId', form, (search) => { return User.list(search); }, (row) => { return { title: `${row.nickname}(${row.mobile})`, value: row.id, }; }, null, null); }).catch(err => { console.log(err); }); } renderBase() { const { data = {}, questionNoIds, questionType, currentQuestionType } = this.state; const { getFieldDecorator } = this.props.form; return
{getFieldDecorator('id')()} {getFieldDecorator('courseId', { rules: [ { required: true, message: '请选择课程' }, ], })( , )} } {getFieldDecorator('questionType', { rules: [ { required: true, message: '请选择题型' }, ], })( , )}
{data.paperModule && { this.setState({ questionNoIds: info.questionNoIds }); return Promise.resolve(); }} />}
; } renderOnline() { return

; } renderVs() { return

; } renderContent() { const { data } = this.state; switch (data.courseModule) { case 'video': return []; case 'online': return [this.renderOnline()]; case 'vs': return [this.renderVs()]; default: return
; } } renderView() { const { data = {} } = this.state; return
{this.renderBase()} {data.id > 0 && this.renderContent()}
; } }