// 初始化路由配置上报后端存储
import asyncRoutes from '@/router/asyncRoutes'
import apiConf from '@/api-conf'

function getBtnResource(route) {
  const name = route.name || ''
  const apiData = apiConf[`${name}`] || {}
  const resource = apiData.resource || []
  if (!resource.length) return
  const list = []
  resource.forEach(item => {
    list.push({
      resourceCode: item.id,
      resourceUiPath: '',
      resourceUrl: item.api ? item.api.join() : '',
      resourceName: item.name,
      displaySort: 0,
      resourceType: 1,
      description: '',
      icon: ''
    })
  })
  return list
}

function getPageResource(routes) {
  const res = []
  routes.forEach(item => {
    const tmp = { ...item }
    if (tmp.children && tmp.children.length > 0) {
      tmp.children = getPageResource(tmp.children)
    }
    const api = apiConf[tmp.name] ? apiConf[tmp.name].api : []
    res.push({
      resourceCode: tmp.name,
      resourceUiPath: tmp.path,
      resourceUrl: api.length ? api.join() : '',
      resourceName: tmp.meta && tmp.meta.title,
      displaySort: 0,
      resourceType: 0,
      description: '',
      icon: tmp.meta && tmp.meta.icon,
      children: tmp.children || getBtnResource(tmp)
    })
  })
  return res
}

const resource = getPageResource(asyncRoutes)
console.log(resource, 'all resource')

export default resource
