import React,{ Component } from "react";
import { Input,Checkbox,Menu,Pagination } from "antd";

import HomeworkModal from '../coursesPublic/HomeworkModal'
import axios from 'axios'
import moment from 'moment'
import { getNextHalfHourOfMoment } from 'educoder'
class PublishRightnow extends Component{
  constructor(props){
    super(props);

    this.state={
      course_groups: [],
      modalname:undefined,
      modaltype:undefined,
      visible:false,
      Topval:undefined,
      Botvalleft:undefined,
      Botval:undefined,
      starttime:undefined,
      endtime:undefined,
      Cancelname:undefined,
      Savesname:undefined,
      Cancel:undefined,
      Saves:undefined,
      Topvalright:undefined
    }
  }
  open = (usingCheckBeforePost) => {
    this.usingCheckBeforePost = usingCheckBeforePost == true
    this.homeworkstart()
  }

  homeworkstart=()=>{
    const isPublish = this.props.isPublish;
    if (!this.props.checkBoxValues || this.props.checkBoxValues.length == 0) {
      this.props.showNotification(`请先选择要立即${isPublish ? "发布" : "截止"}的作业`)
      return;
    }
    
    this.fetchCourseGroups();
    
    
  }
  showDialog = () => {
    const isPublish = this.props.isPublish;
    const dateFormat = 'YYYY-MM-DD HH:mm';

    // getNextHalfHourOfMoment
    const startMoment = (moment());
    this.setState({
      modalname: isPublish ? "立即发布" : "立即截止",
      modaltype:1,
      visible:true,
      Topval: isPublish ? "学生将立即收到作业" : "学生将不能再提交作品",
      // Botvalleft: isPublish ? "暂不发布" : "暂不截止",
      Botval: this.props.fromListPage ? (isPublish ? "本操作只对“未发布”的分班有效" : "本操作只对“提交中”的分班有效") : '',
      starttime: isPublish ? `发布时间：${startMoment.format(dateFormat)}` : '',
			starttimes:isPublish ? `${startMoment.format(dateFormat)}` : '',
      endtime: isPublish ? `截止时间：${startMoment.add(1, 'months').add(1, 'hours').minutes(0).format(dateFormat)}` : '',
      Cancelname:isPublish ? "暂不发布" : "暂不截止",
      Savesname:isPublish ? "立即发布" : "立即截止",
      Cancel:this.homeworkhide,
      Saves:this.homeworkstartend,
			typs:isPublish ? "start" : "end",
    })
  }
  homeworkhide=()=>{
    this.setState({
      modalname:undefined,
      modaltype:undefined,
      visible:false,
      Topval:undefined,
      Topvalright:undefined,
      Botvalleft:undefined,
      Botval:undefined,
      starttime:undefined,
      endtime:undefined,
      Cancelname:undefined,
      Savesname:undefined,
      Cancel:undefined,
      Saves:undefined,
      StudentList_value:undefined,
      addname:undefined,
      addnametype:false,
      addnametab:undefined
    })
  }

  homeworkstartend=(arg_group_ids,endtime)=>{
    if (this.usingCheckBeforePost && this.props.checkBeforePost) {
      const goOn = this.props.checkBeforePost();
      if (!goOn) {
        this.homeworkhide();
        return;
      }
    }
    const isPublish = this.props.isPublish;
    let group_ids = arg_group_ids
    if (this.usingCheckBeforePost) {
      group_ids = this.state.course_groups.map((item) => {
        return item.id
      })
    }
    if (this.state.course_groups.length && (!group_ids || group_ids.length == 0)) {
      this.props.showNotification('请至少选择一个分班');
      return;
    }
    

    let coursesId=this.props.match.params.coursesId;
    const url = `/courses/${coursesId}/homework_commons/${isPublish ? "publish_homework" : "end_homework"}.json`
    axios.post(url, {
        group_ids,
        homework_ids: this.props.checkBoxValues,
        all_check: 0,
				end_time:endtime==="Invalid date"?undefined:endtime
      })
      .then((response) => {
        if (response.data.status == 0) {
          this.props.showNotification(isPublish ? "立即发布成功" : "立即截止成功")
          this.props.doWhenSuccess && this.props.doWhenSuccess()
          this.setState({ visible : false })
					this.props.action && this.props.action()
        }
      })
      .catch(function (error) {
        console.log(error);
      });
  }
  // componentDidUpdate = (prevProps) => {
	// 	if ( prevProps.match.params.boardId != this.props.match.params.boardId ) {
	// 		this.fetchAll(null, 1)
	// 	}
  // }
  fetchCourseGroups = () => {
    let coursesId=this.props.match.params.coursesId;
    // TODO 这里要改成单选作业，接口使用这个 https://www.showdoc.cc/127895880302646?page_id=2035541497546668
    // /homework_commons/:id/publish_groups.json
    let url = `/courses/${coursesId}/all_course_groups.json`
    if (this.props.checkBoxValues.length == 1) {
      const isPublish = this.props.isPublish;
      url = `/homework_commons/${this.props.checkBoxValues[0]}/${ isPublish ? 'publish_groups' : 'end_groups'}.json`
    }

    axios.get(url, {
      })
      .then((response) => {
        if (!response || response.data.status == -1) {
          this.setState({ visible : false })
          return;
        }
        this.showDialog()
        this.setState({
          course_groups: response.data.course_groups,
        })
      })
      .catch(function (error) {
        console.log(error);
      });
  }

  render(){
    const isPublish = this.props.isPublish;

    let{
      Topvalright,
      modalname,
      modaltype,
      visible,
      Topval,
      Botvalleft,
      Botval,
      starttime,
			starttimes,
      endtime,
      Cancelname,
      Savesname,
      Cancel,
      Saves,
      course_groups
    }=this.state

    const { showActionButton } = this.props
    return(
      <div>
				{visible===true?<HomeworkModal
          modaltype={modaltype}
          modalname={modalname}
          visible={visible}
          Topval={Topval}
          Topvalright={Topvalright}
          Botvalleft={Botvalleft}
          Botval={Botval}
          starttime={starttime}
					starttimes={starttimes}
          endtime={endtime}
          Cancelname={Cancelname}
          Savesname={Savesname}
          Cancel={Cancel}
          Saves={Saves}
          course_groups={course_groups}
          usingCheckBeforePost= {this.usingCheckBeforePost}
          onToPublishClick={this.props.onToPublishClick}
					typs={this.state.typs}
        />:""}
        { showActionButton && <a href="javascript:void(0)" className="color-grey-9" onClick={this.homeworkstart}>{ isPublish ? "立即发布" : "立即截止" }</a> }
      </div>
    )
  }
}
export default PublishRightnow;