import type { TableData } from 'lib/type/TableData'

const data: TableData = {
  rowKey: "id",
  defaultExpandAll: true,
  async fetchFun(self, data) {
    const res = await self.api?.getDataList(data)
    return res
  },
  addNods: [

  ],
  methods: {
    formtTree(self: TableData, data: any) {
      data.forEach((item: any) => {
        item.key = item.id + ""
        if (item.children && item.children.length > 0 && self.methods) {
          item.children = self.methods.formtTree(self, item.children)
        }
        else {
          delete item.children
        }
      })
      return data
    },
    getTree(arr: any) {
      // 构建一个map来存储所有节点
      const map: any = {}
      const roots: any[] = []

      // 先创建所有节点的映射
      arr.forEach((item: any) => {
        map[item.key] = { ...item, children: [] }
      })

      // 构建树结构
      arr.forEach((item: any) => {
        const node = map[item.key]
        if (item.pid === 0 || item.pid === '0') {
          // 如果是根节点，添加到roots数组
          roots.push(node)
        } else {
          // 如果有父节点，添加到父节点的children中
          if (map[item.pid]) {
            map[item.pid].children.push(node)
          } else {
            // 如果找不到父节点，也作为根节点处理
            roots.push(node)
          }
        }
      })
      return roots
    },
  },
  launchTask: [
    async (self: any) => {
      if (self.bean) {
        const res = await self.api?.getResources({ type: "", page: 1, size: 1000 })
        const items = res.data.items || []
        const data = items.map((item: any) => {
          return {
            name: item.name,
            key: String(item.id),
            id: item.id,
            pid: item.pid ? String(item.pid) : item.type,
            type: item.type,
          }
        })
        data.unshift(...[
          {
            name: '页面',
            key: 'page',
            id: 'page',
            pid: '0',
            type: 'page'
          },
          {
            name: '接口',
            key: 'api',
            id: 'api',
            pid: '0',
            type: 'api'
          }
        ])
        self.bean.resources = self.methods?.getTree(data)
      }
    }
  ],
  fliter: [
    {
      key: 'id',
      name: 'ID',
      type: 'input'
    },
    {
      key: 'username',
      name: '用户名',
      type: 'input'
    },
    {
      type: 'dialogForm',
      key: 'create',
      form: {
        title: '新建',
        data: [
          {
            key: 'name',
            name: '角色',
            type: 'input',
            must: true
          },
          {
            key: 'resource',
            name: '权限',
            type: 'cascader',
            getItems: (self: any) => {
              return self.bean ? self.bean.resources : []
            },
            cascaderProps: {
              multiple: true,
              checkStrictly: false
            },
            must: true
          },
        ],
        subFun(self, data) {
          const params = {
            ...data,
            resource: data.resource.map((i: number) => String(i))
          }
          return self.api?.addData(params)
        }
      }
    }
  ],
  tableColumns: [
    {
      key: 'id',
      name: 'ID',
      width: '80px',
      showJson: '*'
    },
    {
      key: 'name',
      name: '角色',
      width: '200px'
    },
    {
      key: 'desc',
      name: '描述',
    },
    {
      key: 'create_time',
      name: '创建时间',
    },
    {
      key: 'table_tools',
      name: '操作',
      buttons: [
        {
          type: 'dialogForm',
          key: 'update',
          form: {
            title: '编辑',
            type: 'warning',
            primary: 'id',
            beforeShow: async (self: any, form: any, row: any) => {
              const res = await self.api?.getDataDetail(row.id)
              const resources = res.data.map((item: any) => String(item.id))
              const findItem = form.data.find((i: any) => i.key === 'resource')
              findItem.values[form.show] = resources
            },
            data: [
              {
                key: 'name',
                name: '角色',
                type: 'input',
                must: true
              },
              {
                key: 'resource',
                name: '权限',
                type: 'cascader',
                cascaderProps: {
                  multiple: true,
                  checkStrictly: false
                },
                getItems: (self: any) => {
                  return self.bean ? self.bean.resources : []
                },
                must: false
              },
            ],
            subFun(self, data) {
              const params = {
                ...data,
                id: String(data.id),
                resource: data.resource.map((i: number) => String(i))
              }
              return self.api?.updateData(params)
            }
          }
        },
        {
          type: 'popoverConfirm',
          key: 'delete',
          confirm: {
            title: '删除',
            primary: 'id',
            subFun(self, data) {
              return self.api?.deleteData(data)
            }
          }
        }
      ]
    }
  ]
}

export default data
