import React from 'react';

import { connect } from 'react-redux';
import { Redirect } from 'react-router-dom';

import { getBaseName } from '../utils/utils';

class SecurityLayout extends React.Component {
  state = {
    isReady: false,
  };

  componentDidMount() {
    this.setState({
      isReady: true,
    }); 
  }

  render() {
    const { isReady } = this.state
    const { children, global, loading } = this.props;
    const isLogin = global.token !== null;

    // if(Cookie.get("token")==="") {
    //   return (
    //     <Redirect to={`/user/login?client=${global.client}`} />
    //   );
    // }
    // if((!isLogin && loading) || !isReady) {
    //   return <PageLoading/>
    // }
    if (
      !isLogin &&
      window.location.pathname !== `/${getBaseName()}/user/login`
    ) {
      return (
        <Redirect to={`/user/login?client=${global.client}`} />
      );
    }
    return children;
  }
}

const mapStateToProps = state => {
  return {
    global: state.getIn(['global', 'globalConfig']),
  };
};
export default connect(
  mapStateToProps,
  null,
)(SecurityLayout);
