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) => {
      if (self.bean) {
        const res = await self.api?.getRoleList()
        self.bean.roleList = res.data.items.map((item:any) => ({key: item.id, name: item.name}))
      }
    }
  ],
  fliter: [
    {
      key: 'id',
      name: 'ID',
      type: 'input'
    },
    {
      key: 'username',
      name: '用户名',
      type: 'input'
    },
    {
      type: 'dialogForm',
      key: 'create',
      form: {
        title: '新建',
        data: [
          {
            key: 'username',
            name: '用户名',
            type: 'input',
            must: true
          },
          {
            key: 'account',
            name: '账号',
            type: 'input',
            must: true
          },
          {
            key: 'password',
            name: '密码',
            type: 'input',
            must: true
          },
          {
            key: 'role',
            name: '角色',
            type: 'select',
            getItems: (self) => {
              return self.bean ? self.bean.roleList : []
            },
            must: true
          },
        ],
        subFun(self, data) {
          return self.api?.addData(data)
        }
      }
    }
  ],
  tableColumns: [
    {
      key: 'id',
      name: 'ID',
      width: '80px',
      showJson: '*'
    },
    {
      key: 'username',
      name: '用户名',
      width: '200px'
    },
    {
      key: 'account',
      name: '账号',
      width: '200px'
    },
    {
      key: 'role_name',
      name: '角色',
      width: '200px'
    },
    {
      key: 'status',
      name: '状态',
      width: '100px',
      editor: {
        type: 'switch',
        openValue: 1,
        closeValue: 2,
        subFun(self, data) {
          return self.api?.updateData({id: String(data.id), status: String(data.status)})
        },
      }
    },
    {
      key: 'create_time',
      name: '创建时间',
    },
    {
      key: 'table_tools',
      name: '操作',
      buttons: [
        {
          type: 'dialogForm',
          key: 'update',
          form: {
            title: '编辑',
            type: 'warning',
            primary: 'id',
            data: [
              {
                key: 'username',
                name: '用户名',
                type: 'input',
                must: true
              },
              {
                key: 'role_id',
                name: '角色',
                type: 'select',
                getItems: (self) => {
                  return self.bean ? self.bean.roleList : []
                },
                must: true
              },
              {
                key: 'status',
                name: '状态',
                type: 'switch',
                openValue: 1,
                closeValue: 2,
                tips: '启用/禁用',
                must: true
              },
            ],
            subFun(self, data) {
              return self.api?.updateData({...data, id: String(data.id), role_id: String(data.role_id), status: String(data.status)})
            }
          }
        },
        {
          type: 'popoverConfirm',
          key: 'delete',
          confirm: {
            title: '删除',
            primary: 'id',
            subFun(self, data) {
              return self.api?.deleteData(data)
            }
          }
        }
      ]
    }
  ]
}

export default data
