import { message, Modal } from '@/common/one-ui'
import FlowService from '@/webflow/flow-service'
import { router } from '@/common/router-ready'
export default function({ button }) {
  return new Promise((resolve) => {
    Modal.confirm({
      content: '请确认是否撤办',
      onOk: () => {
        const duration = 1

        let p
        if (button.buttonId === 'workflow$encancel') {
          p = FlowService.encancel(this.FlowData, { role: this.$route.query.openAuthParameter })
        } else {
          p = FlowService.transferRetract(this.FlowData, this.userInfo, this.getFormData())
        }
        p.then((res) => {
          if (res === -1) {
            message.warn({ content: '撤回失败' })
            resolve()
          } else {
            // @ts-ignore
            message.success({ content: '已撤回', duration }).then((data) => {
              resolve()
              router.replace(`/sd-webflow/todo-pages/${res}`)
            })
          }
        }).finally(() => {
          resolve()
        })
      },
      onCancel: () => resolve(),
    })
  })
}
