import { constantRoutes } from '@/router'
import { getRouters } from '@/api/route'
import Layout from '@/layout/index'
import ParentView from '@/components/ParentView';
import InnerLink from '@/layout/components/InnerLink'

const permission = {
    state: {
        routes: [],
        addRoutes: [],
        defaultRoutes: [],
        topbarRouters: [],
        // sidebarRouters: [],
        sidebarRouters:JSON.parse(localStorage.getItem('SIDEBAR_ROUTERS')),
        siteConfig: {}
    },
    mutations: {
        SET_ROUTES: (state, routes) => {
            state.addRoutes = routes
            state.routes = constantRoutes.concat(routes)
        },
        SET_DEFAULT_ROUTES: (state, routes) => {
            state.defaultRoutes = constantRoutes.concat(routes)
        },
        // SET_TOPBAR_ROUTES: (state, routes) => {
        //     // 顶部导航菜单默认添加控制台栏指向首页
        //     const index = [{
        //         path: 'index',
        //         meta: { title: '营收总览', icon: 'dashboard' }
        //     }]
        //     state.topbarRouters = routes.concat(index);
        // },
        SET_SIDEBAR_ROUTERS: (state, routes) => {
            state.sidebarRouters = routes
        },
        SET_SITE_CONFIG: (state, config) => {
            state.siteConfig = config
        },
    },
    actions: {
        // 生成路由
        GenerateRoutes({ commit }) {
            return new Promise(resolve => {
                // 向后端请求路由数据
                let indexs = [
                    // {
                    //     alwaysShow: false,
                    //     children: [{ path: "outpatientGH", component: "jiesuan/outpatientGH", type: 2, name: "Refund", meta: { title: "门诊挂号结算", icon: "el-icon-monitor" } }],
                    //     component: "Layout",
                    //     meta: { title: "结算", icon: "el-icon-monitor" },
                    //     name: "Refund_1",
                    //     path: "/yibaomanager",
                    //     type: 1,
                    // },
                    // {
                    //     alwaysShow: false,
                    //     children: [{ path: "outpatientGHT", component: "jiesuan/outpatientGHT", type: 2, name: "Refund", meta: { title: "门诊挂号结算退号", icon: "el-icon-monitor" } }],
                    //     component: "Layout",
                    //     meta: { title: "结算", icon: "el-icon-monitor" },
                    //     name: "Refund_1",
                    //     path: "/yibaomanager",
                    //     type: 1,
                    // },

                    {
                        alwaysShow: false,
                        children: [{ path: "outpatient", component: "jiesuan/outpatient", type: 2, name: "Refund", meta: { title: "门诊医保结算", icon: "el-icon-monitor" } }],
                        component: "Layout",
                        meta: { title: "门诊医保结算", icon: "el-icon-monitor" },
                        name: "Refund_1",
                        path: "/yibaomanager",
                        type: 1,
                    },
                    {
                        alwaysShow: false,
                        children: [{ path: "outpatientTwo", component: "jiesuan/outpatientTwo", type: 2, name: "Refund", meta: { title: "门诊自费转医保结算", icon: "el-icon-monitor" } }],
                        component: "Layout",
                        meta: { title: "门诊自费转医保结算", icon: "el-icon-monitor" },
                        name: "Refund_1",
                        path: "/yibaomanager",
                        type: 1,
                    },
                    {
                        alwaysShow: false,
                        children: [{ path: "outpatientTwoSFC", component: "jiesuan/outpatientTwoSFC", type: 2, name: "Refund", meta: { title: "收费处自费转医保结算", icon: "el-icon-monitor" } }],
                        component: "Layout",
                        meta: { title: "收费处自费转医保结算", icon: "el-icon-monitor" },
                        name: "Refund_1",
                        path: "/yibaomanager",
                        type: 1,
                    },
                    {
                        alwaysShow: false,
                        children: [{ path: "outpatientCharging", component: "jiesuan/outpatientCharging", type: 2, name: "Refund", meta: { title: "门诊收费处结算", icon: "el-icon-monitor" } }],
                        component: "Layout",
                        meta: { title: "门诊收费处结算", icon: "el-icon-monitor" },
                        name: "Refund_1",
                        path: "/yibaomanager",
                        type: 1,
                    },
                    {
                        alwaysShow: false,
                        children: [{ path: "outpatientYfang", component: "jiesuan/outpatientYfang", type: 2, name: "Refund", meta: { title: "药房药库结算", icon: "el-icon-monitor" } }],
                        component: "Layout",
                        meta: { title: "药房药库结算", icon: "el-icon-monitor" },
                        name: "Refund_1",
                        path: "/yibaomanager",
                        type: 1,
                    },
                    {
                        alwaysShow: false,
                        children: [{ path: "refound", component: "jiesuan/refound", type: 2, name: "Refund", meta: { title: "门诊撤销结算", icon: "el-icon-monitor" } }],
                        component: "Layout",
                        meta: { title: "门诊撤销结算", icon: "el-icon-monitor" },
                        name: "Refund_1",
                        path: "/yibaomanager",
                        type: 1,
                    },
                    {
                        alwaysShow: false,
                        children: [{ path: "refoundSFC", component: "jiesuan/refoundSFC", type: 2, name: "Refund", meta: { title: "收费处撤销结算", icon: "el-icon-monitor" } }],
                        component: "Layout",
                        meta: { title: "门诊撤销结算", icon: "el-icon-monitor" },
                        name: "Refund_1",
                        path: "/yibaomanager",
                        type: 1,
                    },
                    {
                        alwaysShow: false,
                        children: [{ path: "patientInfo", component: "jiesuan/patientInfo", type: 2, name: "Refund", meta: { title: "获取患者余额", icon: "el-icon-monitor" } }],
                        component: "Layout",
                        meta: { title: "获取患者余额", icon: "el-icon-monitor" },
                        name: "Refund_1",
                        path: "/yibaomanager",
                        type: 1,
                    },
                    {
                        alwaysShow: false,
                        children: [{ path: "dwText", component: "jiesuan/dwText", type: 2, name: "Refund", meta: { title: "地纬报错修改", icon: "el-icon-monitor" } }],
                        component: "Layout",
                        meta: { title: "地纬报错修改", icon: "el-icon-monitor" },
                        name: "Refund_1",
                        path: "/yibaomanager",
                        type: 1,
                    },
                    // {
                    //     alwaysShow: false,
                    //     children: [{ path: "compare", component: "drug/compare", type: 2, name: "Refund", meta: { title: "医院项目对照", icon: "el-icon-monitor" } }],
                    //     component: "Layout",
                    //     meta: { title: "医院项目对照", icon: "el-icon-monitor" },
                    //     name: "Refund_1",
                    //     path: "/yibaomanager",
                    //     type: 1,
                    // },
                    // {
                    //     alwaysShow: true,
                    //     children: [
                    //         // {
                    //         //     component: "bed/list",
                    //         //     meta: { title: "住院患者管理", icon: "" },
                    //         //     name: "Bed",
                    //         //     path: "bed",
                    //         //     type: 2
                    //         // },
                    //         {
                    //             component: "patient/info",
                    //             meta: { title: "住院医保登记", icon: "" },
                    //             name: "Patient",
                    //             path: "patient",
                    //             type: 2
                    //         },
                    //         {
                    //             component: "jiesuan/preSettlement",
                    //             meta: { title: "住院医保结算", icon: "" },
                    //             name: "Jiesuan",
                    //             path: "jiesuan",
                    //             type: 2
                    //         }, {
                    //             component: "jiesuan/deletepre",
                    //             meta: { title: "撤销出院", icon: "" },
                    //             name: "deletepre",
                    //             path: "deletepre",
                    //             type: 2
                    //         },{
                    //             component: "jiesuan/hospitalHistory",
                    //             meta: { title: "住院结算历史", icon: "" },
                    //             name: "hospitalHistory",
                    //             path: "hospitalHistory",
                    //             type: 2
                    //         },
                    //     ]
                    //     ,
                    //     component: "Layout",
                    //     meta: { title: "住院患者管理", icon: "el-icon-platform-eleme" },
                    //     name: "Yibaomanager",
                    //     path: "/yibaomanager",
                    //     redirect: "compare",
                    //     type: 1
                    // },
                    // {
                    //     alwaysShow: false,
                    //     children: [{ path: "puled", component: "inned/puled", type: 2, name: "Refund", meta: { title: "结算清单上传", icon: "el-icon-monitor" } }],
                    //     component: "Layout",
                    //     meta: { title: "结算清单上传", icon: "el-icon-monitor" },
                    //     name: "Refund_1",
                    //     path: "/yibaomanager",
                    //     type: 1,
                    // },

                    // {
                    //     alwaysShow: false,
                    //     children: [{ path: "jiesuantj", component: "jiesuan/jiesuantj", type: 2, name: "Refund", meta: { title: "结算数据统计", icon: "el-icon-monitor" } }],
                    //     component: "Layout",
                    //     meta: { title: "结算数据统计", icon: "el-icon-monitor" },
                    //     name: "Refund_1",
                    //     path: "/yibaomanager",
                    //     type: 1,
                    // },
                    // {
                    //     alwaysShow: true,
                    //     children: [{
                    //         component: "system/log/index",
                    //         meta: { title: "平台操作日志", icon: "" },
                    //         name: "Log",
                    //         path: "log",
                    //         type: 2
                    //     },
                    //     {
                    //         component: "journal/contrast",
                    //         meta: { title: "账单拉取日志", icon: "" },
                    //         name: "Contrast",
                    //         path: "contrast",
                    //         type: 2
                    //     }],
                    //     component: "Layout",
                    //     meta: { title: "日志管理", icon: "el-icon-files" },
                    //     name: "Journal",
                    //     path: "/journal",
                    //     redirect: "log",
                    //     type: 1
                    // },
                    // {
                    //     alwaysShow: true,
                    //     children: [{
                    //         component: "system/role/index",
                    //         meta: { title: "角色管理", icon: "" },
                    //         name: "Role",
                    //         path: "role",
                    //         type: 2
                    //     },
                    //     {
                    //         component: "system/staff/index",
                    //         meta: { title: "用户管理", icon: "" },
                    //         name: "Staff",
                    //         path: "staff",
                    //         type: 2
                    //     },
                    //     {
                    //         component: "system/config/index",
                    //         meta: { title: "系统配置", icon: "" },
                    //         name: "Config",
                    //         path: "config",
                    //         type: 2
                    //     },
                    //     // {
                    //     //     component: "system/menu/index",
                    //     //     meta: { title: "菜单管理", icon: "" },
                    //     //     name: "Menu",
                    //     //     path: "menu",
                    //     //     type: 2
                    //     // }
                    // ],
                    //     component: "Layout",
                    //     meta: { title: "系统管理", icon: "el-icon-setting" },
                    //     name: "System",
                    //     path: "/system",
                    //     redirect: "channel",
                    //     type: 1
                    // }

                ]
                let siteConfig = {
                    site_back_image: "",
                    site_beian: "苏ICP备xxxxx号",
                    site_copyright: "© 2015-2023 泛喜健康版权所有",
                    site_hospital: "东营市人民医院",
                    site_icon: "",
                    site_ip_blacklist: "2223.8.9,127.0.0.1,211.234.98.112",
                    site_isreg: "1",
                    site_logo: "",
                    site_open_time: "6,7,5",
                    site_switch: "1",
                    site_title: "泛喜统一结算平台"
                }
                // getRouters().then((response) => {
                let indate = {
                    component: "inned/puled",
                    meta: { title: "核对", icon: "" },
                    name: "Puled",
                    path: "puled",
                    type: 2
                }
                // let mdate = response.data.menuInfo[4].children.push(indate)
                const sdata = JSON.parse(JSON.stringify(indexs))
                const rdata = JSON.parse(JSON.stringify(indexs))
                const sidebarRoutes = filterAsyncRouter(sdata)
                const rewriteRoutes = filterAsyncRouter(rdata, false, true)
                rewriteRoutes.push({ path: '*', redirect: '/404', hidden: true })
                commit('SET_ROUTES', rewriteRoutes)
                localStorage.setItem('SIDEBAR_ROUTERS', JSON.stringify(constantRoutes.concat(sidebarRoutes)))
                commit('SET_SIDEBAR_ROUTERS', constantRoutes.concat(sidebarRoutes))
                commit('SET_DEFAULT_ROUTES', sidebarRoutes)
                // commit('SET_TOPBAR_ROUTES', sidebarRoutes)
                commit('SET_SITE_CONFIG', siteConfig)
                resolve(rewriteRoutes)
                // })
            })
        }
    }
}

