<template>
  <a-modal
    destroy-on-close
    :title="currentActionButton.buttonName"
    :body-style="{ overflow: 'auto' }"
    :width="600"
    :visible="showCancelInform"
    :confirm-loading="confirmLoading"
    @ok="handleOkCancelInform"
    @cancel="handleCancelCancelInform"
  >
    <sd-users-check
      ref="cancelInformChild"
      v-model="selectCancelUser"
      :data-source="informUserList"
    />
    <span v-if="canceledUserList && canceledUserList.length > 0" :class="$style.canceled"
      >同时终止如下用户的沟通:{{ canceledUserList }}</span
    >
  </a-modal>
</template>

<script>
import { Message, Modal } from 'ant-design-vue'
import FlowService from '../flow-service'
import components from './_import-components/sd-workflow-cancel-comm-import'
const duration = 1
let resolve1
export default {
  name: 'SdWorkflowCancelComm',
  components,
  data() {
    return {
      currentActionButton: {},
      showCancelInform: false,
      confirmLoading: false,
      informUserList: [],
      canceledUserList: '', // 同时被终止沟通用户
      selectCancelUser: [],
    }
  },
  watch: {
    selectCancelUser() {
      this.canceledUserList = ''
      if (this.selectCancelUser.length > 0) {
        const getSelectUser = (usersArr) => {
          let canceledUserList = []
          usersArr.forEach((item) => {
            if (item.childrens && item.childrens.length > 0) {
              item.childrens.map((childItem) => {
                canceledUserList.push(childItem.receiverName)
              })
              canceledUserList = canceledUserList.concat(getSelectUser(item.childrens))
            }
          })
          return canceledUserList
        }
        this.canceledUserList = getSelectUser(this.selectCancelUser).toString()
      }
    },
  },
  methods: {
    run({ button }) {
      this.canceledUserList = ''
      this.currentActionButton = button
      const key = Symbol.for()
      const hide = Message.loading({ content: '处理中...', key, duration })
      FlowService.getConnectTree(
        this.webflow.FlowData.instId,
        this.webflow.FlowData.attrs.stepN,
        0
      ).then(
        (res) => {
          hide()
          this.informUserList = res.data.map((item) => {
            item.name = item.receiverName
            return item
          })
          this.showCancelInform = true
        },
        (_) => {
          hide()
          Message.error({
            content: `获取被沟通人员失败`,
            key,
            duration,
          })
          resolve1()
        }
      )

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

    handleOkCancelInform() {
      if (this.selectCancelUser.length === 0) {
        const h = this.$createElement
        Modal.warning({
          title: '提示',
          content: h('div', {}, [h('p', '请选择要撤销的人员')]),
          onOk() {},
        })
      } else {
        const key = Symbol.for()
        Message.loading({ content: '处理中...', key })
        this.confirmLoading = true
        FlowService.cancelFeed(this.webflow.FlowData, this.selectCancelUser).then(
          (res) => {
            if (res) {
              this.showCancelInform = false
              this.confirmLoading = false
              resolve1(true)
              Message.success({ content: '已终止', key, duration }).then(() => {
                if (res.toDoId && res.module === 'Workflow') {
                  this.$router.replace(`/sd-webflow/todo-pages/${res.toDoId}`)
                } else this.webflow.close(true)
              })
            }
          },
          (_) => {
            this.confirmLoading = false
            Message.error({ content: '终止沟通失败', key, duration })
            resolve1()
          }
        )
      }
      this.showCancelComm = false
    },
    // 终止沟通取消
    handleCancelCancelInform() {
      this.showCancelInform = false
      resolve1()
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.canceled {
  color: $error-color;
}
</style>
