import React from 'react';
import './App.css';
import 'bootstrap/dist/css/bootstrap.min.css';
import {Login} from './account/Login';
import {Cookies, withCookies} from 'react-cookie';
import {Main} from "./Main";
import {Register} from "./account/Register";
import {manager_cookie} from "./account/PropCookie";
import {LoginRes} from "./result";


class App extends React.Component<
    {
      allCookies:any;
      cookies:Cookies
    },
    {
      page?:JSX.Element;
    }
  > {


  constructor(props: Readonly<any>) {
    super(props);

    this.state={}
  }

  componentDidMount() {
    const { cookies } = this.props
    if((cookies.get(manager_cookie)||"").length>0){
      this.setState({
        page:<Main cookies={this.props.cookies} logout={()=>this.logout()}/>
      })
    }else{
      this.toLogin()
    }
  }


  //注销登录
  logout(){
    this.props.cookies.remove(manager_cookie)
    this.toLogin()
  }

  /**
   * 跳转注册
   */
  toRegister(){
    this.setState({
      page:<Register toLogin={()=>this.toLogin()}/>
    })
  }

  /**
   * 跳转登录
   */
  toLogin(){
    this.setState({
      page:<Login cookies={this.props.cookies} toRegister={()=>this.toRegister()} onLoginSuccess={(res:LoginRes)=>{
          this.props.cookies.set(manager_cookie,res.managerId)

          this.setState({
            page:<Main  logout={()=>this.logout()} cookies={this.props.cookies}/>
          })
        }
      }/>
    })
  }


  render() {
    return (
        <div className="App">
          {this.state.page}
        </div>
    );
  }
}

export default withCookies(App);
