import React, { useEffect } from 'react';

import styles from './index.css';
import { router, connect } from 'dva';
import { getToken, setToken } from '@/H5Public/utils/authority';
import { setLoginToUrl } from '../utils/authority';
import { Base16Encode } from '../utils/Base16';

const routerWhiteList = [
  '/login',
  '/webActivtiDetali',
  // 来访登记者填写表单
  '/visitorRegister/visitorFormPage',
];
const { Redirect } = router;

let BasicLayout = ({ children, dispatch, location: { pathname } }) => {
  if (pathname === '/login') {
    return <div>{children}</div>;
  }
  return <div className={styles.normal}>{children}</div>;
};
BasicLayout = connect()(BasicLayout);

export default (props) => {
  const { location: { pathname, query: { token, returnUrl } }, children } = props;

  if (routerWhiteList.includes(pathname)) return children;
  if (!!token || getToken()) {
    if (!!token) {
      setToken(token);
    }
    if (!!returnUrl) {
      return <Redirect to={returnUrl} />;
    }
    return <BasicLayout {...props} />;
  }
  let loginPathname = '/login';
  if (!!returnUrl) {
    loginPathname = `${loginPathname}?returnUrl=${returnUrl}`;
  }
  if (window.location.href.indexOf('/CollegeYongthLeague/Student') > -1) { // 团委签到, 团委签退
    setLoginToUrl(window.location.href);
  }

  return <Redirect to={loginPathname} />;
}

