<template>
  <a-modal
    :visible="showModal"
    title="作废原因"
    :confirm-loading="confirmLoading"
    @ok="handleOk"
    @cancel="() => (showModal = false)"
  >
    <a-textarea
      v-model="remark"
      placeholder="请输入作废原因"
      :auto-size="{ minRows: 3 }"
    ></a-textarea>
  </a-modal>
</template>

<script>
import { message } from 'ant-design-vue'
import FlowService from '../flow-service'
import components from './_import-components/sd-workflow-invalid-import'

export default {
  name: 'SdWorkflowInvalid',
  metaInfo: {
    title: 'SdWorkflowInvalid',
  },
  components,
  data() {
    return {
      showModal: false,
      remark: '',
      confirmLoading: false,
    }
  },
  methods: {
    run() {
      this.showModal = true
    },
    handleOk() {
      this.confirmLoading = true
      const params = {
        processInstanceIds: this.webflow.FlowData.instId,
        flowCallbackBeanName: 'subflowDataBinderCallback',
        remark: this.remark,
      }
      FlowService.workflowCancel(params)
        .then((res) => {
          if (res) {
            this.showModal = false
            message.success('作废成功').then(() => {
              this.webflow.close(true)
            })
          } else {
            message.warn('作废失败')
          }
        })
        .catch((e) => {
          if (e.response?.data?.code === '20001009') {
            message.error({ content: e.response.data.message })
            this.showModal = false
            this.webflow.updateFlow()
            return
          }
          throw e
        })
        .finally(() => {
          this.confirmLoading = false
        })
    },
  },
}
</script>

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