import React, { useEffect, useState, useRef } from 'react';
import { LocaleProvider } from 'antd-mobile';
import styles from './index.css';
import { router, connect } from 'dva';
import { getToken, setToken } from '@/H5Public/utils/authority';

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

let BasicLayout = ({ children, dispatch, location: { pathname } }) => {
  if (pathname === '/login') {
    return <div>{children}</div>;
  }
  return <LocaleProvider locale={{
      value: '中国',
      label: '中国',
      language: undefined,
    }}>
    <div className={styles.normal}>
      {children}
    </div>
    </LocaleProvider>;
};
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}`;
  }
  return <Redirect to={loginPathname} />;
}

