import React, { Component, useEffect, useState } from "react";

import { Route, Switch } from "react-router-dom";
import { withRouter } from "react-router";
import { SnackbarHOC } from "educoder";
import { CNotificationHOC } from "../modules/courses/common/CNotificationHOC";
import { TPMIndexHOC } from "../modules/tpm/TPMIndexHOC";
import Loadable from "react-loadable";
import Loading from "../Loading";
import { getUserInfo } from './task/api';
import { ImageLayerOfCommentHOC } from "../modules/page/layers/ImageLayerOfCommentHOC";
import './index.scss';

const TaskList = Loadable({
  loader: () => import("./task/taskList"),
  loading: Loading,
});

const TaskDetail = Loadable({
  loader: () => import("./task/taskDetail"),
  loading: Loading,
});

const TaskEdit = Loadable({
  loader: () => import("./task/taskEdit"),
  loading: Loading,
});

const MyTask = Loadable({
  loader: () => import("./task/myTask"),
  loading: Loading,
});


const TaskAdminRouter = Loadable({
  loader: () => import("./task/taskAdminRouter"),
  loading: Loading,
});



const Index = (propsTransmit) => {
  // 开发时，从代理的位置获取用户信息
  const [currentUser, setCurrentUser] = useState(propsTransmit.current_user);
  // const isDev = window.location.href.indexOf('3007') > -1 ? true : false;
  useEffect(() => {
    getUserInfo().then(res => {
      if (res && res.data) {
        setCurrentUser(res.data);
      }
    })
  }, [])
  let propsF = { ...propsTransmit };
  propsF.current_user = currentUser;

  return (
    <div className="newMain clearfix">
      <Switch {...propsF}>

        {/* 任务详情 */}
        <Route
          path="/task/taskDetail/:taskId"
          render={(props) => (
            <TaskDetail {...propsF} {...props} />
          )}
        ></Route>

        {/* 新增任务 */}
        <Route
          path="/task/taskAdd"
          render={(props) => (
            <TaskEdit {...propsF} {...props} />
          )}
        ></Route>

        {/* 编辑任务 */}
        <Route
          path="/task/taskEdit/:taskId"
          render={(props) => (
            <TaskEdit {...propsF} {...props} />
          )}
        ></Route>

        {/* 我的任务 */}
        <Route
          path="/task/myTask"
          render={(props) => (
            <MyTask {...propsF} {...props} />
          )}
        ></Route>

        {/* 管理员管理 */}
        <Route
          path="/task/:admin"
          render={(props) => (
            <TaskAdminRouter {...propsF} {...props} />
          )}
        ></Route>

        {/* 任务列表 */}
        <Route
          path="/task"
          render={(props) => (
            <TaskList {...propsF} {...props} />
          )}
        ></Route>

      </Switch>
    </div>
  );
}
// }
export default withRouter(
  ImageLayerOfCommentHOC({
    imgSelector: ".imageLayerParent img, .imageLayerParent .imageTarget",
    parentSelector: ".newMain",
  })(CNotificationHOC()(SnackbarHOC()(TPMIndexHOC(Index))))
);
