import React, { Component } from 'react';
import { SnackbarHOC } from 'educoder';
import { Route, Switch } from 'react-router-dom';
import { withRouter } from 'react-router'
import Loadable from 'react-loadable';
import Loading from '../../Loading';
import axios from 'axios';
import { TPMIndexHOC } from '../tpm/TPMIndexHOC';
import { CNotificationHOC } from './common/CNotificationHOC'
import { ImageLayerOfCommentHOC } from '../page/layers/ImageLayerOfCommentHOC'
import "./css/Courses.css";
//引入对应跳转的组件
//里面有资源
const ListPageIndex = Loadable({
  loader: () => import('./ListPageIndex'),
  loading: Loading,
})
//课堂首页
const CoursesHome = Loadable({
  loader: () => import('./coursesHome/CoursesHome'),
  loading: Loading,
})
//新建实训
const WrappedCoursesNewApp = Loadable({
  loader: () => import('./new/CoursesNew'),
  loading: Loading,
})
//新建精品课堂
const WrappedCoursesNewAppGoldclass = Loadable({
  loader: () => import('./new/Goldsubject'),
  loading: Loading,
})
const ShixunHomeworkPage = Loadable({
  loader: () => import('./shixunHomework/ShixunHomeworkPage'),
  loading: Loading,
})
//实训作业提交总结
const CommitSummary = Loadable({
  loader: () => import('./shixunHomework/CommitSummary'),
  loading: Loading,
})
//实训报告
const ShixunWorkReport = Loadable({
  loader: () => import('./shixunHomework/ShixunWorkReport'),
  loading: Loading,
})
//实训查重详情
const ShixunWorkDetails = Loadable({
  loader: () => import('./shixunHomework/ShixunWorkDetails'),
  loading: Loading,
})
//学生列表
const Studentshavecompletedthelist = Loadable({
  loader: () => import('./exercise/Studentshavecompletedthelist'),
  loading: Loading,
})

//普通作业
const CommonWork = Loadable({
  loader: () => import('./busyWork/Index'),
  loading: Loading,
})
const GroupWork = Loadable({
  loader: () => import('./busyWork/IndexGroup'),
  loading: Loading,
})
//毕设任务列表新建
const GraduationTaskseditApp = Loadable({
  loader: () => import('./graduation/tasks/GraduationTasksedit'),
  loading: Loading,
})
//毕设任务列表编辑
const GraduationTasksnewApp = Loadable({
  loader: () => import('./graduation/tasks/GraduationTasksnew'),
  loading: Loading,
})
//毕设任务列表
const GraduationTaskssettinglist = Loadable({
  loader: () => import('./graduation/tasks/GraduationTaskDetail'),
  loading: Loading,
})
// 评阅列表
const GraduationTasksappraise = Loadable({
  loader: () => import('./graduation/tasks/GraduationTasksappraise'),
  loading: Loading,
})
//创建作品
const GraduationTasksSubmitnewApp = Loadable({
  loader: () => import('./graduation/tasks/GraduationTasksSubmitnew'),
  loading: Loading,
})
//修改作品
const GraduationTasksSubmiteditApp = Loadable({
  loader: () => import('./graduation/tasks/GraduationTasksSubmitedit'),
  loading: Loading,
})
//排序
const Ordering = Loadable({
  loader: () => import('../../modules/courses/ordering/Ordering'),
  loading: Loading,
});
class CoursesIndex extends Component {
  constructor(props) {
    super(props)
    this.state = {
      coursesids: undefined,
      coursesidsfun: this.coursesidsfun,
      updataleftNavfun: this.updataleftNav,
      course_modules: undefined,
      hidden_modules: [],
      is_teacher: false,
      isaloadtype: true,
      coursesidtype: undefined,
      locationNavfun: this.locationNav,
      unlocationNavfun: this.unlocationNavfun
    }
  }

  coursesidsfun = (id, type) => {
    this.setState({
      coursesids: id,
      coursesidtype: type
    })
  }

