<template>
  <a-modal
    :destroy-on-close="true"
    :title="currentActionButton.buttonName"
    :body-style="{ overflow: 'auto' }"
    :width="600"
    :visible="currentActionButton.showRollback"
    :confirm-loading="confirmLoading"
    @ok="handleOkRollback"
    @cancel="handleCancelRollback"
  >
    <sd-webflow-rollback ref="rollbackChild" :rollback-steps="currentActionButton.rollbackSteps" />
    <sd-notify-type v-model="notifyType" tag="div" :class="$style.notify" />
  </a-modal>
</template>

<script>
import { Message } from 'ant-design-vue'
import { sdLocalStorage } from '@/common/services/storage-service'
import FlowService from '../flow-service'
import components from './_import-components/sd-workflow-interfere-import'
const duration = 1
let resolve1
export default {
  name: 'SdWorkflowInterfere',
  components,
  data() {
    return {
      confirmLoading: false,
      currentActionButton: {},
      notifyType: [],
    }
  },
  methods: {
    run({ button }) {
      this.webflow
        .validateField('approveOpinion')
        .then((flag) => {
          if (flag) {
            this.currentActionButton = button
            // 如果驳回方式是驳回到上一环节，直接驳回，不弹出选择界面
            // rebackTypeType:{ all:全部环节, assign:到指定环节, previous:到上一环节, no:不能驳回 }
            if (button.attrs.rebackTypeType === 'previous') {
              const rollbacktype = this.currentActionButton.buttonId // 驳回方式
              const rollbackstep = this.currentActionButton.rollbackSteps[0] // 驳回到上一环节时直接取第一个环节
              if (
                rollbacktype &&
                rollbackstep &&
                rollbacktype !== '' &&
                rollbackstep.historyStepId
              ) {
                this.webflow.FlowData.inputs = this.webflow.getFormData()
                this.webflow
                  .checkLock()
                  .then(() => {
                    FlowService.reject(this.webflow.FlowData, rollbackstep, rollbacktype).then(
                      (_) => {
                        Message.success({
                          content: '已驳回',
                          duration: 1,
                        }).then(() => {
                          resolve1(true)
                          sdLocalStorage.removeItem(`Lock${this.webflow.FlowData.instId}`)
                          this.webflow.close(true)
                        })
                      }
                    )
                  })
                  .finally(resolve1)
              } else {
                Message.warn('未找到可驳回的环节！')
                resolve1()
              }
            } else {
              this.currentActionButton.showRollback = true
            }
          } else {
            resolve1()
          }
        })
        .catch(() => {
          resolve1()
        })
      return new Promise((resolve, reject) => {
        resolve1 = resolve
      })
    },
    handleOkRollback() {
      const rollbacktype = this.currentActionButton.buttonId // 驳回方式
      const rollbackstep = this.$refs.rollbackChild.getSelectedStepInfo() // 驳回环节
      // 校验驳回选择
      if (!rollbacktype || !rollbackstep || !rollbackstep.historyStepId) {
        Message.warn('请选择驳回环节！')
      } else if (rollbackstep.users.length === 0) {
        Message.warn('请选择驳回环节的处理人！')
      } else {
        this.confirmLoading = true
        this.webflow.FlowData.inputs = this.webflow.getFormData()
        this.webflow
          .checkLock()
          .then(() => {
            FlowService.reject(
              this.webflow.FlowData,
              rollbackstep,
              rollbacktype,
              this.notifyType
            ).then(
              (_) => {
                Message.success({ content: '已驳回', duration }).then(() => {
                  this.confirmLoading = false
                  this.currentActionButton.showRollback = false
                  sdLocalStorage.removeItem(`Lock${this.webflow.FlowData.instId}`)
                  resolve1(true)
                  this.webflow.close(true)
                })
              },
              (e) => {
                this.confirmLoading = false
                resolve1()
              }
            )
          })
          .finally(resolve1)
      }
    },
    handleCancelRollback() {
      this.currentActionButton.showRollback = false
      this.confirmLoading = false
      this.currentActionButton = { ...this.currentActionButton }
      resolve1()
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
.notify {
  margin-top: 15px;
}
</style>
