/**
 * Ant Design Pro v4 use `@ant-design/pro-layout` to handle Layout.
 * You can view component api by:
 * https://github.com/ant-design/ant-design-pro-layout
 */
import ProLayout from '@ant-design/pro-layout';
import React, { useEffect } from 'react';
import Link from 'umi/link';
import Redirect from 'umi/redirect';
import { connect } from 'dva';
import { formatMessage } from 'umi-plugin-react/locale';
// import Authorized from '@/utils/Authorized';
import RightContent from '@/components/GlobalHeader/RightContent';
import RenderAuthorize from '@/components/Authorized';
import { isAntDesignPro } from '@/utils/utils';
import logo from '../assets/logo.png';
  
const footerRender = () => {
  return (
    <div
      style={{
        padding: ' 44px 0',
        textAlign: 'center',
        fontSize: '30px',
        fontFamily: 'monospace',
        fontWeight: '200',
        color: 'rgba(102, 102, 102, 1)',
      }}
    >
      copy Right @ 知与行
    </div>
  )
}

const BasicLayout = props => {
  const { dispatch, children, settings } = props;
  
  useEffect(() => {
    if (dispatch && !props.user.currentUser.userId) {
      dispatch({
        type: 'user/fetchCurrent',
      });
      dispatch({
        type: 'settings/getSetting',
      });
    }
  }, []);
  
  const Authorized = RenderAuthorize(props.user.currentUser.roles)

  const handleMenuCollapse = payload => {
    if (dispatch) {
      dispatch({
        type: 'global/changeLayoutCollapsed',
        payload,
      });
    }
  };

  const findAuthority = (path) => {
    return ((props.user.menuList || []).filter(x => x.code === path)[0] || {}).roles
  }
  
  /**
   * use Authorized check all menu item
   */
  const menuDataRender = menuList =>
    menuList.map(item => {
      const localItem = { ...item, children: item.children ? menuDataRender(item.children) : [] };
      const authority = findAuthority(item.path);
      // return Authorized.check(item.authority, localItem, null);
      return Authorized.check(authority, localItem, null);
    });

  const checkRights = (children) => {
    const authority = findAuthority(props.location.pathname);
    return Authorized.check(authority, children, <Redirect to="/exception/403" />);
  }

  return (
    <ProLayout
      logo={logo}
      onCollapse={handleMenuCollapse}
      menuItemRender={(menuItemProps, defaultDom) => {
        if (menuItemProps.isUrl) {
          return defaultDom;
        }

        return <Link to={menuItemProps.path}>{defaultDom}</Link>;
      }}
      breadcrumbRender={(routers = []) => [
        {
          path: '/',
          breadcrumbName: formatMessage({
            id: 'menu.home',
            defaultMessage: 'Home',
          }),
        },
        ...routers,
      ]}
      itemRender={(route, params, routes, paths) => {
        const first = routes.indexOf(route) === 0;
        return first ? (
          <Link to={paths.join('/')}>{route.breadcrumbName}</Link>
        ) : (
            <span>{route.breadcrumbName}</span>
          );
      }}
      footerRender={footerRender}
      menuDataRender={menuDataRender}
      // formatMessage={formatMessage}
      rightContentRender={rightProps => <RightContent {...rightProps} />}
      {...props}
      {...settings}
    // pageTitleRender={()=><></>}
    >
      {checkRights(children)}
    </ProLayout>
  );
};

export default connect(({ global, settings, user }) => ({
  collapsed: global.collapsed,
  settings,
  user
}))(BasicLayout);
