import React, { useEffect } from 'react';
// import { ConfigProvider } from 'antd';
import { LocaleProvider } from 'antd-mobile';
// import zhCN from 'antd/es/locale/zh_CN';
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 <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}`;
  }
  if (window.location.href.indexOf('/CollegeYongthLeague/Student') > -1) { // 团委签到, 团委签退
    setLoginToUrl(window.location.href);
  }

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

