import {
    hasChild,
    sessionRead,
    getToken
} from '@/libs/base/util'
import Main from '_c/main'
import parentView from '_c/base/parent-view'
import {
    forEach
} from '@/libs/tools/tools'
import {page404} from "@/router/routers";
import config from '@/config/index'

// 加载菜单
export const loadMenu = () => {
    let list = []
    let data = sessionRead('route')
    if (!data) {
        return list
    }
    // console.log('34343td',data)
    list = formatMenu(JSON.parse(data))
    list.push(page404)
    // console.log('list',list)
    return list
}

// 格式化菜单
export const formatMenu = (list) => {
    let res = []
    forEach(list, item => {
        let obj = {
            path: item.path,
            name: item.name
        }
        obj.meta = item.meta
        // 说明是需要传id
        if (item.meta?.flgChooseId) {
            obj.path = item.path + '/:id';
        }
        if (item.component === 'Main') {
            // obj.component = Main
            // obj.component = () => import('_c/main/main.vue')
            if (item.meta.appCode === config.appCode.WEB) {// PC端应用
                obj.component = () => import('_c/main/main.vue')
            }
        } else if (item.component === 'parentView') {
            // obj.component = parentView
            obj.component = () => import('_c/base/parent-view/parent-view')
        } else if (item.component) {
            try {
                let path = item.component;
                if (item.meta.appCode === config.appCode.WEB) { // PC端应用
                    if (item.flgSameRoute) {
                        path = (path + '|').replace('/' + item.name + '|', '').replace('|', '')
                        obj.component = {...require('@/view' + path + '.vue').default}
                        obj.component.name = item.name;
                    } else {
                        obj.component = () => import('@/view' + path + '.vue')
                    }
                } else if (item.meta.appCode === config.appCode.OPERATE) { // 移动端应用
                    if (item.flgSameRoute) {
                        path = (path + '|').replace('/' + item.name + '|', '').replace('|', '')
                        obj.component = {...require('@/view-app' + path + '.vue').default}
                        obj.component.name = item.name;
                    } else {
                        obj.component = () => import('@/view-app' + path + '.vue')
                    }
                }
            } catch (e) {
                // console.log('error', e)
                obj.component = require('@/view/error-page/404.vue').default
            }
        } else {
            obj.component = () => import('@/view/error-page/404.vue')
        }
        if (hasChild(item)) {
            obj.children = formatMenu(item.children)
        }
        res.push(obj)
    })
    // console.log('3333',res)
    return res
}

/**
 * @desc   : 获取页面中对应component
 * @author : 周兴
 * @date   : 2022/5/16 15:19
 */
export const filterAsyncRouter = (asyncRouterMap, menu) => {
    let tt = asyncRouterMap.filter(route => {
        if (route.component && route.meta) {
            if (route.component === 'Main') {
                if (route.meta.appCode === config.appCode.WEB) {// PC端应用
                    route.component = Main
                }
            } else if (route.component === 'parentView') {
                route.component = parentView
            } else {
                //只有非modal的页面需要加载
                // if (!route.meta.modal) {
                try {
                    let path = route.component;
                    // path = (path?.endsWith('/edit') || path?.endsWith('/detail') || path?.endsWith('/process-setting')) ? path + '/:id' : path
                    if (route.meta.appCode === config.appCode.WEB) {// PC端应用
                        if (route.flgSameRoute) {
                            path = (path + '|').replace('/' + route.name + '|', '').replace('|', '')
                            route.component = {...require('@/view' + path + '.vue').default}
                            route.component.name = route.name;
                        } else {
                            route.component = require('@/view' + path + '.vue').default
                        }
                    } else if (route.meta.appCode === config.appCode.WEIXIN) { // 移动端应用

                    }
                    // route.path = (route.path?.endsWith('/edit') || route.path?.endsWith('/detail') || route.path?.endsWith('/process-setting')) ? route.path + '/:id' : route.path
                    // 说明是需要传id
                    // if(route.meta?.flgChooseId){
                    //   route.path = route.path + '/:id';
                    // }
                } catch (e) {
                    // console.log('error', e)
                    route.component = require('@/view/error-page/404.vue').default
                }
            }
        }
        if (route.children && route.children.length) {
            route.children = filterAsyncRouter(route.children, menu)
        }
        return true
    })
    return tt;
}

/**
 * @desc   : 根据层级 生成路由
 * @author : 洪旭东
 * @date   : 2022-05-21 13:30
 */
export const getRouteTree2 = (list, uuid, ans, parent) => {
    if (!list || list.length === 0) return ans
    let noSetList = []
    // debugger
    list.forEach(it => {
        if (it.parentUuid === uuid) {
            if (it.menuType === 0) {
                ans.children.push({
                    path: it.path,
                    menuUuid: it.menuUuid,
                    parentUuid: it.parentUuid,
                    name: it.menuName,
                    icon: it.icon,
                    image: it.fastImage ? it.fastImage + '.png' : null,
                    title: it.menuName,
                    notCache: false,
                    objectPath: it.objectPath,
                    // parentPath: it.parentPath,
                    menuTags: it.menuTags,
                    id: it.id,
                    parentId: it.parentId,
                    // meta: {
                    //   modal:it.menuTag.modal,
                    //   icon: it.menuTag.icon,
                    //   image: it.menuTag.image,
                    //   commonFlag: it.menuTag.commonFlag,
                    //   title: it.menuName,
                    //   hideInMenu: it.menuTag.hideInMenu,
                    //   notCache: !it.menuTag.cache
                    // },
                    component: it.parentUuid ? 'parentView' : 'Main',
                    children: []
                })
            } else if (it.menuType === 1) {
                // let title = it.menuTag.hideInMenu ? vm.$t(it.menuTag.name) : it.menuName
                let r = {
                    // path: it.menuTag.path ? (it.menuTag.path.endsWith('/edit') || it.menuTag.path.endsWith('/detail')) ? it.menuTag.path + '/:id' : it.menuTag.path : '',
                    path: it.path,
                    menuUuid: it.menuUuid,
                    parentUuid: it.parentUuid,
                    name: it.name,
                    icon: it.icon,
                    title: it.menuName,
                    notCache: false,
                    objectPaths: it.objectPaths,
                    flgFast: it.flgFast,
                    parentPath: it.parentPath,
                    menuTags: it.menuTags,
                    id: it.id,
                    parentId: it.parentId,
                    // meta: {
                    //   modal: it.menuTag.modal,
                    //   icon: it.menuTag.icon,
                    //   image: it.menuTag.image,
                    //   commonFlag: it.menuTag.commonFlag,
                    //   title: title,
                    //   hideInMenu: it.menuTag.hideInMenu,
                    //   notCache: !it.menuTag.cache
                    // },
                    component: it.path,
                    children: []
                }
                if (it.menuType === 2) {
                    // 四级的菜单（按钮），要同三级菜单一样，继承在parentView下
                    parent.children.push(r)
                } else {
                    ans.children.push(r)
                }
            }
        } else {
            noSetList.push(it)
        }
    })
    if (ans.children) {
        if (ans.children.length == 0) {
            // 没有子菜单的不显示
            // ans.meta.hideInMenu = true
        } else {
            ans.children.forEach(it => {
                getRouteTree2(noSetList, it.menuUuid, it, ans)
            })
        }
    }
    return ans;
}

export const removeObject = (list) => {
    for (let it of list) {
        // if (it.component){
        if (typeof it.component == 'object') {
            delete it.component
        }
        if (it.children && it.children.length > 0) {
            removeObject(it.children)
        }
    }
}



