import React from 'react';
import { connect } from 'dva';
import { Redirect } from 'umi';
import PageLoading from '@/components/PageLoading';
import Swiper from '../pages/swiper/index';

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

  componentDidMount() {
    this.setState({
      isReady: true,
    });
    const { dispatch } = this.props;

    if (dispatch) {
      dispatch({
        type: 'user/fetchCurrent',
      });
    }
  }

  render() {
    const { isReady } = this.state;
    const { children, loading, currentUser } = this.props;

    if ((!currentUser.userId && loading) || !isReady) {
      return <PageLoading />;
    }

    if (!currentUser.userId) {
      return <Redirect to="/user/login"></Redirect>;
    }
    console.log(window.location, "window.locationwindow.locationwindow.location")
    return <>
      {children}
      {window.location.hash == "#/index" && <Swiper />}
    </>;
  }
}

export default connect(({ user, loading }) => ({
  currentUser: user.currentUser,
  loading: loading.models.user,
}))(SecurityLayout);
