<template>
  <a-modal
    :destroy-on-close="true"
    :title="currentActionButton.buttonName"
    :body-style="{ overflow: 'auto' }"
    :width="600"
    :visible="showCancelInform"
    :confirm-loading="confirmLoading"
    @ok="handleOkCancelInform"
    @cancel="handleCancelCancelInform"
  >
    <sd-users-check ref="cancelInformChild" :data-source="informUserList"></sd-users-check>
  </a-modal>
</template>

<script>
import { Message, Modal } from 'ant-design-vue'
import FlowService from '../flow-service'
import components from './_import-components/sd-workflow-cancel-inform-import'

const duration = 1
let resolve1

export default {
  name: 'SdWorkflowCancelInform',
  components,
  data() {
    return {
      currentActionButton: {},
      showCancelInform: false, // 取消知会
      confirmLoading: false,
      informUserList: [], // 被知会用户
    }
  },
  methods: {
    run({ button }) {
      this.currentActionButton = button
      const key = Symbol.for()
      const hide = Message.loading({ content: '处理中...', key, duration })
      FlowService.senderinform(this.webflow.FlowData, this.userInfo).then(
        (res) => {
          hide()
          this.informUserList = res.datas
          this.showCancelInform = true
        },
        (_) => {
          hide()
          Message.error({
            content: `获取被${
              this.currentActionButton.buttonName.indexOf('知会') !== -1 ? '知会' : '传阅'
            }人员失败`,
            key,
            duration,
          })
          resolve1()
        }
      )

      return new Promise((resolve, reject) => {
        resolve1 = resolve
      })
    },

    // 撤回知会确定
    handleOkCancelInform() {
      const selectUser = this.$refs.cancelInformChild.checkedList // 要撤销沟通的人员
      if (selectUser.length === 0) {
        const h = this.$createElement
        Modal.warning({
          title: '提示',
          content: h('div', {}, [h('p', '请选择要撤销的人员')]),
          onOk() {},
        })
      } else {
        const key = Symbol.for()
        const hideLoading = Message.loading({ content: '处理中...', key })
        this.confirmLoading = true

        FlowService.retractInform(this.webflow.FlowData, selectUser).then(
          (res) => {
            if (res?.status === 200) {
              this.webflow.updateFlow()
              this.showCancelInform = false
              let p
              const data = res.data
              if (data.fail?.length > 0) {
                if (selectUser.length > 1) {
                  let msgFunc = Modal.warning
                  let msg = `${data.fail
                    .map((item) => item.name)
                    .join(',')}已经阅读此文件，无法再进行撤回`
                  if (data.success?.length > 0) {
                    msg += '，其他用户均成功撤回'
                    msgFunc = Modal.info
                  }
                  msg += '。'
                  hideLoading()
                  p = new Promise((resolve) => {
                    msgFunc({
                      content: msg,
                      onOk() {
                        resolve(true)
                      },
                    })
                  })
                } else {
                  // 只选择了一个用户
                  p = Message.warning({
                    content: '此用户已进行了反馈，无法再进行撤回。',
                    key,
                    duration,
                  })
                }
              } else {
                p = Message.success({ content: '已撤回', key, duration })
              }
              p.then(() => {
                resolve1(true)
                this.confirmLoading = false
              })
            } else {
              this.confirmLoading = false
              Message.error({ content: '撤回失败', key, duration })
              resolve1()
            }
          },
          (_) => {
            this.confirmLoading = false
            Message.error({ content: '撤回失败', key, duration })
            resolve1()
          }
        )
      }
    },
    // 撤回知会取消
    handleCancelCancelInform() {
      this.showCancelInform = false
      resolve1()
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
</style>