  componentDidMount() {
    this.historyArray = [window.location.pathname];

    this.props.history.listen(location => {
      this.historyArray.unshift(window.location.pathname);
      this.historyArray.length = 2;
    });
  }
  //更新左边课堂导航
  updataleftNav = () => {
    let query = this.props.location.pathname
    let { isaloadtype } = this.state;
    let types = isaloadtype === false ? true : false
    const type = query.split('/');
    let id = parseInt(type[2])
    if (isNaN(id)) {
      return
    }

    const querys = this.props.location.search;
    var dataqueryss = {}
    try {
      var foqus = this.foo(querys);
      if (JSON.stringify(foqus) === "{}") {
        this.setState({
          dataquerys: {},
        });
      } else {
        this.setState({
          dataquerys: foqus,
        });
        dataqueryss = foqus;
      }
    } catch (e) {
      this.setState({
        dataquerys: {},
      })
    }
    let url = "/courses/" + id + "/left_banner.json"
    axios.get(url,
      {
        params:
          dataqueryss
      }
    ).then((response) => {
      if (response != undefined) {
        if (response.data && response.data) {
          this.setState({
            course_modules: response.data.course_modules,
            hidden_modules: response.data.hidden_modules,
            is_teacher: response.data.is_teacher,
            isaloadtype: types
          })
          this.locationNav(response.data.course_modules)
        }
      }
    })
  };
  foo = (url) => {
    var json = {};
    var regExp = /[\?\&](\w+)(=?)(\w*)/g;
    var arr;
    do {
      arr = regExp.exec(url);
      if (arr) {
        var key = arr[1];
        var value = arr[3];
        if (!arr[2])
          value = undefined;

        json[key] = value;
      }
    } while (arr);

    return json;
  };

  locationNav = (list) => {
    if (list) {
      let url = this.props.location.pathname;
      const type = url.split('/');
      let filesId = parseInt(type[4]);
      var lists = list;

      lists.forEach((item, index) => {

        if (url === item.category_url) {
          this.setState({
            indexs: index,
            mainurl: item.category_url
          })
          this.coursesidsfun(item.id, "node")
        } else {
          if (item.second_category != undefined && item.second_category.length != 0) {
            item.second_category.forEach((iem, key) => {
              if (url === iem.second_category_url) {

                if (parseInt(filesId) === iem.category_id || parseInt(filesId) === item.id) {
                  this.setState({
                    indexs: index,
                    url: url,
                    mainurl: item.category_url
                  })
                }
                this.coursesidsfun(item.id, "child")
              }
            })
          }

        }

      })
    }

  }

