/** @format */
import * as React from 'react'
import {Redirect} from 'react-router-dom'
import {Spin, Icon} from 'antd'
import Loadable, {LoadingComponentProps} from 'react-loadable'
import { createFromIconfontCN } from "@ant-design/icons";
const IconFont = createFromIconfontCN({
    scriptUrl: ["//at.alicdn.com/t/c/font_3884408_w5ddmhselno.js"]
  });

const Loading = (props: LoadingComponentProps) => {
    if (props.error) {
        return (
            <div>
                Error! <button onClick={props.retry}>Retry</button>
            </div>
        )
    }
    return <Spin size='large'/>
}
// dashbord
const dashboard_merchant = Loadable({
    loader: () => import(/* webpackChunkName:"dashbord-merchant" */ '../pages/dashbord/merchant'),
    loading: Loading,
})
dashboard_merchant.preload()
const dashboard_provide = Loadable({
    loader: () => import('../pages/dashbord/provide'),
    loading: Loading,
})
//merchant
const merchant_list = Loadable({
    loader: () => import('../pages/merchant/list'),
    loading: Loading,
})

const merchant_review_list = Loadable({
    loader: () => import('../pages/merchant/merchant-review-list'),
    loading: Loading,
})

// 商户订单
const merchant_order_list = Loadable({
    loader:()=>import('../pages/merchant/merchant-order-list'),
    loading:Loading,
})

//商户列表--权限管理
const PermissionManger = Loadable({
    loader: () => import('../pages/merchant/permission'),
    loading: Loading,
})
//商户列表--订单管理
const orderManent = Loadable({
    loader: () => import('../pages/merchant/order-manage'),
    loading: Loading,
})

// 财会--消费记录
const expenses_list = Loadable({
    loader: () => import('../pages/accounting/expenses'),
    loading: Loading,
})
// 财会--充值记录
const recharge_list = Loadable({
    loader: () => import('../pages/accounting/recharge'),
    loading: Loading,
})

// 财会--商户列表
const account_merchant_list = Loadable({
    loader: () => import('../pages/accounting/merchant'),
    loading: Loading,
})

// 财会--账单记录
const bill_list = Loadable({
    loader: () => import('../pages/accounting/bill'),
    loading: Loading,
})
// 财会--账单详情
const bill_detail_list = Loadable({
    loader: () => import('../pages/accounting/bill/detail'),
    loading: Loading,
})

// 财会--商户价格设置
const merchant_data_api_price_list = Loadable({
    loader: () => import('../pages/accounting/merchant/merchantPriceSet'),
    loading: Loading,
})


const interfaceSet = Loadable({
    loader: () => import('../pages/merchant/interfaceSet/interfaceSet'),
    loading: Loading,
})
//控制面板

const CallLog = Loadable({
    loader: () => import('../pages/logAnalysis/log'),
    loading: Loading,
})
const Report = Loadable({
    loader: () => import('../pages/logAnalysis/report'),
    loading: Loading,
})
//数据服务系统
const dataQuery = Loadable({
    loader: () => import('../pages/dataServer/api-query'),
    loading: Loading,
})
const interfaceList = Loadable({
    loader: () => import('../pages/dataServer/baseApi/list'),
    loading: Loading,
})
//接口编辑
const interfaceEdit = Loadable({
    loader: () => import('../pages/dataServer/baseApi/update'),
    loading: Loading,
})
//接口新增
const interfaceAdd = Loadable({
    loader: () => import('../pages/dataServer/baseApi/insert'),
    loading: Loading,
})
//查询方式
const dataApiQueryMode = Loadable({
    loader: () => import('../pages/dataServer/dataApi/query-mode'),
    loading: Loading,
})
//数据API查询方式新增
const ApiQueryInsertConfig = Loadable({
    loader: () => import('../pages/dataServer/dataApi/query-mode/apiQuery-config'),
    loading: Loading,
})
const DataApi = Loadable({
    loader: () => import('../pages/dataServer/dataApi/list'),
    loading: Loading,
})
const baseApisource = Loadable({
    loader: () => import('../pages/dataServer/baseApi/source'),
    loading: Loading,
})
const sourceQuery = Loadable({
    loader: () => import('../pages/source/source-query'),
    loading: Loading,
})
const sourceList = Loadable({
    loader: () => import('../pages/source/provide/index'),
    loading: Loading,
})

