import React,{ Component } from "react";
import { Input,Checkbox,Table, Divider, Tooltip,Spin } from "antd";
import CourseLayoutcomponent from '../common/CourseLayoutComponent'
import Titlesearchsection from '../common/titleSearch/TitleSearchSection'
import ColorCountText from '../common/titleSearch/ColorCountText'
import update from 'immutability-helper'
import { WordsBtn, ConditionToolTip, on, off } from 'educoder'
import axios from 'axios'
import _ from 'lodash'
// import { RouteHOC } from './common.js'
import '../css/members.css'
import { from } from "array-flatten";
// import AddTeacherModal from './modal/AddTeacherModal'
// import AddStudentModal from './modal/AddStudentModal'
import AddGraduationGroupModal from './modal/AddGraduationGroupModal'
import AddAdminModal from './modal/AddAdminModal'
import { ROLE_TEACHER_NUM, ROLE_ASSISTANT_NUM } from './common'
const Search = Input.Search;
const ROLE_ADMIN = "管理员"
const ROLE_TEACHER = "教师"
const ROLE_TEACHER_ASSISTANT = "助教"
const pageSize = 20;
function buildColumns(that) {
const isAdmin = that.props.isAdmin()
const isAdminOrCreator = that.props.isAdminOrCreator();
const isAdminOrTeacher = that.props.isAdminOrTeacher()
const { course_groups } = that.state
const columns = [{
title: '序号',
// dataIndex: 'name',
key: 'index',
render: (content, item, index) => {
return {(that.state.page - 1) * 20 + index + 1}
}
}, {
title: '姓名',
dataIndex: 'name',
width: 160,
key: 'name',
render: (name, record) => {
return {name}
}
}, {
title: '角色',
dataIndex: 'role',
key: 'role',
}]
that.course_groups && that.course_groups.length && columns.push({
title: 管理权限,
width: 230,
key: 'course_groups',
dataIndex: 'course_groups',
// onClick={() => that.joinCourseGroup(item.id)}
// "right":"0px",
render: (arg_course_groups, item, index) => {
if (!arg_course_groups) {
return ''
}
// ((!course_groups || course_groups.length == 0) &&
暂未有分班信息,不能操作
)
const noGroups = (!course_groups || course_groups.length == 0);
return (
{ arg_course_groups.length == 0 ? '不限' : arg_course_groups.map(item => item.name).join(', ') }
{ isAdmin &&
{!noGroups && }
}
)
}
,
});
const hasGraduationModule = that.hasGraduationModule()
if (hasGraduationModule) {
columns.push({
title: '答辩组',
key: 'graduation_group',
dataIndex: 'graduation_group',
render: text => (
{text}
),
})
}
if (isAdminOrTeacher) {
columns.push({
title: '操作',
key: 'action',
render: (text, record) => {
if (record.application_id) {
return (
that.onRefuse(record)} style={'grey'}>拒绝
that.onAgree(record)} style={{color: '#4CACFF'}}>同意
)
} else {
return (
{record.role != ROLE_ADMIN && that.onDelete(record)} style={'grey'}>删除}
{(record.role == ROLE_TEACHER || record.role == ROLE_TEACHER_ASSISTANT || isAdminOrCreator) && record.role != ROLE_ADMIN
&& }
{ record.role == ROLE_TEACHER ? that.changeToAssistant(record)}>变更为助教 : '' }
{ record.role == ROLE_TEACHER_ASSISTANT ? that.changeToTeacher(record)}>变更为教师 : '' }
{ record.role == ROLE_ADMIN && isAdminOrCreator ? that.showChangeAdminModal(record)}>更换管理员 : '' }
)
}
},
})
}
if(isAdmin && hasGraduationModule) {
columns.unshift({
title: '',
dataIndex: 'course_member_id',
key: 'course_member_id',
render: (content, item, index) => {
return
}
})
}
return columns
}
const ORDER_BY_NAME = 1;
const ORDER_BY_DATE = 2;
const ORDER_BY_GRADUATION_GROUP = 3;
class studentsList extends Component{
constructor(props){
super(props);
this.state={
headIndex:"1",
page:1,
totalPage:undefined,
searchValue:"",
order: ORDER_BY_DATE,
search:"",
groupList:undefined,
teachers: [],
checkBoxValues: [],
isSpin:false,
application_list: []
}
}
inputSearch=(e)=>{
this.setState({
searchValue:e.target.value
})
}
// approval 2 - 拒绝
onAgree = (record, approval = 1) => {
const courseId = this.props.match.params.coursesId
let url = `/courses/${courseId}/teacher_application_review.json`
axios.post(url, {
user_id: record.user_id,
application_id: record.application_id,
approval: approval
})
.then((response) => {
if (response.data.status == 0) {
this.props.showNotification(`已${approval == 1? '同意' : '拒绝'}`)
this.fetchAll(1)
}
})
.catch(function (error) {
console.log(error);
});
}
onRefuse = (record) => {
this.onAgree(record, 2)
}
componentDidMount(){
this.setState({
isSpin:true
})
this.fetchAll(1);
const isAdminOrTeacher = this.props.isAdminOrTeacher()
const isAdmin = this.props.isAdmin()
isAdminOrTeacher && this.getGroupList();
this.getCourseGroups();
on('addTeacherSuccess', this.addTeacherSuccessListener)
isAdmin && on('updateNavSuccess', this.updateNavSuccess)
}
componentWillUnmount() {
off('addTeacherSuccess', this.addTeacherSuccessListener)
const isAdmin = this.props.isAdmin()
isAdmin && off('updateNavSuccess', this.updateNavSuccess)
}
updateNavSuccess = () => {
this.getCourseGroups()
}
addTeacherSuccessListener = (e, data) => {
// const params = JSON.parse(data)
// const coursesId = this.props.match.params.coursesId
if (window.location.pathname.endsWith('teachers')) {
this.fetchAll(1)
} else {
// this.props.history.push(`/courses/${coursesId}/teachers`)
}
// console.log('addTeacherSuccessListener', data)
}
getCourseGroups = () => {
const courseId = this.props.match.params.coursesId
let url = `/courses/${courseId}/all_course_groups.json`
axios.get(url, {
params: { all: true }
})
.then((response) => {
if (response.data.course_groups && response.data.course_groups.length) {
let course_groups_map = {}
response.data.course_groups.forEach(item => {
course_groups_map[item.id] = item.name
})
this.setState({
course_groups: response.data.course_groups,
course_groups_map
})
} else {
}
})
.catch(function (error) {
console.log(error);
});
}
joinCourseGroup = (ids, item, index) => {
console.log('join ', ids, item)
const courseId = this.props.match.params.coursesId
let url = `/courses/${courseId}/set_course_group.json`
axios.post(url, {
course_group_ids: ids,
user_id: item.user_id,
course_member_id: item.course_member_id
})
.then((response) => {
if (response.data.status == 0) {
this.props.showNotification('修改成功')
const newArray = ids.map((item) => {return {id: item, name: this.state.course_groups_map[item]}});
this.setState(
(prevState) => ({
teachers : update(prevState.teachers, {[ this.state.page == 1 ? index - this.state.application_list.length : index]: { course_groups: {$set: newArray }}})
}))
// this.fetchAll()
} else {
}
})
.catch(function (error) {
console.log(error);
});
}
fetchAll=(argPage)=>{
this.setState({
isSpin:true
})
let id = this.props.match.params.coursesId
if (argPage) {
this.setState({ page: argPage })
}
let page = argPage || this.state.page
let { order, searchValue }=this.state
let url=`/courses/${id}/teachers.json?order=${order}&page=${page}`;
if(searchValue!=""){
url+='&search='+searchValue;
}
axios.get((url)).then((result)=>{
if (result.data.teacher_list) {
this.setState({
teachers: result.data.teacher_list,
total_count: result.data.teacher_list_size,
application_list: result.data.application_list,
is_admin: result.data.is_admin,
isSpin:false
})
}
}).catch((error)=>{
console.log(error);
this.setState({
isSpin:false
})
})
}
getGroupList(){
let id = this.props.match.params.coursesId
let url='/courses/'+id+'/graduation_group_list.json';
axios.get((url)).then((result)=>{
if(result.status==200){
this.setState({
groupList:result.data
})
}
}).catch((error)=>{
console.log(error);
})
}
onAddGraduationGroupOk = () => {
this.getGroupList()
}
// join_graduation_group
joinGraduationGroup = (graduation_group_id) => {
const courseId = this.props.match.params.coursesId
let url= `/courses/${courseId}/join_graduation_group.json`;
axios.post(url, {
course_member_list: this.state.checkBoxValues.map (item => { return { course_member_id: item } }),
graduation_group_id: graduation_group_id
}).then((result)=>{
if(result.data.status==0){
this.props.showNotification('操作成功。')
this.fetchAll()
}
}).catch((error)=>{
console.log(error);
})
}
onInputSearchChange = (e) => {
this.setState({
searchValue: e.target.value
})
if (this.timeoutHandler) {
clearTimeout(this.timeoutHandler)
}
this.timeoutHandler = setTimeout(() => {
this.fetchAll(1)
}, 1200)
}
onPressEnter = (e) => {
this.fetchAll(1)
}
onPageChange = (page, pageSize) => {
this.fetchAll(page)
this.setState({ checkAllValue: false })
}
onCheckBoxChange = (checkedValues) => {
this.setState({
checkBoxValues: checkedValues,
checkAllValue: checkedValues.length == this.state.teachers.length
})
}
onCheckAll = (e) => {
this.setState({
checkAllValue: e.target.checked
})
const values = this.state.teachers.map(item => {
return item.course_member_id
})
if (e.target.checked) {
const concated = this.state.checkBoxValues.concat(values);
const sortedUniqed = _.uniq(concated)
this.setState({
checkBoxValues: sortedUniqed
})
} else {
this.setState({
checkBoxValues: _.difference(this.state.checkBoxValues, values)
})
}
}
onSortTypeChange = (order) => {
this.setState({ order: order }, () => {
this.fetchAll()
})
}
changeRole = (member, role) => {
const courseId = this.props.match.params.coursesId
let url= `/courses/${courseId}/change_course_teacher.json`;
axios.post(url, {
course_member_id: member.course_member_id
// "user_list": [
// { "user_id": member.user_id }
// ],
// "graduation_group_id": member.graduation_group_id,
// "course_group_id": "820",
// "role": role
}).then((result)=>{
if(result.data.status==0){
this.props.showNotification('操作成功。')
this.fetchAll()
}
}).catch((error)=>{
console.log(error);
})
}
changeToAssistant = (member) => {
this.changeRole(member, ROLE_ASSISTANT_NUM)
}
changeToTeacher = (member) => {
this.changeRole(member, ROLE_TEACHER_NUM)
}
showChangeAdminModal = () => {
this.refs.addAdminModal.setVisible(true)
}
changeAdminSuccess = () => {
this.fetchAll()
}
onDelete = (member) => {
this.props.confirm({
content: `确认要将“${member.name}”从教师列表中移除吗?`,
onOk: () => {
// const cid = this.props.match.params.coursesId
const courseId = this.props.match.params.coursesId
const url = `/courses/${courseId}/delete_course_teacher.json`
axios.post(url, {
course_member_id: member.course_member_id
})
.then((response) => {
if (response.data.status == 0) {
// {"status":1,"message":"删除成功"}
this.props.showNotification('删除成功')
this.fetchAll()
}
})
.catch(function (error) {
console.log(error);
});
}
})
}
addTeacher = () => {
this.refs.addTeacherModal.setVisible(true)
}
addStudent = () => {
this.refs.addStudentModal.setVisible(true)
}
hasGraduationModule = () => {
const { course_modules } = this.props;
const result = course_modules && course_modules.filter( item => {
return item.type == 'graduation'
})
return result && result.length > 0
}
render(){
const isAdmin = this.props.isAdmin()
const columns = buildColumns(this)
let {
searchValue, checkBoxValues, checkAllValue,
groupList, total_count, teachers, order, page
}=this.state
let currentOrderName = '加入时间排序'
if (order == ORDER_BY_NAME) {
currentOrderName = '姓名排序'
} else if (order == ORDER_BY_GRADUATION_GROUP) {
currentOrderName = '答辩组排序'
}
const combineArray = teachers.slice(0)
if (page == 1) {
this.state.application_list && this.state.application_list.slice(0).reverse().forEach(item => {
combineArray.unshift(item)
})
}
const isAdminOrTeacher = this.props.isAdminOrTeacher()
const hasGraduationModule = this.hasGraduationModule()
return(
{/*
*/}
{isAdminOrTeacher && }
= 10}
searchPlaceholder={ '请输入姓名进行搜索' }
firstRowRight={
{/* { isAdmin && this.addTeacher()}>添加教师 }
{ isAdmin && this.addStudent()}>添加学生 } */}
}
secondRowLeft={
total_count ? : ''
}
onPressEnter={this.onPressEnter}
>
{ isAdmin && hasGraduationModule &&
已选 {checkBoxValues.length} 个 }
{
{ hasGraduationModule && isAdminOrTeacher &&
加入答辩组
{
groupList && groupList.graduation_groups_count>10?
(
{this.setState({graduationGroupSearchValue: e.target.value})}} allowClear/>
):
(groupList && groupList.graduation_groups_count==0 && 暂无数据
)
}
{
groupList && groupList.graduation_group_list && groupList.graduation_group_list.filter((item)=> {
return (!this.state.graduationGroupSearchValue || item.name.indexOf(this.state.graduationGroupSearchValue) != -1)
}).map((item,key)=>{
return(
- this.joinGraduationGroup(item.id)}>{item.name}
)
})
}
this.refs['addGraduationGroupModal'].setVisible(true)}
>添加答辩组...
}
{currentOrderName}
- this.onSortTypeChange(ORDER_BY_NAME)} >姓名排序
- this.onSortTypeChange(ORDER_BY_DATE)} style={{width: '125px'}}>加入时间排序
{this.hasGraduationModule() && - this.onSortTypeChange(ORDER_BY_GRADUATION_GROUP)} >答辩组排序
}
}
{/* pagination={{ current: page, total: total_count, pageSize:20, onChange: this.onPageChange }} */}
20 ? { //分页
total: page == 1 && this.state.application_list.length ? total_count + total_count / Math.floor(this.state.application_list.length + 20) : total_count , //数据总数量
pageSize: page == 1 && this.state.application_list.length ? this.state.application_list.length + 20 : 20, //显示几条一页
current: page,
onChange: this.onPageChange
} : false}
>
)
}
}
export default studentsList;