  unlocationNavfun = (val) => {
    this.setState({
      indexs: val,
    })
  }
  render() {
    const common = {
      previousPathName: this.historyArray && this.historyArray[1]
    }
    return (
      <Switch {...this.props}>
        {/*排序*/}
        <Route path="/classrooms/:coursesId/ordering/:ordering_type/:main_id"
          render={
            (props) => (<Ordering {...this.props} {...props} {...this.state} />)
          }
        ></Route>
        {/* 资源列表页  */}
        <Route path="/classrooms/:coursesId/file/:Id" exact
          render={
            (props) => (<ListPageIndex {...this.props} {...props} {...this.state} {...common} />)
          }
        ></Route>
        <Route path="/classrooms/:coursesId/files/:main_id"
          render={
            (props) => (<ListPageIndex {...this.props} {...props} {...this.state} {...common} />)
          }
        ></Route>
        {/* 作品评阅 https://www.trustie.net/issues/19981 */}
        <Route path="/classrooms/:coursesId/graduation_tasks/:category_id/appraise"
          render={
            (props) => (<GraduationTasksappraise {...this.props} {...props} {...this.state} {...common} />)
          }
        ></Route>

        {/* 新建作品 */}
        <Route path="/classrooms/:coursesId/graduation_tasks/:category_id/works/:task_Id/new"
          render={
            (props) => (<GraduationTasksSubmitnewApp {...this.props} {...props} {...this.state} {...common} />)
          }
        ></Route>

        {/* 修改作品  */}
        <Route path="/classrooms/:coursesId/graduation_tasks/:category_id/works/:work_Id/edit"
          render={
            (props) => (<GraduationTasksSubmiteditApp {...this.props} {...props} {...this.state} {...common} />)
          }
        ></Route>

        <Route path="/classrooms/:coursesId/graduation_tasks/:category_id/detail/:task_Id"
          render={
            (props) => (<GraduationTaskssettinglist {...this.props} {...props} {...this.state} {...common} />)
          }
        ></Route>

        {/* 修改毕设任务 https://www.trustie.net/issues/19981 */}
        <Route path="/classrooms/:coursesId/graduation_tasks/:category_id/edit"
          render={
            (props) => (<GraduationTaskseditApp {...this.props} {...props} {...this.state} {...common} />)
          }
        ></Route>

        {/* 新建毕设任务 https://www.trustie.net/issues/19981 */}
        <Route path="/classrooms/:coursesId/graduation_tasks/:category_id/new"
          render={
            (props) => (<GraduationTasksnewApp {...this.props} {...props} {...this.state} {...common} />)
          }
        ></Route>

        {/* 毕设任务列表 https://www.trustie.net/issues/19981 */}
        <Route path="/classrooms/:coursesId/graduation_tasks/:Id" exact
          render={
            (props) => (<ListPageIndex {...this.props} {...props} {...this.state} {...common} />)
          }
        ></Route>

        {/* 教师列表*/}
        <Route path="/classrooms/:coursesId/teachers"
          render={
            (props) => (<ListPageIndex {...this.props} {...props} {...this.state} {...common} />)
          }
        ></Route>
        {/* 学生列表*/}
        <Route path="/classrooms/:coursesId/students"
          render={
            (props) => (<ListPageIndex {...this.props} {...props} {...this.state} {...common} />)
          }
        ></Route>
        {/* 分班列表 */}
        <Route path="/classrooms/:coursesId/course_groups/:course_group_id"
          render={
            (props) => (<ListPageIndex {...this.props} {...props} {...this.state} {...common} />)
          }
        ></Route>
        <Route path="/classrooms/:coursesId/course_groups"
          render={
            (props) => (<ListPageIndex {...this.props} {...props} {...this.state} {...common} />)
          }
        ></Route>

        {/* 普通作业 */}
        <Route path="/classrooms/:coursesId/common_homeworks/:category_id" exact
          render={
            (props) => (<ListPageIndex {...this.props} {...props} {...this.state} {...common} />)
          }
        ></Route>
        {/* 子目录普通作业 */}
        <Route path="/classrooms/:coursesId/common_homework/:category_id" exact
          render={
            (props) => (<ListPageIndex {...this.props} {...props} {...this.state} {...common} />)
          }
        ></Route>
        {/* 分组作业 */}
        <Route path="/classrooms/:coursesId/group_homeworks/:category_id" exact
          render={
            (props) => (<ListPageIndex {...this.props} {...props} {...this.state} {...common} />)
          }
        ></Route>
        {/* 分组作业 */}
        <Route path="/classrooms/:coursesId/group_homework/:category_id" exact
          render={
            (props) => (<ListPageIndex {...this.props} {...props} {...this.state} {...common} />)
          }
        ></Route>

        {/* 普通作业 */}
        <Route path="/classrooms/:coursesId/common_homeworks/" strict
          render={
            (props) => (<CommonWork {...this.props} {...props} {...this.state} {...common} />)
          }
        ></Route>
        {/* 子普通作业 */}
        <Route path="/classrooms/:coursesId/common_homework/" strict
          render={
            (props) => (<CommonWork {...this.props} {...props} {...this.state} {...common} />)
          }
        ></Route>

        {/* 分组作业 */}
        <Route path="/classrooms/:coursesId/group_homeworks/" strict
          render={
            (props) => (<GroupWork {...this.props} {...props} {...this.state} {...common} />)
          }
        ></Route>

        {/* 分组作业 */}
        <Route path="/classrooms/:coursesId/group_homework/" strict
          render={
            (props) => (<GroupWork {...this.props} {...props} {...this.state} {...common} />)
          }
        ></Route>

        {/*学生完成列表*/}
        <Route
          path="/classrooms/:coursesId/exercises/:Id/Studentshavecompletedthelist"
          render={
            (props) => (<Studentshavecompletedthelist {...this.props} {...props} {...this.state} {...common} />)
          }
        >
        </Route>

        {/*实训查重详情*/}
        <Route path="/classrooms/:coursesId/shixun_homeworks/:homeworkid/review_detail/:userid"
          render={
            (props) => (<ShixunWorkDetails {...this.props} {...props}  {...this.state} {...common} />)
          }
        ></Route>
        <Route path="/classrooms/:coursesId/shixun_homework/:homeworkid/review_detail/:userid"
          render={
            (props) => (<ShixunWorkDetails {...this.props} {...props}  {...this.state} {...common} />)
          }
        ></Route>

        {/*实训查重列表*/}
        <Route path="/classrooms/:coursesId/shixun_homeworks/:homeworkid/student_work"
          render={
            (props) => (<ShixunHomeworkPage {...this.props} {...props}  {...this.state} {...common} />)
          }
        ></Route>
        <Route path="/classrooms/:coursesId/shixun_homework/:homeworkid/student_work"
          render={
            (props) => (<ShixunHomeworkPage {...this.props} {...props}  {...this.state} {...common} />)
          }
        ></Route>

        {/*实训报告*/}
        <Route path="/classrooms/:coursesId/shixun_homeworks/:homeworkid/shixun_work_report"
          render={
            (props) => (<ShixunWorkReport {...this.props} {...props}  {...this.state} {...common} />)
          }
        ></Route>
        <Route path="/classrooms/:coursesId/shixun_homework/:homeworkid/shixun_work_report"
          render={
            (props) => (<ShixunWorkReport {...this.props} {...props}  {...this.state} {...common} />)
          }
        ></Route>

        {/*教师列表*/}
        <Route path="/classrooms/:coursesId/shixun_homeworks/:homeworkid/list"
          render={
            (props) => (<ShixunHomeworkPage {...this.props} {...props}  {...this.state} {...common} />)
          }
        ></Route>
        <Route path="/classrooms/:coursesId/shixun_homework/:homeworkid/list"
          render={
            (props) => (<ShixunHomeworkPage {...this.props} {...props}  {...this.state} {...common} />)
          }
        ></Route>
        {/*实训作业page*/}
        <Route path="/classrooms/:coursesId/shixun_homeworks/:homeworkid/Page"
          render={
            (props) => (<ShixunHomeworkPage {...this.props} {...props}  {...this.state} {...common} />)
          }
        ></Route>
        {/*实训作业设置*/}
        <Route path="/classrooms/:coursesId/shixun_homeworks/:homeworkid/settings"
          render={
            (props) => (<ShixunHomeworkPage {...this.props} {...props}  {...this.state} {...common} />)
          }
        ></Route>
        <Route path="/classrooms/:coursesId/shixun_homework/:homeworkid/settings"
          render={
            (props) => (<ShixunHomeworkPage {...this.props} {...props}  {...this.state} {...common} />)
          }
        ></Route>
        {/*/!*实训作品列表教师*!/*/}
        {/*提交总结*/}
        <Route path="/classrooms/:coursesId/shixun_homeworks/:id/commitsummary/:homeworkid"
          render={
            (props) => (<CommitSummary {...this.props} {...props}  {...this.state} />)
          }
        ></Route>
        <Route path="/classrooms/:coursesId/shixun_homework/:id/commitsummary/:homeworkid"
          render={
            (props) => (<CommitSummary {...this.props} {...props}  {...this.state} />)
          }
        ></Route>

        {/*实训作业问答主目录*/}
        <Route path="/classrooms/:coursesId/shixun_homeworks/:homeworkid/questions"
          render={
            (props) => (<ShixunHomeworkPage {...this.props} {...props}  {...this.state} {...common} />)
          }
        ></Route>

        {/*实训作业问答子目录*/}
        <Route path="/classrooms/:coursesId/shixun_homework/:homeworkid/questions"
          render={
            (props) => (<ShixunHomeworkPage {...this.props} {...props}  {...this.state} {...common} />)
          }
        ></Route>

        {/*新建课堂*/}
        <Route path="/classrooms/new"
          render={
            (props) => (<WrappedCoursesNewApp {...this.props} {...props} {...this.state} {...common} />)
          }
        ></Route>
        {/*新建精品课堂*/}
        <Route path="/classrooms/news/:subjectid/newgold/:id"
          render={
            (props) => (<WrappedCoursesNewAppGoldclass {...this.props} {...props} {...this.state} {...common} />)
          }
        ></Route>
        {/*修改精品课堂*/}
        <Route path="/classrooms/:coursesId/newgolds/settings"
          render={
            (props) => (<WrappedCoursesNewAppGoldclass {...this.props} {...props} {...this.state} {...common} />)
          }
        ></Route>
        {/*修改课堂*/}
        <Route path="/classrooms/:coursesId/settings"
          render={
            (props) => (<WrappedCoursesNewApp {...this.props} {...props} {...this.state} {...common} />)
          }
        ></Route>

        {/* 实训作业子页面*/}
        <Route path="/classrooms/:coursesId/shixun_homework/:category_id"
          render={
            (props) => (<ListPageIndex {...this.props} {...props} {...this.state} {...common} />)
          }
        ></Route>

        {/* 实训作业页面*/}
        <Route path="/classrooms/:coursesId/shixun_homeworks/:main_id"
          render={
            (props) => (<ListPageIndex {...this.props} {...props} {...this.state} {...common} />)
          }
        ></Route>

        {/*课堂统计列表2019.10.29 */}
        <Route path="/classrooms/:coursesId/statistics"
          render={
            (props) => (<ListPageIndex {...this.props} {...props} {...this.state} />)
          }
        ></Route>

        {/* 主签到 */}
        <Route path="/classrooms/:coursesId/attendances"
          render={
            (props) => (<ListPageIndex {...this.props} {...props} {...this.state} />)
          }
        ></Route>

        {/*/!*实训作业and课堂详情页*!/*/}
        <Route path="/classrooms/:coursesId"
          render={
            (props) => (<ListPageIndex {...this.props} {...props} {...this.state} {...common} />)
          }
        ></Route>

        {/*课堂首页*/}
        <Route path="/classrooms"
          render={
            (props) => (<CoursesHome {...this.props} {...props} {...this.state} {...common} />)
          }
        ></Route>

      </Switch>
    )
  }
}

export default withRouter(ImageLayerOfCommentHOC({ imgSelector: '.imageLayerParent img, .imageLayerParent .imageTarget', parentSelector: '.newMain' })(CNotificationHOC()(SnackbarHOC()(TPMIndexHOC(CoursesIndex)))));
