import {
  AppstoreOutlined,
  ContainerOutlined,
  DesktopOutlined,
  MailOutlined,
  MenuFoldOutlined,
  MenuUnfoldOutlined,
  PieChartOutlined,
} from "@ant-design/icons";
import { Navigate } from "react-router-dom";
import AuthLayout from "@/layouts/AuthLayout";
import Container from "@/layouts/Container";
import Login from "@/pages/login";
import Page404 from "@/pages/404";
import Home from "@/pages/sample/home";
import BasicForm from "@/pages/sample/form";
import BasicTable from "@/pages/sample/table";
import GuaranteeTaskList from "@/pages/guaranteeTask";
import GuaranteeTaskEdit from "@/pages/guaranteeTask/Edit";
import GuaranteeTaskPrint from "@/pages/guaranteeTask/print";
import GuaranteeTaskEvaluate from "@/pages/evaluate";
import GuaranteeTaskEvaluateList from "@/pages/evaluate/evaluateList";
import DishList from "@/pages/dish/list";
import DishEdit from "@/pages/dish/edit";
import PackageList from "@/pages/package";
import StockList from "@/pages/stock/list";
import StockEdit from "@/pages/stock/edit";
import StockInOut from "@/pages/stock/outAndIn";
import StockLog from "@/pages/stock/stockLog";
import StockClassificationList from "@/pages/stockClassification/list";
import StockClassificationEdit from "@/pages/stockClassification/edit";
import RotationChartList from "@/pages/rotationChart/list";
import RotationChartEdit from "@/pages/rotationChart/edit";
import RotationChartIntroduction from "@/pages/rotationChart/introduction";
import RotationChartIntroductionEdit from "@/pages/rotationChart/introduction/edit";
import RotationChartDetail from "@/pages/rotationChart/detail";
import Roles from "@/pages/roles/index";
import RegulationList from "@/regulation";
import RegulationEdit from "@/regulation/edit";
import UserList from "@/pages/user";
import UserEdit from "@/pages/user/Edit";
import PurchasePlanList from "@/pages/purchase/plan/list";
import PurchasePlanEdit from "@/pages/purchase/plan/edit";
import PurchaseBillEdit from "@/pages/purchase/bill/edit";
import PurchaseInStoreEdit from "@/pages/purchase/inStore/edit";
import EmergencyPlanList from "@/pages/cms/emergencyPlan/list";
import EmergencyPlanEdit from "@/pages/cms/emergencyPlan/edit";
import EmergencyPlanDetail from "@/pages/cms/emergencyPlan/detail";
import FilesList from "@/pages/cms/files/list";
import MessageList from "@/pages/message";
import MessageDetail from "@/pages/message/detail";
import StatisCharts from "@/pages/statis/charts";

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

