import AddressBook from '@/addressbook/group-user-service'
import { message } from '@/common/one-ui'
import FlowService from '@/webflow/flow-service'
export default function({ button }) {
  return this.validateField('approveOpinion')
    .then((flag) => {
      if (flag) {
        return FlowService.getNodeInfo(
          this.FlowData,
          { actionId: button.attrs.actionId },
          this.getFormData()
        ).then((taskCandidateInfos) => {
          return FlowService.getActionInfo(this.FlowData, button, this.getFormData())
            .then((res) => {
              return AddressBook.selectUser({
                single: !taskCandidateInfos[0]?.multi,
                secretLevel: this.getFieldValue('secretLevel'),
                hierarchical: res?.hierarchical !== '0',
              })
            })
            .then((data) => {
              if (!data) return
              const Users = data.map((item) => ({
                userAccount: item.code,
                userName: item.name,
              }))
              if (Users.length <= 0) {
                message.error('请选择要转办的人员')
                return false
              }
              if (Users.findIndex((item) => item.userAccount === this.userInfo.account) !== -1) {
                message.error('不能转办给自己')
                return false
              }

              const key = 'key'
              const duration = 1
              message.loading({ content: '处理中...', key, duration })
              return this.checkLock().then(() => {
                return FlowService.transfer(this.FlowData, Users, this.getFormData()).then(
                  (res) => {
                    // @ts-ignore
                    message.success({ content: '流程已处理', key, duration }).then(() => {
                      this.close(true)
                    })
                  },
                  () => {
                    message.error({ content: '转办失败', key, duration })
                  }
                )
              })
            })
        })
      } else {
        return Promise.resolve()
      }
    })
    .catch(() => Promise.resolve())
}
