import { Navigate, Outlet } from "react-router-dom";
import {
  AuditOutlined,
  CalendarOutlined,
  BankOutlined,
  UserOutlined,
  BlockOutlined,
  TeamOutlined,
  SettingOutlined,
  ScheduleOutlined,
  SwapOutlined,
  ApartmentOutlined,
} from "@ant-design/icons";
import AuthLayout from "@/layouts/AuthLayout";
import Page404 from "@/pages/404";
import Home from "@/pages/home";
import History from "@/pages/home/History";
import Detail from "@/pages/home/Detail";
import UserList from "@/pages/system/user/index";
import UserEdit from "@/pages/system/user/Edit";
import Role from "@/pages/system/role";
import Org from "@/pages/system/org";
import Position from "@/pages/system/position";
import PositionEdit from "@/pages/system/position/edit/";
import Incompatible from "@/pages/system/Incompatible";
import IncompatibleEdit from "@/pages/system/Incompatible/edit";

import CompatibilityQuery from "@/pages/rotationManage/compatibilityQuery/index";
import RotationList from "@/pages/rotationManage/rotationList/index";
import RotationListEdit from "@/pages/rotationManage/rotationList/Edit";
import MandatoryleaveList from "@/pages/mandatoryleave/index";
import MandatoryleaveEdit from "@/pages/mandatoryleave/Edit";
import Work from "@/pages/work";

import { flatten } from "./utils";

/**
 * meta 用来扩展自定义数据数据
 * {
 *    title: 用于页面或者菜单的标题, 没有此字段, 菜单不会显示
 *    hideInMenu: 布尔值, 如果为 false, 菜单不会显示
 *    noLayout: 布尔值, 如果为 true, 将不会使用默认布局
 *    noSiderBar: 布尔值, 如果为 true, 将没有左侧菜单栏
 *    noFooter: 布尔值, 如果为 true, 将没有底部 footer
 *    target: 字符串, 如果为 _blank, 将在新窗口打开
 *    permission: 对应服务器端权限名称
 * }
 */

export const routes = [
  {
    index: true,
    element: <Navigate to="/home" replace={true} />,
  },

  {
    path: `home`,
    element: <Home />,
    meta: {
      title: "首页",
      // permission: "org",
      icon: <BankOutlined />,
    },
  },
  {
    path: `home/history`,
    element: <History />,
    meta: {
      title: "历史列表",
      hideInMenu: true,
      // permission: "org"
    },
  },
  {
    path: `home/detail`,
    element: <Detail />,
    meta: {
      title: "消息详情",
      hideInMenu: true,
      // permission: "org"
    },
  },
  {
    path: `rotationMange`,
    element: <Outlet />,
    meta: {
      title: "轮岗管理",
      icon: <SwapOutlined />,
      permission: "rotation.mange",
    },
    children: [
      {
        path: `compatibilityQuery`,
        element: <CompatibilityQuery />,
        meta: {
          title: "相容性查询",
          // icon: <BankOutlined />,
          permission: "compatibility",
        },
      },
      {
        path: `rotationList`,
        element: <RotationList />,
        meta: {
          title: "轮岗列表",
          // icon: <BankOutlined />,
          permission: "rotation.list",
        },
      },
      {
        path: `rotationList/edit`,
        element: <RotationListEdit />,
        meta: {
          title: "轮岗维护",
          hideInMenu: true,
          permission: "rotation.edit",
        },
      },
    ],
  },
  {
    path: `mandatoryLeaveMange`,
    element: <Outlet />,
    meta: {
      title: "强制休假管理",
      icon: <CalendarOutlined />,
      permission: "mandatory.leave.mange",
    },
    children: [
      {
        path: `mandatoryLeaveList`,
        element: <MandatoryleaveList />,
        meta: {
          title: "强制休假列表",
          // icon: <BankOutlined />,
          permission: "mandatory.leave.list",
        },
      },
      {
        path: `mandatoryleave/edit`,
        element: <MandatoryleaveEdit />,
        meta: {
          title: "强制休假维护",
          hideInMenu: true,
          permission: "mandatory.leave.edit",
        },
      },
    ],
  },

  {
    path: `system`,
    element: <Outlet />,
    meta: {
      title: "系统管理",
      icon: <SettingOutlined />,
      menuType: "group",
      permission: "system",
    },
    children: [
      {
        path: `org`,
        element: <Org />,
        meta: {
          title: "机构管理",
          permission: "org",
          icon: <AuditOutlined />,
        },
      },
      {
        path: `user`,
        element: <UserList />,
        meta: {
          title: "人员管理",
          permission: "user",
          icon: <UserOutlined />,
        },
      },
      {
        path: `user/edit`,
        element: <UserEdit />,
        meta: {
          title: "人员维护",
          permission: "user.edit",
          hideInMenu: true,
        },
      },
      {
        path: `role`,
        element: <Role />,
        meta: {
          title: "角色管理",
          permission: "role",
          icon: <TeamOutlined />,
        },
      },
      {
        path: `position`,
        element: <Position />,
        meta: {
          title: "岗位管理",
          permission: "position",
          icon: <BlockOutlined />,
        },
      },
      {
        path: `position/edit`,
        element: <PositionEdit />,
        meta: {
          title: "岗位维护",
          permission: "position.edit",
          hideInMenu: true,
        },
      },
      {
        path: `incompatible`,
        element: <Incompatible />,
        meta: {
          title: "不相容设置",
          icon: <ApartmentOutlined />,
          permission: "incompatible",
        },
      },
      {
        path: `incompatible/edit`,
        element: <IncompatibleEdit />,
        meta: {
          title: "不相容维护",
          // permission: "incompatible",
          hideInMenu: true,
        },
      }
    ],
  },
];

export function getAllRoutes (routes) {
  return [
    {
      path: "/",
      element: <AuthLayout />,
      children: [
        {
          path: "*",
          element: <Page404 />,
        },
        ...(routes || []),
      ],
    },
    {
      path: "*",
      element: <Page404 />,
    },
  ];
}

// 展平路由
export const flattenRoutes = flatten(getAllRoutes(routes));
