// import Logo from '@/assets/bitcoin.webp';
import { MyIcons, MyIconsType, PermissionsType, useMyState } from '@/common';
import { Link, RuntimeConfig, history } from '@umijs/max';
import AvatarProps from '../../components/layout/AvatarProps';
import { CustomHeader, CustomLogo } from '@/common/components/layout';

const loopMenu = (permissions: PermissionsType[] | undefined) => {
  let tree: PermissionsType[] = [];
  let map: Record<number, PermissionsType> = {};
  permissions?.forEach((permission) => {
    map[permission.id] = {
      path: permission.type === 'Button' ? 'null' : permission.path,
      name: permission.name,
      icon: permission.icon && MyIcons[permission.icon as MyIconsType],
      children: [],
      hideInMenu: permission.type === 'Button',
    };
  });

  permissions?.forEach((permission) => {
    let node = map[permission.id];
    if (permission.parent_id !== null) {
      map[permission.parent_id].children.push(node);
    } else {
      tree.push(node);
    }
  });

  return tree?.[0]?.children;
};

export const LayoutConfig: RuntimeConfig['layout'] = () => {
  const { snap } = useMyState();

  return {
    title: snap.session.campus?.name ?? '总后台',
    logo: false, // 禁用默认logo，使用自定义
    layout: 'side', // 从 'mix' 改为 'side'
    colorPrimary: '#1890ff',
    siderWidth: 220,
    pure: history.location.pathname === '/login',
    // 禁用顶部导航栏的菜单
    navTheme: 'light',
    // 确保header显示
    header: true,

    // 自定义Header渲染 - 尝试不同的方式
    // headerRender: () => <CustomHeader />,

    // 使用headerContentRender来渲染header内容
    headerContentRender: () => <CustomHeader />,

    // 新增：自定义Logo渲染
    logoRenderer: (collapsed: boolean) => {
      return <CustomLogo collapsed={collapsed} />;
    },

    // 完全禁用用户信息在侧边栏底部显示
    // avatarProps: false, // 这样可以完全禁用
    //水印设置
    // waterMarkProps: {
    //   content: snap.session.user?.username,
    // },
    collapsedButtonRender: false,
    // 禁用底部的版权信息等
    footerRender: false,
    token: {
      bgLayout: '#f5f5f5', // 浅灰布局背景
      header: {
        colorBgHeader: '#ffffff', // 白色header背景
        colorHeaderTitle: '#262626', // 深色标题文字
        colorTextRightActionsItem: '#595959', // 深色操作文字
        heightLayoutHeader: 64, // Header高度64px
      },
      sider: {
        colorMenuBackground: '#ffffff', // 白色菜单背景
        colorMenuText: '#595959', // 深色菜单文字
        colorMenuTextSelected: '#1890ff', // 激活菜单文字蓝色
        colorMenuItemBgSelected: '#e6f7ff', // 激活菜单背景浅蓝
        colorMenuDivider: '#f0f0f0', // 分割线颜色
      },
    },
    menuItemRender: (item, dom) => <Link to={item.path || '/'}>{dom}</Link>,
    menu: {
      params: snap.session.permissions,
      request: async () => {
        let objjs: any = [];
        snap.session.permissions?.forEach((res: any) => {
          objjs.push(res);
        });
        let data = objjs.sort((a: any, b: any) => {
          return a._lft - b._lft;
        });
        const menus = loopMenu(data);
        return Promise.resolve(menus);
      },
    },
    unAccessible: <div>unAccessible</div>,
  };
};