export const authRoutes = [
  {
    path: "task",
    element: <Container />,
    meta: {
      title: "军供任务",
    },
    children: [
      {
        index: true,
        element: <Navigate to="guaranteeTask" replace />,
      },
      {
        path: "guaranteeTask",
        element: <GuaranteeTaskList />,
        meta: {
          title: "军供通报",
        },
      },
      {
        path: "guaranteeTask/edit",
        element: <GuaranteeTaskEdit />,
        meta: {
          title: "任务配置",
        },
      },
      {
        path: "guaranteeTask/print",
        element: <GuaranteeTaskPrint />,
        meta: {
          hideInMenu: true,
          noLayout: true,
          target: "_blank",
          title: "任务执行",
        },
      },
      {
        path: "evaluate",
        element: <GuaranteeTaskEvaluate />,
        meta: {
          title: "任务评价",
        },
      },
      {
        path: "evaluate/list",
        element: <GuaranteeTaskEvaluateList />,
        meta: {
          title: "任务评价",
          hideInMenu: true,
        },
      },
    ],
  },
  {
    path: "stock",
    element: <Container />,
    meta: {
      title: "库存物资",
    },
    children: [
      {
        index: true,
        element: <Navigate to="dish/list" replace />,
      },
      {
        path: "dish/list",
        element: <DishList />,
        meta: {
          title: "菜肴管理",
        },
      },
      {
        path: "dish/edit",
        element: <DishEdit />,
        meta: {
          hideInMenu: true,
          title: "菜肴维护",
        },
      },
      {
        path: "package/list",
        element: <PackageList />,
        meta: {
          title: "套餐管理",
        },
      },
      {
        path: "list",
        element: <StockList />,
        meta: {
          title: "库存列表",
        },
      },
      {
        path: "add",
        element: <StockEdit />,
        meta: {
          title: "库存维护",
        },
      },
    ],
  },
  {
    path: "cms",
    element: <Container />,
    meta: {
      title: "公告文件",
    },
    children: [
      {
        index: true,
        element: <Navigate to="rotationChart/list" replace />,
      },
      {
        path: "station",
        element: null,
        meta: {
          title: "本站信息",
        },
      },
      {
        path: "rotationChart/list",
        element: <RotationChartList />,
        meta: {
          title: "公告管理",
        },
      },
      {
        path: "rotationChart/add",
        element: <RotationChartEdit />,
        meta: {
          title: "公告维护",
          hideInMenu: true,
        },
      },
      {
        path: "rotationChart/detail",
        element: <RotationChartDetail />,
        meta: {
          title: "公告详情",
          hideInMenu: true,
        },
      },
      {
        path: "rotationChart/introduction",
        element: <RotationChartIntroduction />,
        meta: {
          title: "本站信息简介",
        },
      },
      {
        path: "rotationChart/introduction/edit",
        element: <RotationChartIntroductionEdit />,
        meta: {
          hideInMenu: true,
          title: "本站信息简介维护",
        },
      },
      {
        path: "regulation",
        element: <RegulationList />,
        meta: {
          title: "规章制度",
        },
      },
      {
        path: "regulation/add",
        element: <RegulationEdit />,
        meta: {
          hideInMenu: true,
          title: "规章制度维护",
        },
      },
      {
        path: "emergency-plan",
        element: <EmergencyPlanList />,
        meta: {
          title: "应急预案",
        },
      },
      {
        path: "emergency-plan/edit",
        element: <EmergencyPlanEdit />,
        meta: {
          title: "应急预案维护",
          hideInMenu: true,
        },
      },
      {
        path: "emergency-plan/detail",
        element: <EmergencyPlanDetail />,
        meta: {
          title: "应急预案详情",
          hideInMenu: true,
        },
      },

      {
        path: "files",
        element: <FilesList />,
        meta: {
          title: "文件管理",
        },
      },
    ],
  },
  {
    path: "static",
    element: <StatisCharts />,
    meta: {
      title: "数据分析",
      noSiderBar: true,
      noFooter: true,
    },
  },
  {
    path: "purchase",
    element: <Container />,
    meta: {
      title: "采购管理",
    },
    children: [
      {
        index: true,
        element: <Navigate to="plan/list" replace />,
      },
      {
        path: "plan/list",
        element: <PurchasePlanList type="plan" />,
        meta: {
          title: "采购计划",
        },
      },
      {
        path: "plan/edit",
        element: <PurchasePlanEdit />,
        meta: {
          title: "采购计划维护",
          hideInMenu: true,
        },
      },
      {
        path: "bill/list",
        element: <PurchasePlanList type="bill" />,
        meta: {
          title: "采购账单",
        },
      },
      {
        path: "bill/edit",
        element: <PurchaseBillEdit />,
        meta: {
          title: "采购账单维护",
          hideInMenu: true,
        },
      },
      {
        path: "inStore/list",
        element: <PurchasePlanList type="inStore" />,
        meta: {
          title: "采购入库",
        },
      },
      {
        path: "inStore/edit",
        element: <PurchaseInStoreEdit />,
        meta: {
          title: "采购入库维护",
          hideInMenu: true,
        },
      },
    ],
  },
  {
    path: "system",
    element: <Container />,
    meta: {
      title: "系统管理",
    },
    children: [
      {
        index: true,
        element: <Navigate to="stockClassification/list" replace />,
      },
      {
        path: DH_MONITOR,
        element: null,
        meta: {
          title: "大华监控",
        },
      },
      {
        path: DH_MONITOR,
        element: null,
        meta: {
          title: "海康监控",
        },
      },
      {
        path: "stockClassification/list",
        element: <StockClassificationList />,
        meta: {
          title: "库存分类",
        },
      },
      {
        path: "stockClassification/edit",
        element: <StockClassificationEdit />,
        meta: {
          title: "库存分类维护",
          hideInMenu: true,
        },
      },
      {
        path: "log",
        element: <StockLog />,
        meta: {
          title: "库存日志",
        },
      },
      {
        path: "roles",
        element: <Roles />,
        meta: {
          title: "角色管理",
        },
      },
      {
        path: "user",
        element: <UserList />,
        meta: {
          title: "用户管理",
        },
      },
      {
        path: "user/edit",
        element: <UserEdit />,
        meta: {
          hideInMenu: true,
          title: "系统用户编辑",
        },
      },
      {
        path: "message",
        element: <MessageList />,
        meta: {
          title: "消息列表",
        },
      },
      {
        path: "message/detail",
        element: <MessageDetail />,
        meta: {
          title: "消息详情",
          hideInMenu: true,
        },
      },
      {
        path: "foo",
        element: null,
        meta: {
          title: "操作手册",
        },
      },
    ],
  },
];

export const defaultRoutes = [
  {
    path: "/",
    element: <AuthLayout />,
    children: [
      {
        index: true,
        element: <Navigate to="home" replace />,
      },
      {
        path: "home",
        element: <Home />,
        meta: {
          title: "首页",
          icon: <DesktopOutlined />,
          noSiderBar: true,
          noFooter: true,
        },
      },
      {
        path: "*",
        element: <Page404 />,
      },
    ],
  },
  {
    path: "/login",
    element: <Login />,
  },
  {
    path: "*",
    element: <Page404 />,
  },
];

export function mergeAuthRoutes(r1, r2) {
  const r = r1.slice();
  const children = r1[0].children.slice();
  r[0].children = children.concat(r2);
  return r;
}

// 全部路由
export const routes = mergeAuthRoutes(defaultRoutes, authRoutes);
function getPath(parent = "/", current = "") {
  if (current.indexOf("/") === 0 || current.indexOf("http") === 0)
    return current;
  return `${parent}/${current}`.replace(/\/\//g, "/");
}

// 路由数组, 一维数组
export const routeArr = (() => {
  const flatten = (routes, parentPath = "/") => {
    return routes.reduce((acc, route) => {
      const path = getPath(parentPath, route.path);
      const children = route.children ? flatten(route.children, path) : [];

      return acc.concat([{ ...route, path }].concat(children));
    }, []);
  };

  return flatten(routes);
})();