// 遍历后台传来的路由字符串，转换为组件对象
function filterAsyncRouter(asyncRouterMap, lastRouter = false, type = false) {
    return asyncRouterMap.filter(route => {
        if (type && route.children) {
            route.children = filterChildren(route.children)
        }
        if (route.component) {
            // Layout ParentView 组件特殊处理
            if (route.component === 'Layout') {
                route.component = Layout
            } else if (route.component === 'ParentView') {
                route.component = ParentView
            } else if (route.component === 'InnerLink') {
                route.component = InnerLink
            } else {
                route.component = loadView(route.component)
            }
        }
        if (route.children != null && route.children && route.children.length) {
            route.children = filterAsyncRouter(route.children, route, type)
        } else {
            delete route['children']
            delete route['redirect']
        }
        return true
    })
}

function filterChildren(childrenMap, lastRouter = false) {
    var children = []
    childrenMap.forEach((el, index) => {
        if (el.children && el.children.length) {
            if (el.component === 'ParentView') {
                el.children.forEach(c => {
                    c.path = el.path + '/' + c.path
                    if (c.children && c.children.length) {
                        children = children.concat(filterChildren(c.children, c))
                        return
                    }
                    children.push(c)
                })
                return
            }
        }
        if (lastRouter) {
            el.path = lastRouter.path + '/' + el.path
        }
        children = children.concat(el)
    })
    return children
}

export const loadView = (view) => { // 路由懒加载
    return (resolve) => require([`@/views/${view}`], resolve)
}

export default permission
