import type { TableData } from 'lib/type/TableData'
import "./index.less"
const data: TableData = {
  rowKey: "id",
  defaultExpandAll: true,
  async fetchFun(self, data) {
    const res = await self.api?.getDataList(data)
    if (self.bean) {
      const items = res.data.items || []
      const data = items.map((item: any) => {
        return {
          name: item.name,
          key: String(item.id),
          id: item.id,
          pid: String(item.pid),
          color: 'category',
          create_time: item.create_time,
          method: item.method,
          type: item.type,
          path: item.path,
        }
      })
      self.bean.categoryList = self.methods?.getTree(data)
      res.data.items = self.bean.categoryList
      self.bean.allList = data
    }
    return res
  },
  addNods: [

  ],
  launchTask: [
  ],
  methods: {
    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
    },
  },
  fliter: [
    {
      key: 'id',
      name: 'ID',
      type: 'input'
    },
    {
      key: 'name',
      name: '名称',
      type: 'input'
    },
    {
      key: 'type',
      name: '类型',
      type: 'select',
      items: [
        { key: 'page', name: '页面' },
        { key: 'api', name: 'API' },
      ],
      value: 'page'
    },
    {
      key: 'path',
      name: '路径',
      type: 'input',
    },
    {
      type: 'dialogForm',
      key: 'create',
      form: {
        title: '新建',
        data: [
          {
            key: 'pid',
            name: '父级',
            type: 'cascader',
            must: false,
            getItems(self: TableData) {
              return self.bean ? self.bean.categoryList : []
            }
          },
          {
            key: 'name',
            name: '名称',
            type: 'input',
            must: true
          },
          {
            key: 'type',
            name: '类型',
            type: 'select',
            items: [
              { key: 'page', name: '页面' },
              { key: 'api', name: 'API' },
            ],
            must: true,
            value: 'page'
          },
          {
            key: 'path',
            name: '路径',
            type: 'input',
            must: false
          },
          {
            key: 'method',
            name: '方法',
            type: 'input',
          },
        ],
        subFun(self, data) {
          return self.api?.addData(data)
        }
      }
    }
  ],
  tableColumns: [
    {
      key: 'id',
      name: 'ID',
      width: '80px',
      showJson: '*'
    },
    {
      key: 'pid',
      name: 'PID',
      width: '80px',
      showJson: '*'
    },
    {
      key: 'name',
      name: '名称',
      width: '200px'
    },
    {
      key: 'type',
      name: '类型',
    },
    {
      key: 'path',
      name: '路径',
    },
    {
      key: 'method',
      name: '方法',
    },
    {
      key: 'create_time',
      name: '创建时间',
    },
    {
      key: 'table_tools',
      name: '操作',
      buttons: [
        {
          type: 'dialogForm',
          key: 'update',
          form: {
            title: '编辑',
            type: 'warning',
            primary: 'id',
            data: [
              {
                key: 'pid',
                name: '父级',
                type: 'cascader',
                must: false,
                getItems(self: TableData) {
                  return self.bean ? self.bean.categoryList : []
                }
              },
              {
                key: 'name',
                name: '名称',
                type: 'input',
                must: true
              },
              {
                key: 'type',
                name: '类型',
                type: 'select',
                must: true,
                items: [
                  { key: 'page', name: '页面' },
                  { key: 'api', name: 'API' },
                ],
                value: 'page'
              },
              {
                key: 'path',
                name: '路径',
                type: 'input',
                must: false
              },
              {
                key: 'method',
                name: '方法',
                type: 'input',
              },
            ],
            subFun(self, data) {
              return self.api?.updateData({ ...data, id: String(data.id) })
            }
          }
        },
        {
          type: 'popoverConfirm',
          key: 'delete',
          confirm: {
            title: '删除',
            primary: 'id',
            subFun(self, data) {
              return self.api?.deleteData(data)
            }
          }
        }
      ]
    }
  ]
}

export default data
