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

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

  ],
  launchTask: [
    async (self: any) => {
      if (self.bean) {
        const res = await self.api?.getResources({type: "page"})
        const res2 = await self.api?.getResources({type: "api"})
        self.bean.resources = [...res.data.items, ...res2.data.items].map((item:any) => {
          const type = item.type === 'page' ? '页面' : '接口'
          return {
            key: item.id,
            name: `【${type}】${item.name}`
          }
        })
      }
    }
  ],
  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: 'select',
            getItems: (self: any) => {
              return self.bean ? self.bean.resources : []
            },
            multiple: true,
            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',
            data: [
              {
                key: 'name',
                name: '角色',
                type: 'input',
                must: true
              },
              {
                key: 'resource',
                name: '权限',
                type: 'select',
                getItems: (self: any) => {
                  return self.bean ? self.bean.resources : []
                },
                multiple: true,
                must: true
              },
            ],
            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
