import React, { Component } from 'react';
import { Route , Switch } from 'react-router-dom';

import { TPMIndexHOC } from '../modules/tpm/TPMIndexHOC';
import { SnackbarHOC , CNotificationHOC } from 'educoder';

import ExchangeIndex from './ExchangeIndex';

import Post from './Post/Index'
import PostNew from './Post/MemoNew';
import PlateIndex from './Plate/Index';
import MyExchange from './MyExchange/Index'
import PreManager from './Manage/PreModerator'
import axios from 'axios';
import './exchange.css';

class Index extends Component{
  constructor(props){
    super(props);
    this.state={
      current_user:undefined
    }
  }

  componentDidMount=()=>{
    const url = `/users/get_user_info.json`;
    axios.get(url).then(result=>{
      if(result){
        this.setState({
          current_user:result.data.current_user
        })
        // 给NewHeader传值，当前用户的信息
        if(result.data.current_user && result.data.current_user.login){
          this.props.initCommonState(result.data.current_user);
        }
      }
    }).catch(error=>{

    })
  }

  componentDidUpdate=()=>{
    this.props.history.listen(()=>{
      if (document.body.scrollTop || document.documentElement.scrollTop > 0) {
        window.scrollTo(0, 0)
      }
    })
  }
  render(){
    return(
      <Switch {...this.props}>
        {/* 帖子列表 */}
        <Route path='/forums/plates/:plateid/all' render={
          (props) => (<PlateIndex {...this.props} {...this.state} {...props} />)
        } ></Route>
        {/* 编辑帖子 */}
        <Route path={`/forums/:memoId/edit`} render={
          (props) => {
            return (<PostNew {...this.props} {...this.state} {...props}></PostNew>)}
        }></Route>
        {/* 新建帖子 */} 
        <Route path={`/forums/new`} render={
          (props) => {
            return (<PostNew {...this.props} {...this.state} {...props}></PostNew>)}
        }></Route>
        {/* 版主管理 */}
       <Route path={`/forums/manage/:plateId`} render={
         (props) => {
           return (<PreManager {...this.props} {...this.state} {...props}></PreManager>)}
       }></Route>

        {/* 我的话题 */}
        <Route path={`/forums/MyTopic`} render={
          (props) => {
            return (<MyExchange {...this.props} {...this.state} {...props}></MyExchange>)}
        }></Route>
        <Route path={`/forums/MyEnshrine`} render={
          (props) => {
            return (<MyExchange {...this.props} {...this.state} {...props}></MyExchange>)}
        }></Route>
        <Route path={`/forums/MyInteresting`} render={
          (props) => {
            return (<MyExchange {...this.props} {...this.state} {...props}></MyExchange>)}
        }></Route>
        {/* 帖子详情 */}
        <Route path={`/forums/:postid`} render={
          (props) => {
            return (<Post {...this.props} {...this.state} {...props}></Post>)}
        }></Route>
        {/* 论坛首页 */}
        <Route path={`/forums`} render={
          (props) => (<ExchangeIndex {...this.props} {...this.state} {...props} />)
        }></Route>
      </Switch>
    )
  }
}
export default SnackbarHOC() (CNotificationHOC() (TPMIndexHOC ( Index ))) ;