// 接口列表
const sourceApi = Loadable({
    loader: () => import('../pages/source/provide-Api'),
    loading: Loading,
})

interface RoutesProps {
    [propname: string]: string[];
}

// 管理系统

const accountList = Loadable({
    loader: () => import('../pages/users/list/accountList'),
    loading: Loading,
})
const accountAdd = Loadable({
    loader: () => import('../pages/users/list/accountAdd'),
    loading: Loading,
})
const accountEdit = Loadable({
    loader: () => import('../pages/users/list/accountEdit'),
    loading: Loading,
})
const logging = Loadable({
    loader: () => import('../pages/users/log'),
    loading: Loading,
})
const groupEdit = Loadable({
    loader: () => import('../pages/users/list/groupEdit'),
    loading: Loading,
})

const baseEditor = Loadable({
    loader: () => import("../pages/dataEditor/baseEditor/baseEditor"),
    loading: Loading
})

export interface routeProps {
    path: string;
    name: string;
    index: number;
    component?: any;
    Icon?: React.ReactNode;
    exact: boolean;
    breadcrumbName: string;
    child: routeProps[];
    render?: React.FunctionComponent;
    extraRouter?: routeProps[];
}

interface defaultProps {
    [propname: string]: routeProps;
}

const defaultAllRoute: defaultProps = {
    dashBoard: {
        path: '/dashBoard',
        breadcrumbName: '仪表盘',
        name: '仪表盘',
        exact: true,
        index: 1,
        Icon: <IconFont type="icon-yibiaopan" />,
        render: () => <Redirect to='/home/dashBoard/merchant'/>,
        child: [
            {
                path: '/merchant',
                exact: true,
                name: '商户',
                breadcrumbName: '商户',
                child: [],
                index: 1,
                component: dashboard_merchant,
            },
            {
                path: '/provide',
                name: '供应商',
                breadcrumbName: '供应商',
                exact: true,
                child: [],
                index: 2,
                component: dashboard_provide,
            },
        ],
    },
    merchant: {
        path: '/merchant',
        breadcrumbName: '商户',
        name: '商户',
        exact: true,
        Icon: <IconFont type="icon-shanghu" />,
        index: 2,
        render: () => <Redirect to='/home/merchant/list'/>,
        child: [
            {
                path: '/list',
                name: '列表',
                breadcrumbName: '列表',
                child: [],
                exact: true,
                index: 2,
                component: merchant_list,
                extraRouter: [
                    {
                        path: '/permissionManger',
                        name: '权限管理',
                        breadcrumbName: '权限管理',
                        child: [],
                        exact: true,
                        index: 2,
                        component: PermissionManger,
                    },
                    {
                        path: '/interfaceSet',
                        name: '接口设置',
                        breadcrumbName: '接口设置',
                        child: [],
                        exact: true,
                        index: 2,
                        component: interfaceSet,
                    },
                    {
                        path: '/orderManent',
                        name: '订单管理',
                        breadcrumbName: '订单管理',
                        child: [],
                        exact: true,
                        index: 2,
                        component: orderManent,
                    },
                ],
            },
            {
                path: '/merchantReviewList',
                name: '审核列表',
                breadcrumbName: '审核列表',
                child: [],
                exact: true,
                index: 2,
                component: merchant_review_list,
                extraRouter: [
                ],
            },
            // {
            //     path: '/merchantOrderList',
            //     name: '订单列表',
            //     breadcrumbName: '订单列表',
            //     child: [],
            //     exact: true,
            //     index: 2,
            //     component: merchant_order_list,
            // },
        ],
    },
    accounting:{
        path:'/accounting',
        exact:true,
        name:'财会',
        breadcrumbName:'财会',
        index:3,
        Icon: <IconFont type="icon-caihui" />,
        render: () => {
            return <Redirect to='/home/dataServer/baseApi'/>
        },
        child:[
            {
               path:'/merchant',
               name:'商户',
               breadcrumbName:'商户',
               exact:true,
               child:[],
               extraRouter:[
                {
                    path:'/merchantDataApiPriceList',
                    name:'价格设置',
                    breadcrumbName:'价格设置',
                    child:[],
                    exact:true,
                    index:4,
                    component:merchant_data_api_price_list
                }
               ],
               component:account_merchant_list,
               index:3,
            },
            {
                path:'/expenses',
                name:'消费记录',
                breadcrumbName:'消费记录',
                component:expenses_list,
                exact:true,
                child:[],
                index:3,
             },
             {
                path:'/recharge',
                name:'充值记录',
                breadcrumbName:'充值记录',
                exact:true,
                component:recharge_list,
                child:[],
                index:3,
             },
             {
                path:'/bill',
                name:'账单记录',
                breadcrumbName:'账单记录',
                component:bill_list,
                exact:true,
                child:[],
                index:3,
                extraRouter:[
                    {
                        path:'/billDetail',
                        name:'账单详情',
                        breadcrumbName:'账单详情',
                        child:[],
                        exact:true,
                        index:3,
                        component:bill_detail_list
                    }
                ]
             }
        ]
    },
    dataServer: {
        path: '/dataServer',
        exact: true,
        name: '数据服务',
        breadcrumbName: '数据服务',
        Icon: <IconFont type="icon-a-shujufuwu" />,
        index: 4,
        render: () => {
            return <Redirect to='/home/dataServer/baseApi'/>
        },
        child: [
            {
                path: '/baseApi',
                name: '基础API',
                breadcrumbName: '基础API',
                exact: true,
                child: [],
                index: 1,
                component: interfaceList,
                extraRouter: [
                    {
                        path: '/editInterface',
                        name: '接口编辑',
                        breadcrumbName: '接口编辑',
                        child: [],
                        exact: true,
                        index: 2,
                        component: interfaceEdit,
                    },
                    {
                        path: '/interfaceAdd',
                        name: '新增接口',
                        breadcrumbName: '新增接口',
                        child: [],
                        exact: true,
                        index: 2,
                        component: interfaceAdd,
                    },
                    {
                        path: '/source',
                        name: '数据源配置',
                        breadcrumbName: '数据源配置',
                        child: [],
                        exact: true,
                        index: 2,
                        component: baseApisource,
                    },
                ],
            },
            {
                path: '/dataApi',
                child: [],
                name: '数据API',
                breadcrumbName: '数据API',
                index: 2,
                exact: true,
                component: DataApi,
                extraRouter: [
                    {
                        component: dataApiQueryMode,
                        path: '/queryMode',
                        name: '查询方式',
                        breadcrumbName: '查询方式',
                        child: [],
                        exact: true,
                        index: 3,
                    },
                    {
                        component: ApiQueryInsertConfig,
                        path: '/queryModeConfig',
                        name: '查询方式配置',
                        breadcrumbName: '查询方式配置',
                        child: [],
                        exact: true,
                        index: 3,
                    },
                ],
            },

            {
                path: '/apiQuery',
                breadcrumbName: 'API查询',
                child: [],
                name: 'API查询',
                index: 5,
                exact: true,
                component: dataQuery,
            },
        ],
    },
    source: {
        path: '/source',
        name: '数据源',
        breadcrumbName: '数据源',
        Icon: <IconFont type="icon-a-shujuyuan" />,
        index: 5,
        render: () => {
            return <Redirect to='/home/source/list'/>
        },
        child: [
            {
                path: '/list',
                name: '列表',
                breadcrumbName: '列表',
                child: [],
                exact: true,
                index: 2,
                component: sourceList,
                extraRouter: [
                    {
                        path: '/sourceApi',
                        name: '数据源接口',
                        breadcrumbName: '数据源接口',
                        child: [],
                        exact: true,
                        index: 2,
                        component: sourceApi,
                    },
                ],
            },
            // {
            //     path: '/query',
            //     name: '查询',
            //     breadcrumbName: '查询',
            //     child: [],
            //     exact: true,
            //     index: 2,
            //     component: sourceQuery,
            // },
        ],
        exact: true,
    },

    logAnalysis: {
        path: '/logAnalysis',
        name: '日志分析',
        breadcrumbName: '日志分析',
        Icon: <IconFont type="icon-rizhi" />,
        index: 6,
        exact: true,
        render: () => {
            return <Redirect to='/home/logAnalysis/log'/>
        },
        child: [
            {
                path: '/list',
                breadcrumbName: '调用日志',
                child: [],
                name: '调用日志',
                index: 1,
                exact: true,
                component: CallLog,
            },
            // {
            //     path: '/report',
            //     name: '统计报表',
            //     breadcrumbName: '统计报表',
            //     child: [],
            //     index: 2,
            //     exact: true,
            //     component: Report,
            // },
        ],
    },
    users: {
        path: '/users',
        name: '账号管理',
        breadcrumbName: '账号管理',
        index: 7,
        Icon: <IconFont type="icon-zhanghao" />,

        exact: true,
        render: () => {
            return <Redirect to='/home/users/list'/>
        },
        child: [
            {
                path: '/list',
                breadcrumbName: '列表',
                exact: true,

                child: [],
                name: '列表',
                index: 1,
                component: accountList,
                extraRouter: [
                    {
                        path: '/accountAdd',
                        name: '账号新增',
                        breadcrumbName: '账号新增',
                        child: [],
                        exact: true,
                        index: 2,
                        component: accountAdd,
                    },
                    {
                        path: '/accountEdit',
                        name: '账号编辑',
                        breadcrumbName: '账号编辑',
                        child: [],
                        exact: true,
                        index: 2,
                        component: accountEdit,
                    },
                    {
                        path: '/groupEdit',
                        name: '编辑分组',
                        breadcrumbName: '编辑分组',
                        child: [],
                        exact: true,
                        index: 2,
                        component: groupEdit,
                    },
                ],
            },
            {
                path: '/log',
                breadcrumbName: '日志',
                name: '日志',
                child: [],
                index: 2,
                exact: true,
                component: logging,
            },
        ],
    },

    // dataEditor: {
    //     path: '/dataEditor',
    //     name: '数据录入',
    //     breadcrumbName: '数据录入',
    //     index: 8,
    //     Icon: <Icon type='user'/>,
    //     exact: true,
    //     render: () => {
    //         return <Redirect to='/home/dataEditor/baseEditor'/>
    //     },
    //     child: [
    //          {
    //              path: '/baseEditor',
    //              breadcrumbName: '数据录入',
    //              exact: true,
    //              child: [],
    //              name: '数据录入',
    //              index: 1,
    //              component: baseEditor,
    //          }
    //     ],
    // },
}
/**
 * 产生 routes 数据返回的数据是按照index进行排序
 */
export default (routes: RoutesProps) => {
    const createRoutes = []
    for (let k in routes) {
        const currentRoute: routeProps = {
            ...defaultAllRoute[k],
            child: [],
        }
        const currentRoutes = routes[k]
        const defaultRoute = defaultAllRoute[k]
        for (let i = 0; i < currentRoutes.length; i++) {
            const existence = defaultRoute.child.findIndex((element, index) => {
                return element.path === '/' + currentRoutes[i]
            })
            if (existence !== -1) {
                currentRoute.child.push({
                    ...defaultRoute.child[existence],
                })
            }
        }
        createRoutes.push(currentRoute)
    }
    createRoutes.sort((pre, next) => {
        return pre.index - next.index
    })
    return createRoutes
};
